-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmodel.py
197 lines (167 loc) · 7.23 KB
/
model.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
import numpy as np
import time
class DeepNeuralNetwork():
def __init__(self, sizes, activation='sigmoid'):
self.sizes = sizes
# Choose activation function
if activation == 'relu':
self.activation = self.relu
elif activation == 'sigmoid':
self.activation = self.sigmoid
else:
raise ValueError("Activation function is currently not support, please use 'relu' or 'sigmoid' instead.")
# Save all weights
self.params = self.initialize()
# Save all intermediate values, i.e. activations
self.cache = {}
def relu(self, x, derivative=False):
'''
Derivative of ReLU is a bit more complicated since it is not differentiable at x = 0
Forward path:
relu(x) = max(0, x)
In other word,
relu(x) = 0, if x < 0
= x, if x >= 0
Backward path:
∇relu(x) = 0, if x < 0
= 1, if x >=0
'''
if derivative:
x = np.where(x < 0, 0, x)
x = np.where(x >= 0, 1, x)
return x
return np.maximum(0, x)
def sigmoid(self, x, derivative=False):
'''
Forward path:
σ(x) = 1 / 1+exp(-z)
Backward path:
∇σ(x) = exp(-z) / (1+exp(-z))^2
'''
if derivative:
return (np.exp(-x))/((np.exp(-x)+1)**2)
return 1/(1 + np.exp(-x))
def softmax(self, x):
'''
softmax(x) = exp(x) / ∑exp(x)
'''
# Numerically stable with large exponentials
exps = np.exp(x - x.max())
return exps / np.sum(exps, axis=0)
def initialize(self):
# number of nodes in each layer
input_layer=self.sizes[0]
hidden_layer=self.sizes[1]
output_layer=self.sizes[2]
params = {
"W1": np.random.randn(hidden_layer, input_layer) * np.sqrt(1./input_layer),
"b1": np.zeros((hidden_layer, 1)) * np.sqrt(1./input_layer),
"W2": np.random.randn(output_layer, hidden_layer) * np.sqrt(1./hidden_layer),
"b2": np.zeros((output_layer, 1)) * np.sqrt(1./hidden_layer)
}
return params
def initialize_momemtum_optimizer(self):
momemtum_opt = {
"W1": np.zeros(self.params["W1"].shape),
"b1": np.zeros(self.params["b1"].shape),
"W2": np.zeros(self.params["W2"].shape),
"b2": np.zeros(self.params["b2"].shape),
}
return momemtum_opt
def feed_forward(self, x):
'''
y = σ(wX + b)
'''
self.cache["X"] = x
self.cache["Z1"] = np.matmul(self.params["W1"], self.cache["X"].T) + self.params["b1"]
self.cache["A1"] = self.activation(self.cache["Z1"])
self.cache["Z2"] = np.matmul(self.params["W2"], self.cache["A1"]) + self.params["b2"]
self.cache["A2"] = self.softmax(self.cache["Z2"])
return self.cache["A2"]
def back_propagate(self, y, output):
'''
This is the backpropagation algorithm, for calculating the updates
of the neural network's parameters.
Note: There is a stability issue that causes warnings. This is
caused by the dot and multiply operations on the huge arrays.
RuntimeWarning: invalid value encountered in true_divide
RuntimeWarning: overflow encountered in exp
RuntimeWarning: overflow encountered in square
'''
current_batch_size = y.shape[0]
dZ2 = output - y.T
dW2 = (1./current_batch_size) * np.matmul(dZ2, self.cache["A1"].T)
db2 = (1./current_batch_size) * np.sum(dZ2, axis=1, keepdims=True)
dA1 = np.matmul(self.params["W2"].T, dZ2)
dZ1 = dA1 * self.activation(self.cache["Z1"], derivative=True)
dW1 = (1./current_batch_size) * np.matmul(dZ1, self.cache["X"])
db1 = (1./current_batch_size) * np.sum(dZ1, axis=1, keepdims=True)
self.grads = {"W1": dW1, "b1": db1, "W2": dW2, "b2": db2}
return self.grads
def cross_entropy_loss(self, y, output):
'''
L(y, ŷ) = −∑ylog(ŷ).
'''
l_sum = np.sum(np.multiply(y.T, np.log(output)))
m = y.shape[0]
l = -(1./m) * l_sum
return l
def optimize(self, l_rate=0.1, beta=.9):
'''
Stochatic Gradient Descent (SGD):
θ^(t+1) <- θ^t - η∇L(y, ŷ)
Momentum:
v^(t+1) <- βv^t + (1-β)∇L(y, ŷ)^t
θ^(t+1) <- θ^t - ηv^(t+1)
'''
if self.optimizer == "sgd":
for key in self.params:
self.params[key] = self.params[key] - l_rate * self.grads[key]
elif self.optimizer == "momentum":
for key in self.params:
self.momemtum_opt[key] = (beta * self.momemtum_opt[key] + (1. - beta) * self.grads[key])
self.params[key] = self.params[key] - l_rate * self.momemtum_opt[key]
else:
raise ValueError("Optimizer is currently not support, please use 'sgd' or 'momentum' instead.")
def accuracy(self, y, output):
return np.mean(np.argmax(y, axis=-1) == np.argmax(output.T, axis=-1))
def train(self, x_train, y_train, x_test, y_test, epochs=10,
batch_size=64, optimizer='momentum', l_rate=0.1, beta=.9):
# Hyperparameters
self.epochs = epochs
self.batch_size = batch_size
num_batches = -(-x_train.shape[0] // self.batch_size)
# Initialize optimizer
self.optimizer = optimizer
if self.optimizer == 'momentum':
self.momemtum_opt = self.initialize_momemtum_optimizer()
start_time = time.time()
template = "Epoch {}: {:.2f}s, train acc={:.2f}, train loss={:.2f}, test acc={:.2f}, test loss={:.2f}"
# Train
for i in range(self.epochs):
# Shuffle
permutation = np.random.permutation(x_train.shape[0])
x_train_shuffled = x_train[permutation]
y_train_shuffled = y_train[permutation]
for j in range(num_batches):
# Batch
begin = j * self.batch_size
end = min(begin + self.batch_size, x_train.shape[0]-1)
x = x_train_shuffled[begin:end]
y = y_train_shuffled[begin:end]
# Forward
output = self.feed_forward(x)
# Backprop
_ = self.back_propagate(y, output)
# Optimize
self.optimize(l_rate=l_rate, beta=beta)
# Evaluate performance
# Training data
output = self.feed_forward(x_train)
train_acc = self.accuracy(y_train, output)
train_loss = self.cross_entropy_loss(y_train, output)
# Test data
output = self.feed_forward(x_test)
test_acc = self.accuracy(y_test, output)
test_loss = self.cross_entropy_loss(y_test, output)
print(template.format(i+1, time.time()-start_time, train_acc, train_loss, test_acc, test_loss))