arm_bayes_example_f32.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_bayes_example_f32.c
  9. *
  10. * Description: Example code demonstrating how to use Bayes 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 Examples
  42. * @{
  43. *
  44. * @defgroup BayesExample Bayes Example
  45. *
  46. * \par Description:
  47. * \par
  48. * Demonstrates the use of Bayesian classifier 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_bayes_example_f32.c
  53. *
  54. * @} */
  55. #include <math.h>
  56. #include <stdio.h>
  57. #include "arm_math.h"
  58. /*
  59. Those parameters can be generated with the python library scikit-learn.
  60. */
  61. arm_gaussian_naive_bayes_instance_f32 S;
  62. #define NB_OF_CLASSES 3
  63. #define VECTOR_DIMENSION 2
  64. const float32_t theta[NB_OF_CLASSES*VECTOR_DIMENSION] = {
  65. 1.4539529436590528f, 0.8722776016801852f,
  66. -1.5267934452462473f, 0.903204577814203f,
  67. -0.15338006360932258f, -2.9997913665803964f
  68. }; /**< Mean values for the Gaussians */
  69. const float32_t sigma[NB_OF_CLASSES*VECTOR_DIMENSION] = {
  70. 1.0063470889514925f, 0.9038018246524426f,
  71. 1.0224479953244736f, 0.7768764290432544f,
  72. 1.1217662403241206f, 1.2303890106020325f
  73. }; /**< Variances for the Gaussians */
  74. const float32_t classPriors[NB_OF_CLASSES] = {
  75. 0.3333333333333333f, 0.3333333333333333f, 0.3333333333333333f
  76. }; /**< Class prior probabilities */
  77. int32_t main(void)
  78. {
  79. /* Array of input data */
  80. float32_t in[2];
  81. /* Result of the classifier */
  82. float32_t result[NB_OF_CLASSES];
  83. float32_t temp[NB_OF_CLASSES];
  84. float32_t maxProba;
  85. uint32_t index;
  86. S.vectorDimension = VECTOR_DIMENSION;
  87. S.numberOfClasses = NB_OF_CLASSES;
  88. S.theta = theta;
  89. S.sigma = sigma;
  90. S.classPriors = classPriors;
  91. S.epsilon=4.328939296523643e-09f;
  92. in[0] = 1.5f;
  93. in[1] = 1.0f;
  94. index = arm_gaussian_naive_bayes_predict_f32(&S, in, result,temp);
  95. maxProba = result[index];
  96. #if defined(SEMIHOSTING)
  97. printf("Class = %d\n", index);
  98. printf("Max proba = %f\n", (double)maxProba);
  99. #endif
  100. in[0] = -1.5f;
  101. in[1] = 1.0f;
  102. index = arm_gaussian_naive_bayes_predict_f32(&S, in, result,temp);
  103. maxProba = result[index];
  104. #if defined(SEMIHOSTING)
  105. printf("Class = %d\n", index);
  106. printf("Max proba = %f\n", (double)maxProba);
  107. #endif
  108. in[0] = 0.0f;
  109. in[1] = -3.0f;
  110. index = arm_gaussian_naive_bayes_predict_f32(&S, in, result,temp);
  111. maxProba = result[index];
  112. #if defined(SEMIHOSTING)
  113. printf("Class = %d\n", index);
  114. printf("Max proba = %f\n", (double)maxProba);
  115. #endif
  116. #if !defined(SEMIHOSTING)
  117. while (1); /* main function does not return */
  118. #endif
  119. }