-
Notifications
You must be signed in to change notification settings - Fork 0
/
widrow_hoff_experiment.py
343 lines (286 loc) · 11.6 KB
/
widrow_hoff_experiment.py
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import os
from os.path import join as pjoin
import numpy as np
from pdb import set_trace
import math
import scipy
import matplotlib.pyplot as plt
import random
from utils import eval_activation_func, eval_activation_func_gradient
import warnings
warnings.filterwarnings('ignore')
np.random.seed(0)
random.seed(72)
class NeuralNetwork:
def __init__(self):
pass
class Perceptron(NeuralNetwork):
""" Perceptron. Here for the Widrow-Hoff experiment we evaluate the loss
after updating the gradient at every step.
Attributes
------------
in_dim: Integer.
The input dimension.
out_dim: Integer.
The output dimension.
activation_func: String.
The name of the activation function.
learning_rate: Float.
The learning rate. Default is 0.03.
weight: Numpy array.
The weight, of size (out_dim, in_dim).
bias: Numpy array.
The bias, of size (out_dim).
weight_grad: Numpy array.
The weight, of size (out_dim, in_dim).
bias_grad: Numpy array.
The bias, of size (out_dim).
leaky_alpha: Float.
The coefficient if LeakyRelu or ELU is used. Default is 0.1.
Methods
-----------
eval(x_in)
Evaluate the result given the input x_in.
train(X_train, y_train, n_epoch)
Train the network
"""
def __init__(self, in_dim, out_dim, activation_func="linear", learning_rate=0.03, leaky_alpha=0.1):
"""
Parameters
----------
in_dim : Integer.
The input dimension.
out_dim : Integer.
The output dimension.
activation_func : String.
The name of the activation function.
learning_rate: Float.
The learning rate. Default is 0.03.
leaky_alpha: Float.
The coefficient if LeakyRelu or ELU is used. Default is 0.1.
"""
self.in_dim = in_dim
self.out_dim = out_dim
self.activation_func = activation_func
self.learning_rate = learning_rate
self.leaky_alpha = leaky_alpha
# self.weight = np.random.normal(size=(out_dim, in_dim))
# self.bias = np.random.normal(size=(out_dim))
self.weight = np.zeros((out_dim, in_dim))
self.bias = np.zeros((out_dim))
self.weight_grad = np.zeros((out_dim, in_dim))
self.bias_grad = np.zeros((out_dim))
def eval(self, x_in):
""" Evaluate the result given the input x_in.
Parameters
---------------
x_in : Numpy array.
Input, of size (n_features) where n_features is the number of features.
Returns
----------
out: Numpy array.
The output.
"""
out_mat_1 = np.dot(self.weight, x_in)
out_mat = out_mat_1.squeeze() + self.bias.squeeze()
if (self.activation_func == "LeakyRelu") or (self.activation_func == "elu"):
out = eval_activation_func(out_mat, self.activation_func, leaky_alpha=self.leaky_alpha)
else:
out = eval_activation_func(out_mat, self.activation_func)
return out
def train(self, X_train, y_train, n_epoch):
""" Train the network.
Parameters
---------------
X_train: Numpy array.
The training data, of size (n_inst, n_features) where
n_inst is the number of instances, and n_features is
the number of features.
y_train: Numpy array.
The ground truth, of size (n_inst).
n_epoch: Integer.
The number of epochs
Returns
----------
loss_list: List of float.
The loss values at each epoch.
"""
print("Begin training")
n_inst = X_train.shape[0]
loss_list = []
for epoch_idx in range(1, n_epoch+1):
# Train
loss = 0
random_idx_list = [idx for idx in range(n_inst)]
random.shuffle(random_idx_list)
for inst_idx in range(n_inst):
x_inst = X_train[random_idx_list[inst_idx], :]
y_inst = y_train[random_idx_list[inst_idx]]
if np.size(y_inst) == 1:
y_inst = y_inst[:, np.newaxis]
# Forward pass
out = self.eval(x_inst)
err = y_inst - out
err = np.squeeze(err)
# Update
if (self.activation_func == "LeakyRelu") or (self.activation_func == "elu"):
self.weight += 2*self.learning_rate*err*eval_activation_func_gradient(out, self.activation_func, leaky_alpha=self.leaky_alpha)*x_inst
self.bias += 2*self.learning_rate*err*eval_activation_func_gradient(out, self.activation_func, leaky_alpha=self.leaky_alpha)
else:
self.weight += 2*self.learning_rate*err*eval_activation_func_gradient(out, self.activation_func)*x_inst
self.bias += 2*self.learning_rate*err*eval_activation_func_gradient(out, self.activation_func)
# Calculate sum of square loss
loss = 0
for idx2 in range(n_inst):
x_inst2 = X_train[idx2, :]
y_inst2 = y_train[idx2]
if np.size(y_inst2) == 1:
y_inst2 = y_inst2[:, np.newaxis]
out2 = self.eval(x_inst2)
err2 = y_inst2 - out2
err2 = np.squeeze(err2)
loss += np.sum(err2**2) # MSE
loss_list.append(loss)
return loss_list
def get_experiment_data():
""" Get the experimental data of the Widrow-Hoff experiment, based on descriptions in [1] (exercise P10.9).
[1] M. Hagan et al., Neural network design (2nd ed.), 2014, .
Parameters
---------------
None.
Returns
----------
X_train: Numpy array.
The training data, of size (n_inst, n_features) where
n_inst is the number of instances, and n_features is
the number of features.
y_train: Numpy array.
The ground truth, of size (n_inst).
"""
X_train = np.array([
[1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1 ],
[1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, -1, -1],
[1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1],
[-1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1],
[-1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, -1],
])
y_train = np.array([60, 0, -60, 60, 0, -60])
y_train = y_train[:, np.newaxis]
return (X_train, y_train)
def perform_widrow_hoff_experiment():
""" Perform the Widrow-Hoff experiment, based on descriptions in [1] (exercise P10.9).
[1] M. Hagan et al., Neural network design (2nd ed.), 2014, .
Parameters
---------------
None.
Returns
----------
X_train: Numpy array.
The training data, of size (n_inst, n_features) where
n_inst is the number of instances, and n_features is
the number of features.
y_train: Numpy array.
The ground truth, of size (n_inst).
"""
(X_train, y_train) = get_experiment_data()
(n_inst, n_features) = X_train.shape
net = Perceptron(n_features, 1, "linear")
# net = Perceptron(n_features, 1, "linear", learning_rate=0.09)
n_epoch = 20
loss_list = net.train(X_train, y_train, n_epoch)
loss_list = loss_list[:100]
plt.plot(range(1, len(loss_list)+1), loss_list, color="blue", label="Sum of square error")
plt.legend()
plt.ylim((0, 2.5e+04))
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.xlabel("Time step")
plt.title("Widrow-Hoff experiment")
plt.show()
def perform_widrow_hoff_experiment_leaky_relu():
""" Perform the Widrow-Hoff experiment, based on descriptions in [1] (exercise P10.9).
But this time we use Leaky ReLU (alpha parameter set to 0.1, I tried 0.01 but it did not work).
[1] M. Hagan et al., Neural network design (2nd ed.), 2014.
Parameters
---------------
None.
Returns
----------
X_train: Numpy array.
The training data, of size (n_inst, n_features) where
n_inst is the number of instances, and n_features is
the number of features.
y_train: Numpy array.
The ground truth, of size (n_inst).
"""
(X_train, y_train) = get_experiment_data()
(n_inst, n_features) = X_train.shape
# net = Perceptron(n_features, 1, "linear")
net = Perceptron(n_features, 1, "LeakyRelu", learning_rate=0.03, leaky_alpha=0.1)
n_epoch = 2000
loss_list = net.train(X_train, y_train, n_epoch)
plt.plot(range(1, len(loss_list)+1), loss_list, color="blue", label="Sum of square error")
plt.legend()
# plt.ylim((0, 2.5e+04))
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.xlabel("Time step")
plt.title("Widrow-Hoff experiment (Leaky ReLU)")
plt.show()
def perform_widrow_hoff_experiment_elu():
""" Perform the Widrow-Hoff experiment, based on descriptions in [1] (exercise P10.9).
But this time we use ELU.
[1] M. Hagan et al., Neural network design (2nd ed.), 2014, .
Parameters
---------------
None.
Returns
----------
X_train: Numpy array.
The training data, of size (n_inst, n_features) where
n_inst is the number of instances, and n_features is
the number of features.
y_train: Numpy array.
The ground truth, of size (n_inst).
"""
(X_train, y_train) = get_experiment_data()
(n_inst, n_features) = X_train.shape
# learning_rate_list = np.arange(0.001, 1.0, 0.001)
# learning_rate_list = np.arange(0.032, 1.0, 0.001)
# leaky_alpha_list = np.arange(0.001, 1.0, 0.001)
# learning_rate_list = np.arange(0.01, 1.0, 0.01)
# leaky_alpha_list = np.arange(0.01, 1.0, 0.01)
# for learning_rate in learning_rate_list:
# q1 = 0
# for leaky_alpha in leaky_alpha_list:
# print("Learning rate: %f, leaky alpha: %f" % (learning_rate, leaky_alpha))
# net = Perceptron(n_features, 1, "elu", learning_rate=learning_rate, leaky_alpha=leaky_alpha)
# n_epoch = 1000
# loss_list = net.train(X_train, y_train, n_epoch)
# loss = loss_list[-1]
# print("Loss: %f" % (loss))
# if loss < 0.005:
# print("Found:")
# print(learning_rate)
# print(leaky_alpha)
# q1 = 1
# break
# if q1 == 1:
# break
# set_trace()
net = Perceptron(n_features, 1, "elu", learning_rate=0.06, leaky_alpha=0.1)
n_epoch = 1000
loss_list = net.train(X_train, y_train, n_epoch)
# loss_list = loss_list[-1000:]
plt.plot(range(1, len(loss_list)+1), loss_list, color="blue", label="Sum of square error")
plt.legend()
# plt.ylim((0, 2.5e+04))
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.xlabel("Time step")
plt.title("Widrow-Hoff experiment (ELU)")
plt.show()
def main():
perform_widrow_hoff_experiment()
perform_widrow_hoff_experiment_leaky_relu()
# perform_widrow_hoff_experiment_elu()
if __name__ == "__main__":
main()