-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.cpp
201 lines (180 loc) · 3.38 KB
/
functions.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//Desc: Functions.cpp
#include "platform.h"
#include "functions.h"
double identity_function(double x)
{
return x;
}
double step_function(double x)
{
if(x>0)
return 1;
return 0;
}
double sigmoid(double x)
{
return 1.0/(1.0+exp(-x));
}
double sigmoid_grad(double x)
{
double s=sigmoid(x);
return (1.0-s)*s;
}
double relu(double x)
{
if(x>0)return x;
return 0.0;
}
//FIXME!!!!!!!!!!!!!!!!!!!!!!!!!!
double relu_grad(double x)
{
return relu(x);
}
double expfunc(double x)
{
return exp(x);
}
MatrixXd identityFunction(MatrixXd x)
{
return x;
}
MatrixXd stepFunction(MatrixXd x)
{
MatrixXd m=x;
m.unaryExpr(&step_function);
return m;
}
MatrixXd Sigmoid(MatrixXd x)
{
MatrixXd m=x;
m.unaryExpr(&sigmoid);
return m;
}
MatrixXd Sigmoid_Grad(MatrixXd x)
{
MatrixXd m=x;
m.unaryExpr(&sigmoid_grad);
return m;
}
MatrixXd Relu(MatrixXd x)
{
MatrixXd m=x;
m.unaryExpr(&relu);
return m;
}
/*
MatrixXd Relu_grad()
{
MatrixXd m=MatrixXd::Zero();
m.unaryExpr(&relu);
return m;
}
*/
MatrixXd Softmax(MatrixXd x)
{
MatrixXd y=x.array()-x.maxCoeff();
y.unaryExpr(&expfunc);
double s=y.sum();
return y/s;
}
//data set ±¸Á¶
//Áõ»óid=72row
//Áúº´id=29
float dataset[SYMPTOM_NUM][DISEASE_NUM];
void LoadDataSet()
{
CsvParser *csvparser = CsvParser_new("symdis.csv", ",", 0);
CsvRow *row;
int i,r=0;
while ((row = CsvParser_getRow(csvparser)) ) {
const char **rowFields = CsvParser_getFields(row);
for (i = 0 ; i < CsvParser_getNumFields(row) ; i++) {
int data=strtoul(rowFields[i],NULL,10);
printf("%d",data);//°;¡ ±× char °ª
dataset[r][i]=data;
}
printf("\n");
r++;
CsvParser_destroy_row(row);
}
CsvParser_destroy(csvparser);
}
#include "TwoLayerNet.h"
TwoLayerNet net(SYMPTOM_NUM,HIDDEN_NUM,DISEASE_NUM);
#define learning_rate 0.01
void Train_sub(int i)//i¹ø° Áúº´À» ÇнÀ½ÃŲ´Ù.
{
MatrixXd x(1,SYMPTOM_NUM);
for(int it=0;it<SYMPTOM_NUM;++it)
{
x(0,it)=dataset[it][i];
}
MatrixXd t=MatrixXd::Zero(1,DISEASE_NUM);
t(0,i)=1;
net.CalcGrad(x,t);
MatrixXd gradW1=net.getGradW1();
MatrixXd gradW2=net.getGradW2();
MatrixXd gradb1=net.getGradb1();
MatrixXd gradb2=net.getGradb2();
net.W1-=(learning_rate*gradW1);
net.W2-=(learning_rate*gradW2);
net.b1-=(learning_rate*gradb1);
net.b2-=(learning_rate*gradb2);
}
void Train()
{
for(int j=0;j<200;++j)
{
for(int i=0;i<DISEASE_NUM;++i)
{
Train_sub(i);
}
}
}
void WriteWeights()
{
int row,col;
FILE *fp=fopen("Weights.txt","w");
float data;
row=SYMPTOM_NUM,col=HIDDEN_NUM;
fprintf(fp,"%d %d\n",row,col);
for(int i= 0; i<row;++i)
{
for(int j=0;j<col;++j)
{
data=net.W1(i,j);
fprintf(fp,"%lf\n",data);
}
}
row=HIDDEN_NUM,col=DISEASE_NUM;
fprintf(fp,"%d %d\n",row,col);
for(int i= 0; i<row;++i)
{
for(int j=0;j<col;++j)
{
data=net.W2(i,j);
fprintf(fp,"%lf\n",data);
}
}
row=1,col=HIDDEN_NUM;
fprintf(fp,"%d %d\n",row,col);
for(int i= 0; i<row;++i)
{
for(int j=0;j<col;++j)
{
data=net.b1(i,j);
fprintf(fp,"%lf\n",data);
}
}
row=1,col=DISEASE_NUM;
fprintf(fp,"%d %d\n",row,col);
for(int i= 0; i<row;++i)
{
for(int j=0;j<col;++j)
{
data=net.b2(i,j);
fprintf(fp,"%lf\n",data);
}
}
fclose(fp);
}