-
Notifications
You must be signed in to change notification settings - Fork 4
/
ElementWiseRNAG.m
140 lines (118 loc) · 3.45 KB
/
ElementWiseRNAG.m
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
function [dw,db]=ElementWiseRNAG(data,label,NN)
% Numerical Mehtod Parameters Setting
% -----------------------------------------------------------
if strcmp(NN.InputAutoScaling,'on')==1
data=NN.InputScaleVector.*data-NN.InputCenterVector;
end
i=complex(0,1);
Step=1e-30; ReciprocalStep=1/Step;
dwRecord=NN.weight; dbRecord=NN.bias;
Memory=Nets(data,NN);
NN.ResMap{NN.depth}=0;
% -----------------------------------------------------------
for j=1:NN.depth
% A = A{k}
A=Memory.A{j};
S=Memory.S{j};
Z=Memory.Z{j};
%A0, Z0 are Previous States i.e. A{k-1}
if j==1
A0=data;
P=sparse(size(A,1),size(A,2));
elseif j==NN.depth
P=0;
A0=Memory.A{j-1};
else
A0=Memory.A{j-1};
Z0=Memory.Z{j-1};
P=NN.ResMap{j}*Z0;
end
if j~=NN.depth
LayerActive=@(x) NN.active(x);
else
LayerActive=@(x) NN.OutActive(x);
end
for k=1:NN.LayerStruct(2,j)
% Row
for m=1:NN.LayerStruct(1,j)
% Column
%Ap, Zp are perturbed Matrixs
Sp=S; Zp=Z;
PerturbVector=(i*Step)*A0(m,:);
Zp(k,:)=Z(k,:)+PerturbVector;
Sp(k,:)=LayerActive(Zp(k,:));
Ap=Sp+P;
PerturbCost=LocalCostFunction(Ap,Zp,label,j,NN);
dwRecord{j}(k,m)=imag(PerturbCost);
end
end
for k=1:NN.LayerStruct(2,j)
% Row
Sp=S; Zp=Z;
PerturbVector=(i*Step);
Zp(k,:)=Z(k,:)+PerturbVector;
Sp(k,:)=LayerActive(Zp(k,:));
Ap=Sp+P;
PerturbCost=LocalCostFunction(Ap,Zp,label,j,NN);
dbRecord{j}(k)=imag(PerturbCost);
end
dwRecord{j}=dwRecord{j}*ReciprocalStep;
dbRecord{j}=dbRecord{j}*ReciprocalStep;
end
dw=dwRecord;
db=dbRecord;
end
function Function=Nets(data,NN)
v=data;
Memory.A=NN.bias;
Memory.S=NN.bias;
Memory.Z=NN.bias;
for j=1:NN.depth-1
z=NN.weight{j}*v+NN.bias{j};
if j>1
Active=NN.active(z);
v=Active+NN.ResMap{j}*vp;
else
Active=NN.active(z);
v=Active;
end
vp=z;
Memory.A{j}=v;
Memory.S{j}=Active;
Memory.Z{j}=z;
end
z=NN.weight{NN.depth}*v+NN.bias{NN.depth};
Memory.A{NN.depth}=NN.OutActive(z);
Memory.S{NN.depth}=NN.OutActive(z);
Memory.Z{NN.depth}=z;
Function=Memory;
end
function Predict=ResINN(A,Z,LayerIndex,NN)
% The values of Layer k is known
% A = sigma(Zk), Z = Zk
v=A;
vp=Z;
if LayerIndex<=NN.depth-2
for j=LayerIndex+1:NN.depth-1
temp=NN.weight{j}*v+NN.bias{j}; % Z{k+1}
v=NN.active(temp)+NN.ResMap{j}*vp; % A{k+1}
vp=temp;
end
Predict=NN.OutActive(NN.weight{NN.depth}*v+NN.bias{NN.depth});
elseif LayerIndex==NN.depth-1
Predict=NN.OutActive(NN.weight{NN.depth}*v+NN.bias{NN.depth});
elseif LayerIndex==NN.depth
Predict=NN.OutActive(v);
end
end
function FunctionOutput=LocalCostFunction(A,Z,label,LayerIndex,NN)
Cost=NN.Cost;
temp=(label-ResINN(A,Z,LayerIndex,NN)).^2;
switch Cost
case 'SSE'
E=sum(temp,[1 2]);
case 'MSE'
E=mean(temp,[1 2]);
end
FunctionOutput=E;
end