-
Notifications
You must be signed in to change notification settings - Fork 0
/
propagation.cpp
276 lines (235 loc) · 10.1 KB
/
propagation.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "propagation.h"
//------------------------------------------------------------------ Propagation directe ----------------------------------------------------------------------------------------
void fforward(int const& L, int const& P, std::vector<int> const& nbNeurons, std::vector<std::string> const& activations,
std::vector<Eigen::MatrixXd>& weights, std::vector<Eigen::VectorXd>& bias, std::vector<Eigen::MatrixXd>& As, std::vector<Eigen::MatrixXd>& slopes)
{
int l;
for (l=0;l<L;l++)
{
As[l+1] = weights[l]*As[l];
As[l+1].colwise() += bias[l];
activation(activations[l], As[l+1], slopes[l]);
}
}
double risk(Eigen::MatrixXd const& Y, int const& P, Eigen::MatrixXd const& output_network, std::string const& type_perte, bool const normalized)
{
double cost=0;
for(int p=0; p<P; p++)
{
cost+=L(Y.col(p),output_network.col(p),type_perte);
}
if(normalized){cost/=(double)P;}
return cost;
}
//------------------------------------------------------------------ Rétropropagation ---------------------------------------------------------------------------------------------
void backward(Eigen::MatrixXd const& Y, int const& L, int const& P, std::vector<int> const& nbNeurons, std::vector<std::string> const& activations,
std::vector<int> const& globalIndices, std::vector<Eigen::MatrixXd>& weights, std::vector<Eigen::VectorXd>& bias,
std::vector<Eigen::MatrixXd>& As, std::vector<Eigen::MatrixXd>& slopes, Eigen::VectorXd& gradient, std::string const& type_perte, bool const normalized)
{
int l,p,n,nL=nbNeurons[L],jump;
int N=globalIndices[2*L-1];
Eigen::MatrixXd L_derivative(nL,P);
Eigen::VectorXd LP(nL);
Eigen::MatrixXd dzL(nL,P);
Eigen::MatrixXd dz;
Eigen::MatrixXd dw;
Eigen::VectorXd db;
//#pragma omp parallel for private(LP)
for(p=0; p<P; p++)
{
FO_L(Y.col(p),As[L].col(p),LP,type_perte);
L_derivative.col(p)=LP;
}
//#pragma omp barrier
if(activations[L-1]=="softmax")
{
dzL.setZero();
Eigen::MatrixXd ZL(nL,P);
ZL = slopes[L-1];
for(int r=0; r<nL; r++)
{
activation(activations[L-1],ZL,slopes[L-1],r);
dzL.row(r) = (L_derivative.cwiseProduct(slopes[L-1])).colwise().sum();
}
}
else{dzL = L_derivative.cwiseProduct(slopes[L-1]);}
dz=dzL;
jump=nbNeurons[L]*nbNeurons[L-1];
dw = dz*(As[L-1].transpose());
db = dz.rowwise().sum();
dw.resize(jump,1);
gradient.segment(globalIndices[2*(L-1)]-jump,jump)=dw;
jump=nbNeurons[L];
gradient.segment(globalIndices[2*(L-1)+1]-jump,jump)=db;
for (l=L-1;l>0;l--)
{
dz=(weights[l].transpose()*dz).cwiseProduct(slopes[l-1]);
jump=nbNeurons[l]*nbNeurons[l-1];
dw=dz*(As[l-1].transpose());
db = dz.rowwise().sum();
dw.resize(jump,1);
gradient.segment(globalIndices[2*(l-1)]-jump,jump)=dw;
jump=nbNeurons[l];
gradient.segment(globalIndices[2*(l-1)+1]-jump,jump)=db;
}
if(normalized){gradient/=(double)P;}
}
//Cas où L'' diagonale (la plupart du temps)
void QSO_backward(Eigen::MatrixXd const& Y, int const& L, int const& P, std::vector<int> const& nbNeurons, std::vector<std::string> const& activations,
std::vector<int> const& globalIndices, std::vector<Eigen::MatrixXd>& weights, std::vector<Eigen::VectorXd>& bias, std::vector<Eigen::MatrixXd>& As, std::vector<Eigen::MatrixXd>& slopes,
Eigen::VectorXd& gradient, Eigen::MatrixXd& Q, std::string const& type_perte)
{
int l,m,p,n,nL=nbNeurons[L],jump;
int N=globalIndices[2*L-1];
Eigen::VectorXd LP(nL);
Eigen::MatrixXd LPP(nL,nL);
Eigen::VectorXd dzL(nL);
Eigen::VectorXd dz;
Eigen::MatrixXd dw;
Eigen::VectorXd Jpm(N);
Eigen::MatrixXd ZL(nL,P);
if(activations[L-1]=="softmax")
{
ZL = slopes[L-1];
}
gradient.setZero(); Q.setZero();
for (p=0;p<P;p++)
{
SO_L(Y.col(p),As[L].col(p),LP,LPP,type_perte);
for (m=0;m<nL;m++)
{
if(activations[L-1]=="softmax")
{
for (n=0;n<nL;n++)
{
if(m==n){dzL(n) = -As[L](m,p)*(1-As[L](m,p));}
else{dzL(n) = std::exp(ZL(n,p)-ZL(m,p))*std::pow(As[L](m,p),2);}
}
}
else
{
for (n=0;n<nL;n++)
{
dzL(n) = (n==m) ? -slopes[L-1](m,p) : 0;
}
}
dz=dzL;
jump=nbNeurons[L]*nbNeurons[L-1];
dw=dz*(As[L-1].col(p).transpose());
dw.resize(jump,1);
Jpm.segment(globalIndices[2*(L-1)]-jump,jump)=dw;
jump=nbNeurons[L];
Jpm.segment(globalIndices[2*(L-1)+1]-jump,jump)=dz;
for (l=L-1;l>0;l--)
{
dz=(weights[l].transpose()*dz).cwiseProduct(slopes[l-1].col(p));
jump=nbNeurons[l]*nbNeurons[l-1];
dw=dz*(As[l-1].col(p).transpose());
dw.resize(jump,1);
Jpm.segment(globalIndices[2*(l-1)]-jump,jump)=dw;
jump=nbNeurons[l];
Jpm.segment(globalIndices[2*(l-1)+1]-jump,jump)=dz;
}
Q+=LPP(m,m)*Jpm*Jpm.transpose();
gradient+=-LP(m)*Jpm;
}
}
Q/=(double)P;
gradient/=(double)P;
}
void QSO_backwardJacob(int const& L, int const& P, std::vector<int> const& nbNeurons, std::vector<std::string> const& activations, std::vector<int> const& globalIndices,
std::vector<Eigen::MatrixXd>& weights, std::vector<Eigen::VectorXd>& bias, std::vector<Eigen::MatrixXd>& As, std::vector<Eigen::MatrixXd>& slopes, Eigen::MatrixXd& J)
{
int l,m,p,n,nL=nbNeurons[L],jump,nbLine=0;
int N=globalIndices[2*L-1];
Eigen::VectorXd dzL(nL);
Eigen::VectorXd dz;
Eigen::MatrixXd dw;
Eigen::VectorXd Jpm(N);
Eigen::MatrixXd ZL(nL,P);
if(activations[L-1]=="softmax")
{
ZL = slopes[L-1];
}
for (p=0;p<P;p++)
{
for (m=0;m<nL;m++)
{
if(activations[L-1]=="softmax")
{
for (n=0;n<nL;n++)
{
if(m==n){dzL(n) = -As[L](m,p)*(1-As[L](m,p));}
else{dzL(n) = std::exp(ZL(n,p)-ZL(m,p))*std::pow(As[L](m,p),2);}
}
}
else
{
for (n=0;n<nL;n++)
{
dzL(n) = (n==m) ? -slopes[L-1](m,p) : 0;
}
}
dz=dzL;
jump=nbNeurons[L]*nbNeurons[L-1];
dw=dz*(As[L-1].col(p).transpose());
dw.resize(jump,1);
Jpm.segment(globalIndices[2*(L-1)]-jump,jump)=dw;
jump=nbNeurons[L];
Jpm.segment(globalIndices[2*(L-1)+1]-jump,jump)=dz;
for (l=L-1;l>0;l--)
{
dz=(weights[l].transpose()*dz).cwiseProduct(slopes[l-1].col(p));
jump=nbNeurons[l]*nbNeurons[l-1];
dw=dz*(As[l-1].col(p).transpose());
dw.resize(jump,1);
Jpm.segment(globalIndices[2*(l-1)]-jump,jump)=dw;
jump=nbNeurons[l];
Jpm.segment(globalIndices[2*(l-1)+1]-jump,jump)=dz;
}
J.row(nbLine) = Jpm;
nbLine++;
}
}
}
void solve(Eigen::VectorXd const& gradient, Eigen::MatrixXd const& hessian, Eigen::VectorXd& delta, std::string const method)
{
if(method=="LLT"){Eigen::LLT<Eigen::MatrixXd> solver; solver.compute(hessian); delta = solver.solve(-gradient);}
else if(method=="LDLT"){Eigen::LDLT<Eigen::MatrixXd> solver; solver.compute(hessian); delta = solver.solve(-gradient); }
else if(method=="HouseholderQR"){Eigen::HouseholderQR<Eigen::MatrixXd> solver; solver.compute(hessian); delta = solver.solve(-gradient);}
else if(method=="ColPivHouseholderQR"){Eigen::ColPivHouseholderQR<Eigen::MatrixXd> solver; solver.compute(hessian); delta = solver.solve(-gradient);}
else if(method=="FullPivHouseholderQR"){Eigen::FullPivHouseholderQR<Eigen::MatrixXd> solver; solver.compute(hessian); delta = solver.solve(-gradient);}
else if(method=="PartialPivLU"){Eigen::PartialPivLU<Eigen::MatrixXd> solver; solver.compute(hessian); delta = solver.solve(-gradient);}
else if(method=="FullPivLU"){Eigen::FullPivLU<Eigen::MatrixXd> solver; solver.compute(hessian); delta = solver.solve(-gradient);}
else if(method=="ConjugateGradient"){Eigen::ConjugateGradient<Eigen::MatrixXd> solver; solver.compute(hessian); delta = solver.solve(-gradient);}
else if(method=="LeastSquaresConjugateGradient"){Eigen::LeastSquaresConjugateGradient<Eigen::MatrixXd> solver; solver.compute(hessian); delta = solver.solve(-gradient);}
else if(method=="BiCGSTAB"){Eigen::BiCGSTAB<Eigen::MatrixXd> solver; solver.compute(hessian); delta = solver.solve(-gradient);}
}
void update(int const& L, std::vector<int> const& nbNeurons, std::vector<int> const& globalIndices, std::vector<Eigen::MatrixXd>& weights, std::vector<Eigen::VectorXd>& bias,
Eigen::VectorXd const& delta)
{
int l, jump;
for (l=0;l<L;l++)
{
jump=nbNeurons[l]*nbNeurons[l+1];
weights[l].resize(jump,1);
weights[l] += delta.segment(globalIndices[2*l]-jump,jump);
weights[l].resize(nbNeurons[l+1],nbNeurons[l]);
jump=nbNeurons[l+1];
bias[l] += delta.segment(globalIndices[2*l+1]-jump,jump);
}
}
void updateNesterov(int const& L, std::vector<int> const& nbNeurons, std::vector<int> const& globalIndices, std::vector<Eigen::MatrixXd>& weights, std::vector<Eigen::VectorXd>& bias,
std::vector<Eigen::MatrixXd>& weights2, std::vector<Eigen::VectorXd>& bias2, Eigen::VectorXd const& delta, double const& lambda1, double const& lambda2)
{
int l, jump;
for (l=0;l<L;l++)
{
jump=nbNeurons[l]*nbNeurons[l+1];
weights[l].resize(jump,1); weights2[l].resize(jump,1);
weights[l] = lambda1*weights2[l] + lambda2*delta.segment(globalIndices[2*l]-jump,jump);
weights[l].resize(nbNeurons[l+1],nbNeurons[l]); weights2[l].resize(nbNeurons[l+1],nbNeurons[l]);
jump=nbNeurons[l+1];
bias[l] = lambda1*bias2[l] + lambda2*delta.segment(globalIndices[2*l+1]-jump,jump);
}
}