/ Drivers / CMSIS / DSP / Examples / ARM / arm_svm_example / arm_svm_example_f32.c
arm_svm_example_f32.c
  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   * @ingroup groupExamples
 43   */
 44  
 45  /**
 46   * @defgroup SVMExample SVM Example
 47   *
 48   * \par Description:
 49   * \par
 50   * Demonstrates the use of SVM functions. It is complementing the tutorial
 51   * about classical ML with CMSIS-DSP and python scikit-learn:
 52   * https://developer.arm.com/solutions/machine-learning-on-arm/developer-material/how-to-guides/implement-classical-ml-with-arm-cmsis-dsp-libraries
 53   *
 54   */
 55  
 56  
 57  /** \example arm_svm_example_f32.c
 58    */
 59  
 60  #include <math.h>
 61  #include <stdio.h>
 62  #include "arm_math.h"
 63  
 64  /* 
 65    The polynomial SVM instance containing all parameters.
 66    Those parameters can be generated with the python library scikit-learn.
 67   */
 68  arm_svm_polynomial_instance_f32 params;
 69  
 70  /*
 71    Parameters generated by a training of the SVM classifier
 72    using scikit-learn and some random input data.
 73   */
 74  #define NB_SUPPORT_VECTORS 11
 75  
 76  /*
 77    Dimension of the vector space. A vector is your feature.
 78    It could, for instance, be the pixels of a picture or the FFT of a signal.
 79   */
 80  #define VECTOR_DIMENSION 2
 81  
 82  const float32_t dualCoefficients[NB_SUPPORT_VECTORS]={-0.01628988f, -0.0971605f,
 83    -0.02707579f,  0.0249406f,   0.00223095f,  0.04117345f,
 84    0.0262687f,   0.00800358f,  0.00581823f,  0.02346904f,  0.00862162f}; /* Dual coefficients */
 85  
 86  const float32_t supportVectors[NB_SUPPORT_VECTORS*VECTOR_DIMENSION]={ 1.2510991f,   0.47782799f,
 87   -0.32711859f, -1.49880648f, -0.08905047f,  1.31907242f,
 88    1.14059333f,  2.63443767f, -2.62561524f,  1.02120701f,
 89   -1.2361353f,  -2.53145187f,
 90    2.28308122f, -1.58185875f,  2.73955981f,  0.35759327f,
 91    0.56662986f,  2.79702016f,
 92   -2.51380816f,  1.29295364f, -0.56658669f, -2.81944734f}; /* Support vectors */
 93  
 94  /*
 95    Class A is identified with value 0.
 96    Class B is identified with value 1.
 97    
 98    This array is used by the SVM functions to do a conversion and ease the comparison
 99    with the Python code where different values could be used.
100   */
101  const int32_t   classes[2]={0,1};
102  
103  
104  int32_t main(void)
105  {
106    /* Array of input data */
107    float32_t in[VECTOR_DIMENSION];
108  
109    /* Result of the classifier */
110    int32_t result;
111    
112  
113    /*
114      Initialization of the SVM instance parameters.
115      Additional parameters (intercept, degree, coef0 and gamma) are also coming from Python.
116     */
117    arm_svm_polynomial_init_f32(&params,
118      NB_SUPPORT_VECTORS,
119      VECTOR_DIMENSION,
120      -1.661719f,        /* Intercept */
121      dualCoefficients,
122      supportVectors,
123      classes,
124      3,                 /* degree */
125      1.100000f,         /* Coef0 */
126      0.500000f          /* Gamma */
127    );
128  
129  
130    /*
131      Input data.
132      It is corresponding to a point inside the first class.
133     */
134    in[0] = 0.4f;
135    in[1] = 0.1f;
136  
137    arm_svm_polynomial_predict_f32(&params, in, &result);
138  
139    /* Result should be 0 : First class */
140  #if defined(SEMIHOSTING)
141    printf("Result = %d\n", result);
142  #endif
143  
144    /*
145      This input vector is corresponding to a point inside the second class.
146     */
147    in[0] = 3.0f;
148    in[1] = 0.0f;
149  
150    arm_svm_polynomial_predict_f32(&params, in, &result);
151    
152    /* Result should be 1 : Second class */
153  #if defined(SEMIHOSTING)
154    printf("Result = %d\n", result);
155  #endif
156  
157  #if !defined(SEMIHOSTING)
158    while (1); /* main function does not return */
159  #endif
160  }
161  
162  
163