arm_svm_example_f32.c 5.0 KB

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