-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple-rnn.py
265 lines (199 loc) · 7.01 KB
/
simple-rnn.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
#!/usr/bin/env python
## A simple rnn in numpy - the REAL catch22.
import numpy as np
import sys
import ipdb
from itertools import izip
RANDOM_SEED = 12
np.random.seed(RANDOM_SEED)
def generate_data(length, size):
"""
Generate a random sequence of 1's and 0's.
Inputs: length of sequences, number of samples
Outputs: list of sequences, sum of sequences
"""
print "Generating %d samples of length %d."%(size, length)
data, target = [], []
for i in xrange(size):
row = np.zeros(length)
n_ones = np.random.randint(low=0, high=length+1)
n_indices = np.random.random_integers(low=0, high=length-1, size=n_ones)
row[n_indices] = 1
data.append(row)
target.append(np.sum(row))
assert len(data) == len(target), "-- data, target lengths mis-match --"
return np.array(data), np.array(target)
def relu(x):
"""
ReLU activation,
Inputs: any number
Outputs: ReLU'd number
"""
return np.maximum(0, x)
def deriv_relu(x):
"""
Derivative function for ReLU.
"""
g = np.zeros_like(x)
g[x>0] = 1
return g
def forward_pass((Wi, Wh, Wo), x):
"""
Run single instance of a forward pass.
Inputs: (weights), x_input
Outputs: S_input, S_activation, y
"""
LENGTH = x.shape[1] # LENGTH of sequence
aS = np.zeros((x.shape[0], LENGTH+1)) # BSIZE, LENGTH+1
bS = np.zeros_like(aS)
for t in range(LENGTH):
aS[:, t] = (x[:, t] * Wi) + (bS[:, t-1] * Wh) # right now everything is one number
bS[:, t] = relu(aS[:, t])
y = bS[:, t] * Wo
return aS, bS, y
def mse_loss(y, z):
"""
Returns mse error.
Inputs: output, target
Outputs: mse
"""
# ipdb.set_trace()
assert y.shape == z.shape, "-- y, z shape mis-match --"
assert len(y.shape) == 1, "-- y shape is incorrect --"
return 0.5 * np.mean(np.square(z - y), axis=0)
def backward_pass((Wi, Wh, Wo), x, z, aS, bS, y):
"""
Returns updates for weights.
Inputs: (weights), input, target, aS, bS, output
Returns: del_Wi, del_Wh, del_Wo
"""
LENGTH = x.shape[1] # LENGTH of sequence
BSIZE = x.shape[0]
del_Wi = np.zeros(BSIZE)
del_Wh = np.zeros(BSIZE)
## del_Wo only has one term since
## Wo is only dependent on one activation.
assert y.shape == z.shape, "-- y, z shape mis-match --"
assert len(y.shape) == 1, "-- y shape incorrect --"
del_output = y - z
del_Wo = del_output * bS[:, LENGTH-1]
hidden_factor = 1
hidden_constant = del_output * Wo
input_factor = 1
input_constant = del_output * Wo
## del_Wh, del_Wi require only 1 loop because
## the next layer has only one activation
## which occurs at the final timestep.
# ipdb.set_trace()
for t in range(LENGTH)[::-1]:
hidden_time_derivative = deriv_relu(aS[:, t]) * bS[:, t-1]
del_Wh += hidden_constant * hidden_factor * hidden_time_derivative
hidden_factor *= deriv_relu(aS[:, t]) * Wh ## dS(t)/dS(t-1)
input_time_derivative = deriv_relu(aS[:, t]) * x[:, t]
del_Wi += input_constant * input_factor * input_time_derivative
input_factor *= deriv_relu(aS[:, t]) * Wh ## dS(t)/dS(t-1)
return np.mean(del_Wo, axis=0) , np.mean(del_Wh, axis=0), np.mean(del_Wi, axis=0)
def gradient_check():
"""
Do a numerical gradient check for the entire model.
"""
print "Running numerical gradient check..."
SMALL_VAL = 1e-5
SCALE = 0.1
Wi = np.random.rand() * SCALE
Wh = np.random.rand() * SCALE
Wo = np.random.rand() * SCALE
x = np.array([
[1, 0, 1, 0, 1],
[1, 1, 1, 1, 0]
])
z = np.array([3, 4])
aS, bS, y = forward_pass((Wi, Wh, Wo), x)
del_Wo, del_Wh, del_Wi = backward_pass((Wi, Wh, Wo), x, z, aS, bS, y)
## del_Wo
_, _, upper = forward_pass((Wi, Wh, Wo+SMALL_VAL), x)
lupper = mse_loss(upper, z)
_, _, lower = forward_pass((Wi, Wh, Wo-SMALL_VAL), x)
llower = mse_loss(lower, z)
num_grad = (lupper - llower)/(2*SMALL_VAL)
assert np.allclose(del_Wo, num_grad, rtol=1e-4), \
"-- Mismatch numerical: %f, analytical: % --f"%(num_grad, del_Wo)
## del_Wh
_, _, upper = forward_pass((Wi, Wh+SMALL_VAL, Wo), x)
lupper = mse_loss(upper, z)
_, _, lower = forward_pass((Wi, Wh-SMALL_VAL, Wo), x)
llower = mse_loss(lower, z)
num_grad = (lupper - llower)/(2*SMALL_VAL)
assert np.allclose(del_Wh, num_grad, rtol=1e-4), \
"-- Mismatch numerical: %f, analytical: %f --"%(num_grad, del_Wh)
## del_Wi
_, _, upper = forward_pass((Wi+SMALL_VAL, Wh, Wo), x)
lupper = mse_loss(upper, z)
_, _, lower = forward_pass((Wi-SMALL_VAL, Wh, Wo), x)
llower = mse_loss(lower, z)
num_grad = (lupper - llower)/(2*SMALL_VAL)
assert np.allclose(del_Wi, num_grad, rtol=1e-4), \
"-- Mismatch numerical: %f, analytical: %f --"%(num_grad, del_Wi)
print "PASSED"
return
def main():
"""
Main code.
"""
LENGTH, SAMPLES, BSIZE = 8, 10000, 100
TEST_SAMPLES = 100
EPOCHS = 5
SCALE = 0.1
ALPHA = 0.01
## parameters
Wi = np.random.rand() * SCALE
Wh = np.random.rand() * SCALE
Wo = np.random.rand() * SCALE
## training data
data, target = generate_data(length=LENGTH, size=SAMPLES)
## testing data
test_data, test_target = generate_data(length=LENGTH, size=TEST_SAMPLES)
log = "Epoch %d. train_loss: %.2f, train_acc: %.2f, test_loss: %.2f, test_acc: %.2f"
print "\nStart training.\n"
for _ep in xrange(EPOCHS):
test_acc, test_loss = [], []
train_acc, train_loss = [], []
_limit = SAMPLES//BSIZE
if SAMPLES%BSIZE != 0:
_limit += 1
for _ix in xrange(_limit):
# x, z = np.atleast_2d(data[_ix]), np.array([target[_ix]])
x = np.atleast_2d(data[_ix*BSIZE : (_ix+1)*BSIZE])
z = np.atleast_1d(target[_ix*BSIZE : (_ix+1)*BSIZE])
aS, bS, y = forward_pass((Wi, Wh, Wo), x)
loss = mse_loss(y, z)
del_Wo, del_Wh, del_Wi = backward_pass((Wi, Wh, Wo), x, z, aS, bS, y)
Wi -= ALPHA * del_Wi
Wh -= ALPHA * del_Wh
Wo -= ALPHA * del_Wo
train_acc.append(np.mean(np.round(y)==z))
train_loss.append(loss)
aS, bS, y = forward_pass((Wi, Wh, Wo), np.atleast_2d(test_data))
loss = mse_loss(y, np.atleast_1d(test_target))
test_acc = np.mean(np.round(y)==np.atleast_1d(test_target))
test_loss = loss
print log%(_ep+1, np.mean(train_loss), np.mean(train_acc), test_loss, test_acc)
print "\nComplete.\n"
print "Trained parameters:\nWi %f\nWh %f\nWo %f\n"%(Wi, Wh, Wo)
return
if __name__ == '__main__':
gradient_check()
main()
# x = np.array([
# [1, 1, 1, 1, 1],
# [0, 0, 0, 0, 0]
# ])
# z = np.array([5, 0])
#
# SCALE = 0.1
# Wi = np.random.rand() * SCALE
# Wh = np.random.rand() * SCALE
# Wo = np.random.rand() * SCALE
#
# aS, bS, y = forward_pass((Wi, Wh, Wo), x)
# print backward_pass((Wi, Wh, Wo), x, z, aS, bS, y)