From f7ce7b8f4efe6a8e72f71156f9c97aad4b18eebd Mon Sep 17 00:00:00 2001 From: Qiea <1310371422@qq.com> Date: Tue, 12 Nov 2024 12:03:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.calculate_probabilities计算概率 2.calculate_layer获取通道数 --- cnn.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 14 deletions(-) diff --git a/cnn.c b/cnn.c index 318fb5c..95e3a57 100644 --- a/cnn.c +++ b/cnn.c @@ -170,10 +170,6 @@ float* output(const float* input_matrix){ float *affine2_rslt = (float *) malloc(sizeof(float)*7); memset(affine2_rslt, 0, sizeof(float)*7); -// 比较输出层的最大值 - float temp = -100; // 用于存储最大值的临时变量,初始化为一个非常小的值 - int predict_num; // 用于存储预测的数字(对应最大值的索引) - // 遍历10个输出神经元(假设有10个类别) 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_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; } @@ -266,6 +252,64 @@ float* generateMatrix(Model model, const float* value) 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