-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWeightedLayer.m
167 lines (125 loc) · 5.21 KB
/
WeightedLayer.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
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
classdef WeightedLayer < Layer
%WEIGHTEDLAYER Summary of this class goes here
% Detailed explanation goes here
properties
usebias
activation
kernelinitializer
A
dA
Z
dZ
W
dW
b
db
vdW
vdb
sdW
sdb
options
end
methods(Abstract)
end
methods
function init(layer, prevlayer)
if ~isa(prevlayer, 'Layer')
throw(MException('layer:noPreviousLayer', ...
'A previous layer must be defined for a layer layer'));
end
layer.b = zeros(layer.units, 1);
if strcmp(layer.kernelinitializer, 'uniform')
layer.W = -1 + rand(layer.units, prevlayer.units) * 2;
elseif strcmp(layer.kernelinitializer, 'he')
layer.W = randn(layer.units, prevlayer.units) * sqrt(2 / prevlayer.units);
elseif strcmp(layer.kernelinitializer, 'random')
% layer.W = -1 + rand(layer.units, prevlayer.units) * 2;
epsilon_init = 0.12;
layer.W = rand(layer.units, prevlayer.units) * 2 * epsilon_init - epsilon_init;
else
throw(MException('layer:unknownInitializer', ...
'Unknown initializer %s', layer.initializer));
end
% Default for Adam
layer.vdW = 0;
layer.vdb = 0;
layer.sdW = 0;
layer.sdb = 0;
layer.prevlayer = prevlayer;
end
function y = forward(layer, X, cache)
Zlocal = layer.W * X;
if layer.usebias
Zlocal = Zlocal + layer.b;
end
if strcmp(layer.activation, 'sigmoid')
Alocal = sigmoid(Zlocal);
elseif strcmp(layer.activation, 'relu')
Alocal = relu(Zlocal);
elseif strcmp(layer.activation, 'softmax')
Alocal = softmax(Zlocal);
elseif strcmp(layer.activation, 'tanh')
Alocal = tanh(Zlocal);
elseif strcmp(layer.activation, 'linear')
Alocal = Zlocal;
elseif strcmp(layer.activation, 'leeoscillator')
Alocal = leeoscillator(Zlocal);
else
throw(MException('layer:unknownActivation', ...
'Unknown activation function %s', layer.activation));
end
if cache
layer.Z = Zlocal;
layer.A = Alocal;
end
y = Alocal;
end
function updateadam(layer, learningrate, beta1, beta2, epsilon, t)
% Update the weights of the weighted layer
% learningrate: the learning rate for each gradient descent
% beta1: hyperparameter for Momentum, recommend: 0.9
% beta2: hyperparameter for RMSProp, recommend: 0.999
% epsilon: recommend: 0.00000001
% t: current iteration
% Momentum
layer.vdW = beta1 * layer.vdW + (1-beta1) * layer.dW;
layer.vdb = beta1 * layer.vdb + (1-beta1) * layer.db;
% RMSProp
layer.sdW = beta2 * layer.sdW + (1-beta2) * (layer.dW .^ 2);
layer.sdb = beta2 * layer.sdb + (1-beta2) * (layer.db .^ 2);
% Bias correction
vdWcorrected = layer.vdW / (1-(beta1^t));
vdbcorrected = layer.vdb / (1-(beta1^t));
sdWcorrected = layer.sdW / (1-(beta2^t));
sdbcorrected = layer.sdb / (1-(beta2^t));
layer.W = layer.W - learningrate * ...
(vdWcorrected ./ (sqrt(sdWcorrected) + epsilon));
layer.b = layer.b - learningrate * ...
(vdbcorrected ./ (sqrt(sdbcorrected) + epsilon));
if isa(layer.prevlayer, 'WeightedLayer')
layer.prevlayer.updateadam(...
learningrate, beta1, beta2, epsilon, t);
end
end
function update(layer, learningrate)
layer.W = layer.W - learningrate * layer.dW;
layer.b = layer.b - learningrate * layer.db;
if isa(layer.prevlayer, 'WeightedLayer')
layer.prevlayer.update(learningrate);
end
end
function newlayer = move(layer, newlayer)
newlayer.usebias = layer.usebias;
newlayer.activation = layer.activation;
newlayer.kernelinitializer = layer.kernelinitializer;
newlayer.A = layer.A;
newlayer.dA = layer.dA;
newlayer.Z = layer.Z;
newlayer.dZ = layer.dZ;
newlayer.W = layer.W;
newlayer.dW = layer.dW;
newlayer.b = layer.b;
newlayer.db = layer.db;
end
end
end