-
Notifications
You must be signed in to change notification settings - Fork 0
/
fc.cpp
136 lines (119 loc) · 2.02 KB
/
fc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "param.h"
void reshape(f_32 in[1][1][120],f_32 out[120])
{
int m = 0;
for (int k = 0; k < 120; k++)
{
out[m] = in[0][0][k];
m++;
}
}
//void fc1(f_32 in[120],f_32 fc1_w[84][120], f_32 fc1_b[84],f_32 out[84])
//{
//
// for (int i = 0; i < 84; i++)
// {
// out[i] = 0;
// for (int set=0 ; set < 15; set++)
// {
// for (int j = 0; j < 8; j++)
// {
// out[i] += in[j + set * 15] * fc1_w[i][i + set * 15];
// }
// }
// }
//
// for (int i = 0; i < 84; i++)
// {
// out[i] += fc1_b[i];
// if (out[i]<0)
// {
// out[i] = 0;
// }
// }
//}
//
//
//void fc2(f_32 in[84], f_32 fc2_w[10][84], f_32 fc2_b[10], f_32 out[10])
//{
//
// for (int i = 0; i < 10; i++)
// {
// for (int set=0; set < 21; set++)
// {
// for (int j = 0; j < 4; j++)
// {
// out[i] += in[j + set * 21] * fc2_w[i][i + set * 21];
// }
// }
// }
//
// for (int i = 0; i < 84; i++)
// {
// out[i] += fc2_b[i];
// if (out[i] < 0)
// {
// out[i] = 0;
// }
// }
//}
void fc1(f_32 in[120], f_32 fc1_w[84][120], f_32 fc1_b[84], f_32 out[84])
{
for (int i = 0; i < 84; i++)
{
out[i] = 0; // 将 out[i] 初始化为0
for (int j = 0; j < 120; j++)
{
out[i] += in[j] * fc1_w[i][j]; // 累加和计算
}
}
for (int i = 0; i < 84; i++)
{
out[i] += fc1_b[i];
if (out[i] < 0)
{
out[i] = 0;
}
}
}
void fc2(f_32 in[84], f_32 fc2_w[10][84], f_32 fc2_b[10], f_32 out[10])
{
for (int i = 0; i < 10; i++)
{
out[i] = 0; // 将 out[i] 初始化为0
for (int j = 0; j < 84; j++)
{
out[i] += in[j] * fc2_w[i][j]; // 累加和计算
}
}
for (int i = 0; i < 10; i++)
{
out[i] += fc2_b[i];
if (out[i] < 0)
{
out[i] = 0;
}
}
}
void softmax(f_32 in[10], f_32 out[10])
{
f_32 max_val = in[0];
f_32 sum = 0.0;
// 找到in中的最大值
for (int i = 1; i < 10; i++)
{
if (in[i] > max_val)
max_val = in[i];
}
// 计算softmax值,并累加求和
for (int i = 0; i < 10; i++)
{
out[i] = expf(in[i] - max_val);
sum += out[i];
}
// 归一化处理,得到最终的softmax输出
for (int i = 0; i < 10; i++)
{
out[i] /= sum;
}
}