1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2019-2020 ARM Limited. All rights reserved.
3 *
4 * $Date:         09. December 2019
5 * $Revision:     V1.0.0
6 *
7 * Project:       CMSIS DSP Library
8 * Title:         arm_svm_example_f32.c
9 *
10 * Description:   Example code demonstrating how to use SVM functions.
11 *
12 * Target Processor: Cortex-M/Cortex-A
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *   - Redistributions of source code must retain the above copyright
18 *     notice, this list of conditions and the following disclaimer.
19 *   - Redistributions in binary form must reproduce the above copyright
20 *     notice, this list of conditions and the following disclaimer in
21 *     the documentation and/or other materials provided with the
22 *     distribution.
23 *   - Neither the name of ARM LIMITED nor the names of its contributors
24 *     may be used to endorse or promote products derived from this
25 *     software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 * -------------------------------------------------------------------- */
40 
41 /**
42  * @addtogroup groupExamples
43  * @{
44  *
45  * @defgroup SVMExample SVM Example
46  *
47  * \par Description:
48  * \par
49  * Demonstrates the use of SVM functions. It is complementing the tutorial
50  * about classical ML with CMSIS-DSP and python scikit-learn:
51  * https://developer.arm.com/solutions/machine-learning-on-arm/developer-material/how-to-guides/implement-classical-ml-with-arm-cmsis-dsp-libraries
52  *
53  * \example arm_svm_example_f32.c
54  *
55  * @} */
56 
57 #include <math.h>
58 #include <stdio.h>
59 #include "arm_math.h"
60 
61 /*
62   The polynomial SVM instance containing all parameters.
63   Those parameters can be generated with the python library scikit-learn.
64  */
65 arm_svm_polynomial_instance_f32 params;
66 
67 /*
68   Parameters generated by a training of the SVM classifier
69   using scikit-learn and some random input data.
70  */
71 #define NB_SUPPORT_VECTORS 11
72 
73 /*
74   Dimension of the vector space. A vector is your feature.
75   It could, for instance, be the pixels of a picture or the FFT of a signal.
76  */
77 #define VECTOR_DIMENSION 2
78 
79 const float32_t dualCoefficients[NB_SUPPORT_VECTORS]={-0.01628988f, -0.0971605f,
80   -0.02707579f,  0.0249406f,   0.00223095f,  0.04117345f,
81   0.0262687f,   0.00800358f,  0.00581823f,  0.02346904f,  0.00862162f}; /* Dual coefficients */
82 
83 const float32_t supportVectors[NB_SUPPORT_VECTORS*VECTOR_DIMENSION]={ 1.2510991f,   0.47782799f,
84  -0.32711859f, -1.49880648f, -0.08905047f,  1.31907242f,
85   1.14059333f,  2.63443767f, -2.62561524f,  1.02120701f,
86  -1.2361353f,  -2.53145187f,
87   2.28308122f, -1.58185875f,  2.73955981f,  0.35759327f,
88   0.56662986f,  2.79702016f,
89  -2.51380816f,  1.29295364f, -0.56658669f, -2.81944734f}; /* Support vectors */
90 
91 /*
92   Class A is identified with value 0.
93   Class B is identified with value 1.
94 
95   This array is used by the SVM functions to do a conversion and ease the comparison
96   with the Python code where different values could be used.
97  */
98 const int32_t   classes[2]={0,1};
99 
100 
main(void)101 int32_t main(void)
102 {
103   /* Array of input data */
104   float32_t in[VECTOR_DIMENSION];
105 
106   /* Result of the classifier */
107   int32_t result;
108 
109 
110   /*
111     Initialization of the SVM instance parameters.
112     Additional parameters (intercept, degree, coef0 and gamma) are also coming from Python.
113    */
114   arm_svm_polynomial_init_f32(&params,
115     NB_SUPPORT_VECTORS,
116     VECTOR_DIMENSION,
117     -1.661719f,        /* Intercept */
118     dualCoefficients,
119     supportVectors,
120     classes,
121     3,                 /* degree */
122     1.100000f,         /* Coef0 */
123     0.500000f          /* Gamma */
124   );
125 
126 
127   /*
128     Input data.
129     It is corresponding to a point inside the first class.
130    */
131   in[0] = 0.4f;
132   in[1] = 0.1f;
133 
134   arm_svm_polynomial_predict_f32(&params, in, &result);
135 
136   /* Result should be 0 : First class */
137 #if defined(SEMIHOSTING)
138   printf("Result = %d\n", result);
139 #endif
140 
141   /*
142     This input vector is corresponding to a point inside the second class.
143    */
144   in[0] = 3.0f;
145   in[1] = 0.0f;
146 
147   arm_svm_polynomial_predict_f32(&params, in, &result);
148 
149   /* Result should be 1 : Second class */
150 #if defined(SEMIHOSTING)
151   printf("Result = %d\n", result);
152 #endif
153 
154 #if !defined(SEMIHOSTING)
155   while (1); /* main function does not return */
156 #endif
157 }
158 
159 
160 
161