NMSIS-DSP  Version 1.3.1
NMSIS DSP Software Library
riscv_bayes_example_f32.c
/* ----------------------------------------------------------------------
* Copyright (C) 2019-2020 ARM Limited. All rights reserved.
* Copyright (c) 2019 Nuclei Limited. All rights reserved.
*
* $Date: 09. December 2019
* $Revision: V1.0.0
*
* Project: NMSIS DSP Library
* Title: riscv_bayes_example_f32.c
*
* Description: Example code demonstrating how to use Bayes functions.
*
* Target Processor: RISC-V Cores
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* - Neither the name of ARM LIMITED nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* -------------------------------------------------------------------- */
#include <math.h>
#include <stdio.h>
#include "riscv_math.h"
/*
Those parameters can be generated with the python library scikit-learn.
*/
#define NB_OF_CLASSES 3
#define VECTOR_DIMENSION 2
#define SEMIHOSTING
const float32_t theta[NB_OF_CLASSES*VECTOR_DIMENSION] = {
1.4539529436590528f, 0.8722776016801852f,
-1.5267934452462473f, 0.903204577814203f,
-0.15338006360932258f, -2.9997913665803964f
};
const float32_t sigma[NB_OF_CLASSES*VECTOR_DIMENSION] = {
1.0063470889514925f, 0.9038018246524426f,
1.0224479953244736f, 0.7768764290432544f,
1.1217662403241206f, 1.2303890106020325f
};
const float32_t classPriors[NB_OF_CLASSES] = {
0.3333333333333333f, 0.3333333333333333f, 0.3333333333333333f
};
int32_t main(void)
{
/* Enable Vector */
#if (defined(__riscv_vector))
__RV_CSR_SET(CSR_MSTATUS, 0x200);
#endif
/* Array of input data */
float32_t in[2];
/* Result of the classifier */
float32_t result[NB_OF_CLASSES];
float32_t temp[NB_OF_CLASSES];
float32_t maxProba;
uint32_t index;
S.vectorDimension = VECTOR_DIMENSION;
S.numberOfClasses = NB_OF_CLASSES;
S.theta = theta;
S.sigma = sigma;
S.classPriors = classPriors;
S.epsilon=4.328939296523643e-09f;
in[0] = 1.5f;
in[1] = 1.0f;
index = riscv_gaussian_naive_bayes_predict_f32(&S, in, result,temp);
maxProba = result[index];
#if defined(SEMIHOSTING)
printf("Class = %d\n", index);
#endif
in[0] = -1.5f;
in[1] = 1.0f;
index = riscv_gaussian_naive_bayes_predict_f32(&S, in, result,temp);
maxProba = result[index];
#if defined(SEMIHOSTING)
printf("Class = %d\n", index);
#endif
in[0] = 0.0f;
in[1] = -3.0f;
index = riscv_gaussian_naive_bayes_predict_f32(&S, in, result,temp);
maxProba = result[index];
#if defined(SEMIHOSTING)
printf("Class = %d\n", index);
#endif
printf("Passed\n");
return 0;
}
float32_t
float float32_t
32-bit floating-point type definition.
Definition: riscv_math_types.h:257
riscv_gaussian_naive_bayes_instance_f32::classPriors
const float32_t * classPriors
Definition: bayes_functions.h:64
riscv_gaussian_naive_bayes_instance_f32::sigma
const float32_t * sigma
Definition: bayes_functions.h:63
riscv_gaussian_naive_bayes_instance_f32::theta
const float32_t * theta
Definition: bayes_functions.h:62
riscv_gaussian_naive_bayes_instance_f32::vectorDimension
uint32_t vectorDimension
Definition: bayes_functions.h:60
riscv_gaussian_naive_bayes_instance_f32::numberOfClasses
uint32_t numberOfClasses
Definition: bayes_functions.h:61
riscv_gaussian_naive_bayes_predict_f32
uint32_t riscv_gaussian_naive_bayes_predict_f32(const riscv_gaussian_naive_bayes_instance_f32 *S, const float32_t *in, float32_t *pOutputProbabilities, float32_t *pBufferB)
Naive Gaussian Bayesian Estimator.
Definition: riscv_gaussian_naive_bayes_predict_f32.c:56
riscv_gaussian_naive_bayes_instance_f32
Instance structure for Naive Gaussian Bayesian estimator.
Definition: bayes_functions.h:58
riscv_gaussian_naive_bayes_instance_f32::epsilon
float32_t epsilon
Definition: bayes_functions.h:65