-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLP.py
204 lines (188 loc) · 8.43 KB
/
MLP.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
# MLP Class
# Ziping Chen
# March 2020
import numpy as np
import h5py
import matplotlib.pyplot as plt
from Layer import Layer
class MLP:
def __init__(self, net_conf):
self.arch = net_conf
self.setup_network()
def setup_network(self):
assert len(self.arch['layer'].keys()) >= 3, "Network Architecture Error!!"
self.net = {}
for i, l in enumerate(self.arch['layer'].keys()):
if i == 0:
continue
self.net[l] = Layer(self.arch['layer'][list(self.arch['layer'].keys())[i - 1]]['num'],
self.arch['layer'][l]['num'],
l,
self.arch['layer'][l]['activation'],
self.arch['regularizer'])
# print network detail
# fake keras interface
def summary(self):
print('Layer\tOutput\tActivation\tRegularization')
for i, l in enumerate(self.arch['layer']):
print('%s\t%5d\t%9s\t%14s' % (l, self.arch['layer'][l]['num'],
'None' if not 'activation' in self.arch['layer'][l] else self.arch['layer'][l]['activation'],
'None' if self.arch['regularizer'] is None or i == 0
else self.arch['regularizer'][0] + '=' + str(self.arch['regularizer'][1])))
def feedforward(self, x):
# feedforward
for n in self.net:
x = self.net[n].forward(x)
return x
def backpropagation(self, y_hat, y, eta):
delta = y_hat - y
# backpropagation
for n in reversed(list(self.net.keys())):
delta = self.net[n].backward(delta)
# update parameters
for n in self.net:
self.net[n].update(eta)
def cost(self, y_hat, y):
# cross entropy
# not use in this code
return -np.sum(y * np.log(y_hat))
# prediction
# fake keras interface
def predict(self, x):
y_hat = self.feedforward(x)
return y_hat
def accuracy(self, x, y):
y_hat = self.predict(x)
pred = np.argmax(y_hat, axis=1)
label = np.argmax(y, axis=1)
return np.sum(pred == label) / pred.shape[0]
# performance test
# fake keras interface
def perform(self, x, y):
acc = self.accuracy(x, y)
print("Accuracy: %lf" % (acc))
# stochastic gradient descent
def sgd(self, eta=0.001, epoch=50, minibatch=500, momentum=False, decay=False):
# SGD
# TODO:
# momentum, adam, batch norm...
iteration = self.training['xdata'].shape[0] // minibatch
print("<====start training====>")
print("batch size is", minibatch)
self.train_acc = []
self.valid_acc = []
self.test_acc = []
self.decay_pts = []
for i in range(epoch):
# 1. randomly shuffle
p = np.random.permutation(self.training['xdata'].shape[0])
for j in range(iteration):
string = "\r[epoch %d/%d] iteration#%d " % (i + 1, epoch, j + 1)
y_hat = self.feedforward(self.training['xdata'][p][j * minibatch:(j + 1) * minibatch])
self.backpropagation(y_hat, self.training['ydata'][p][j * minibatch:(j + 1) * minibatch], eta)
if bool(self.validation) is False:
acc = self.accuracy(self.test['xdata'], self.test['ydata'])
print(string + "test acc: %lf " % (acc), end='')
else:
v_acc = self.accuracy(self.validation['xdata'], self.validation['ydata'])
print(string + "valid acc: %lf " % (v_acc), end='')
t_acc = self.accuracy(self.training['xdata'], self.training['ydata'])
self.train_acc.append(t_acc)
if bool(self.validation) is False:
acc = self.accuracy(self.test['xdata'], self.test['ydata'])
self.test_acc.append(acc)
print("[epoch end] train acc: %lf, test acc: %lf " % (t_acc, acc), end='')
else:
v_acc = self.accuracy(self.validation['xdata'], self.validation['ydata'])
self.valid_acc.append(v_acc)
print("[epoch end] train acc: %lf, valid acc: %lf " % (t_acc, v_acc), end='')
if decay is True and (i + 1) % int(np.ceil(epoch / 3)) == 0:
eta /= 2.0
self.decay_pts.append(i)
print("Learning rate decay by 2: %lf" % (eta), end='')
print('')
# training method
# fake keras interface
def fit(self, train_x, train_y, config, valid=None, test_x=None, test_y=None):
self.config = config
self.training = {}
self.training['xdata'] = train_x
self.training['ydata'] = train_y
training_size = self.training['xdata'].shape[0]
self.validation = {}
self.test = {}
if valid is not None and valid > 0.0 and valid < 1.0:
self.training['xdata'], self.validation['xdata'], _ = np.split(self.training['xdata'], [int((1 - valid) * training_size), training_size])
self.training['ydata'], self.validation['ydata'], _ = np.split(self.training['ydata'], [int((1 - valid) * training_size), training_size])
validation_size = int(valid * training_size)
training_size = int((1 - valid) * training_size)
print(f'Train on {training_size} samples, validate on {validation_size} samples')
elif test_x is not None and test_y is not None:
self.test['xdata'] = test_x
self.test['ydata'] = test_y
test_size = self.test['xdata'].shape[0]
print(f'Train on {training_size} samples, test on {test_size} samples')
else:
assert False, "need validation set or test set"
if config['optimizer'] == 'sgd':
self.sgd(config['eta'], config['epoch'],
config['minibatch'], config['momentum'],
config['decay'])
else:
# TODO
# other optimizers
pass
# save model and training process
def save(self, filename):
with h5py.File(filename, 'w') as hf:
for n in self.net:
hf.create_dataset(n + '_w', data=self.net[n].weight)
hf.create_dataset(n + '_b', data=self.net[n].bias)
hf.create_dataset('train_acc', data=self.train_acc)
hf.create_dataset('valid_acc', data=self.valid_acc)
hf.create_dataset('test_acc', data=self.test_acc)
hf.create_dataset('decay_pts', data=self.decay_pts)
# load model and training process
def load(self, filename):
with h5py.File(filename, 'r') as hf:
for n in self.net:
self.net[n].set_par(hf[n + '_w'][:], hf[n + '_b'][:])
self.train_acc = hf['train_acc'][:]
self.valid_acc = hf['valid_acc'][:]
self.test_acc = hf['test_acc'][:]
self.decay_pts = hf['decay_pts'][:]
# plot the learning curve
def plot(self):
title = 'MLP-'
for i, l in enumerate(self.arch['layer']):
title += str(self.arch['layer'][l]['num'])
if i != 0:
title += '(' + self.arch['layer'][l]['activation'] + ')'
if i != len(self.arch['layer'].keys()) - 1:
title += '-'
title += ',epoch=' + str(self.config['epoch'])
title += ',minibatch=' + str(self.config['minibatch'])
title += ',eta=' + str(self.config['eta'])
if self.config['decay'] == True:
title += '(decay)'
if self.arch['regularizer'] is not None:
title += ',' + self.arch['regularizer'][0] + '=' + str(self.arch['regularizer'][1])
plt.title(title)
plt.xlabel("epoch(s)")
plt.ylabel("accuracy")
plt.plot(self.train_acc, label='train accuracy')
if bool(self.validation) is False:
plt.plot(self.test_acc, label='test accuracy')
else:
plt.plot(self.valid_acc, label='validation accuracy')
for i, pt in enumerate(self.decay_pts):
if i == 0:
plt.scatter(pt, self.train_acc[pt], color='red', label="decay points")
else:
plt.scatter(pt, self.train_acc[pt], color='red')
if bool(self.validation) is False:
plt.scatter(pt, self.test_acc[pt], color='red')
else:
plt.scatter(pt, self.valid_acc[pt], color='red')
plt.legend()
plt.show()