添加功能
1.calculate_probabilities计算概率 2.calculate_layer获取通道数
This commit is contained in:
75
cnn.c
75
cnn.c
@@ -170,10 +170,6 @@ float* output(const float* input_matrix){
|
|||||||
float *affine2_rslt = (float *) malloc(sizeof(float)*7);
|
float *affine2_rslt = (float *) malloc(sizeof(float)*7);
|
||||||
memset(affine2_rslt, 0, sizeof(float)*7);
|
memset(affine2_rslt, 0, sizeof(float)*7);
|
||||||
|
|
||||||
// 比较输出层的最大值
|
|
||||||
float temp = -100; // 用于存储最大值的临时变量,初始化为一个非常小的值
|
|
||||||
int predict_num; // 用于存储预测的数字(对应最大值的索引)
|
|
||||||
|
|
||||||
// 遍历10个输出神经元(假设有10个类别)
|
// 遍历10个输出神经元(假设有10个类别)
|
||||||
for(int n=0; n<7; n++)
|
for(int n=0; n<7; n++)
|
||||||
{
|
{
|
||||||
@@ -188,18 +184,8 @@ float* output(const float* input_matrix){
|
|||||||
// 加上对应神经元的偏置
|
// 加上对应神经元的偏置
|
||||||
affine2_temp = affine2_temp + fc2_weight.array[n];
|
affine2_temp = affine2_temp + fc2_weight.array[n];
|
||||||
affine2_rslt[n] = affine2_temp; // 存储输出层的结果
|
affine2_rslt[n] = affine2_temp; // 存储输出层的结果
|
||||||
|
|
||||||
// 寻找最大值
|
|
||||||
if(temp <= affine2_rslt[n])
|
|
||||||
{
|
|
||||||
temp = affine2_rslt[n]; // 更新最大值
|
|
||||||
predict_num = n; // 记录最大值对应的类别索引
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
print_rslt(affine2_rslt,7,7);
|
|
||||||
printf("Label is:%d\r\n",predict_num+1);
|
|
||||||
|
|
||||||
return affine2_rslt;
|
return affine2_rslt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,6 +252,64 @@ float* generateMatrix(Model model, const float* value)
|
|||||||
return CNN_data;
|
return CNN_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float calculate_probabilities(float *input_array)
|
||||||
|
{
|
||||||
|
float sum = 0;
|
||||||
|
u8 input_num = 7;
|
||||||
|
float *result = (float *) malloc(sizeof(float)*input_num);
|
||||||
|
memset(result, 0, sizeof(float)*input_num);
|
||||||
|
|
||||||
|
float *temp = (float *) malloc(sizeof(float)*input_num);
|
||||||
|
memset(temp, 0, sizeof(float)*input_num);
|
||||||
|
|
||||||
|
for (int i = 0; i < input_num; i++)
|
||||||
|
{
|
||||||
|
temp[i] = expf(input_array[i]);
|
||||||
|
sum = sum + temp[i];
|
||||||
|
}
|
||||||
|
for (int j = 0; j < input_num; j++)
|
||||||
|
{
|
||||||
|
result[j] = temp[j] / sum;
|
||||||
|
if(isnan(result[j]))result[j] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int max_index = 0;
|
||||||
|
float max_value = result[0];
|
||||||
|
for (int k = 1; k < input_num; k++)
|
||||||
|
{
|
||||||
|
if (result[k] > max_value)
|
||||||
|
{
|
||||||
|
max_value = result[k];
|
||||||
|
max_index = k;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float _tmp = result[max_index] * 100;
|
||||||
|
free(temp);
|
||||||
|
temp = NULL;
|
||||||
|
free(result);
|
||||||
|
result = NULL;
|
||||||
|
return _tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
u8 calculate_layer(float *input_array){
|
||||||
|
u8 input_num = 7;
|
||||||
|
u8 predict_num = 0;
|
||||||
|
float max_temp = -100;
|
||||||
|
for(int n=0; n<input_num; n++)
|
||||||
|
{
|
||||||
|
if(max_temp <= input_array[n])
|
||||||
|
{
|
||||||
|
max_temp = input_array[n]; // 更新最大值
|
||||||
|
predict_num = n; // 记录最大值对应的类别索引
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//print_rslt(input_array,7,7);
|
||||||
|
return predict_num+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void cnn_run(){
|
void cnn_run(){
|
||||||
float value[3] = {0};
|
float value[3] = {0};
|
||||||
calculate_statistics(data,&value[0]);
|
calculate_statistics(data,&value[0]);
|
||||||
@@ -311,6 +355,9 @@ void cnn_run(){
|
|||||||
float* affine1_rslt = hidden(pool_rslt_3);
|
float* affine1_rslt = hidden(pool_rslt_3);
|
||||||
float* affine2_rslt = output(affine1_rslt);
|
float* affine2_rslt = output(affine1_rslt);
|
||||||
|
|
||||||
|
printf("概率:%f\r\n",calculate_probabilities(affine2_rslt));
|
||||||
|
printf("Label is:%d\r\n",calculate_layer(affine2_rslt));
|
||||||
|
|
||||||
free(pool_rslt_3);
|
free(pool_rslt_3);
|
||||||
pool_rslt_3 = NULL;
|
pool_rslt_3 = NULL;
|
||||||
free(affine1_rslt);
|
free(affine1_rslt);
|
||||||
|
|||||||
Reference in New Issue
Block a user