-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdcl_mlp_layer.cpp
58 lines (45 loc) · 963 Bytes
/
fdcl_mlp_layer.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
#include "fdcl_mlp_layer.h"
fdcl_mlp_layer::fdcl_mlp_layer(int N_in, int N_out)
{
init_io(N_in,N_out);
z.resize(N_out);
theta.resize(2);
dJ_dtheta.resize(2);
theta[0].resize(N_out,N_in); // W
theta[1].resize(N_out,1); // b
dJ_dtheta[0].resize(N_out,N_in);
dJ_dtheta[1].resize(N_out,1);
theta[0].setRandom();
theta[1].setRandom();
}
VectorXd fdcl_mlp_layer::f(VectorXd x)
{
this->x=x;
z=theta[0]*x+theta[1];
for (int i=0; i<N_out ; i++)
y(i)=act_func(z(i));
return y;
}
void fdcl_mlp_layer::compute_dJ_dtheta(VectorXd e)
{
int i,j;
this->e=e;
for(i=0; i<N_out; i++)
{
dJ_dtheta[1](i)=d_act_func(z(i))*e(i);
for (j=0;j<N_in;j++)
{
dJ_dtheta[0](i,j)=dJ_dtheta[1](i)*x(j);
}
}
}
VectorXd fdcl_mlp_layer::back_prop(VectorXd e)
{
VectorXd e_prior, ds_e;
e_prior.resize(N_in);
ds_e.resize(N_out);
for(int i=0;i<N_out;i++)
ds_e(i)=d_act_func(z(i))*e(i);
e_prior=theta[0].transpose()*ds_e;
return e_prior;
}