-
Notifications
You must be signed in to change notification settings - Fork 4
/
ElementWiseAG.m
115 lines (93 loc) · 2.76 KB
/
ElementWiseAG.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
function [dw,db]=ElementWiseAG(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);
%------------------------------------------------------------
for j=1:NN.depth
M=Memory.On{j};
Z=Memory.Off{j};
if j~=1
M0=Memory.On{j-1};
else
M0=data;
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
Temp=M;
PerturbVector=(i*Step)*M0(m,:);
Temp(k,:)=LayerActive(Z(k,:)+PerturbVector);
PerturbCost=LocalCostFunction(Temp,label,j,NN);
dwRecord{j}(k,m)=imag(PerturbCost);
end
end
for k=1:NN.LayerStruct(2,j)
% Row
Temp=M;
PerturbVector=(i*Step);
if j~=NN.depth
Temp(k,:)=NN.active(Z(k,:)+PerturbVector);
else
Temp(k,:)=NN.OutActive(Z(k,:)+PerturbVector);
end
PerturbCost=LocalCostFunction(Temp,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.On=NN.bias;
Memory.Off=NN.bias;
for j=1:NN.depth-1
temp=NN.weight{j}*v+NN.bias{j};
Memory.Off{j}=temp;
v=NN.active(temp);
Memory.On{j}=v;
end
temp=NN.weight{NN.depth}*v+NN.bias{NN.depth};
Memory.Off{NN.depth}=temp;
Memory.On{NN.depth}=NN.OutActive(temp);
Function=Memory;
end
function Predict=AINN(A,LayerIndex,NN)
v=A;
if LayerIndex<=NN.depth-2
for j=LayerIndex+1:NN.depth-1
v=NN.active(NN.weight{j}*v+NN.bias{j});
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=v;
end
end
function FunctionOutput=LocalCostFunction(A,label,LayerIndex,NN)
Cost=NN.Cost;
temp=(label-AINN(A,LayerIndex,NN)).^2;
switch Cost
case 'SSE'
E=sum(temp,[1 2]);
case 'MSE'
E=mean(temp,[1 2]);
end
FunctionOutput=E;
end