-
Notifications
You must be signed in to change notification settings - Fork 11
/
nl.py
292 lines (208 loc) · 7.63 KB
/
nl.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
import numpy as np
#%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from IPython import display
plt.style.use('seaborn-white')
# Read and process data
data = open('input.txt', 'r').read()
chars = list(set(data))
data_size, X_size = len(data), len(chars)
print("data has %d characters, %d unique" % (data_size, X_size))
char_to_idx = {ch:i for i,ch in enumerate(chars)}
idx_to_char = {i:ch for i,ch in enumerate(chars)}
# Parameters
H_size = 100 # Size of the hidden layer
T_steps = 25 # Number of time steps (length of the sequence) used for training
learning_rate = 1e-1 # Learning rate
weight_sd = 0.1 # Standard deviation of weights for initialization
z_size = H_size + X_size # Size of concatenate(H, X) vector
# Activations and derivs
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def dsigmoid(y):
return y * (1 - y)
def tanh(x):
return np.tanh(x)
def dtanh(y):
return 1 - y * y
# Init weights
W_f = np.random.randn(H_size, z_size) * weight_sd + 0.5
b_f = np.zeros((H_size, 1))
W_i = np.random.randn(H_size, z_size) * weight_sd + 0.5
b_i = np.zeros((H_size, 1))
W_C = np.random.randn(H_size, z_size) * weight_sd
b_C = np.zeros((H_size, 1))
W_o = np.random.randn(H_size, z_size) * weight_sd + 0.5
b_o = np.zeros((H_size, 1))
#For final layer to predict the next character
W_y = np.random.randn(X_size, H_size) * weight_sd
b_y = np.zeros((X_size, 1))
# Gradients
dW_f = np.zeros_like(W_f)
dW_i = np.zeros_like(W_i)
dW_C = np.zeros_like(W_C)
dW_o = np.zeros_like(W_o)
dW_y = np.zeros_like(W_y)
db_f = np.zeros_like(b_f)
db_i = np.zeros_like(b_i)
db_C = np.zeros_like(b_C)
db_o = np.zeros_like(b_o)
db_y = np.zeros_like(b_y)
def forward(x, h_prev, C_prev):
assert x.shape == (X_size, 1)
assert h_prev.shape == (H_size, 1)
assert C_prev.shape == (H_size, 1)
z = np.row_stack((h_prev, x))
f = sigmoid(np.dot(W_f, z) + b_f)
i = sigmoid(np.dot(W_i, z) + b_i)
C_bar = tanh(np.dot(W_C, z) + b_C)
C = f * C_prev + i * C_bar
o = sigmoid(np.dot(W_o, z) + b_o)
h = o * tanh(C)
y = np.dot(W_y, h) + b_y
p = np.exp(y) / np.sum(np.exp(y))
return z, f, i, C_bar, C, o, h, y, p
def backward(target, dh_next, dC_next, C_prev, z, f, i, C_bar, C, o, h, y, p):
global dW_f, dW_i, dW_C, dW_o, dW_y
global db_f, db_i, db_C, db_o, db_y
assert z.shape == (X_size + H_size, 1)
assert y.shape == (X_size, 1)
assert p.shape == (X_size, 1)
for param in [dh_next, dC_next, C_prev, f, i, C_bar, C, o, h]:
assert param.shape == (H_size, 1)
dy = np.copy(p)
dy[target] -= 1
dW_y += np.dot(dy, h.T)
db_y += dy
dh = np.dot(W_y.T, dy)
dh += dh_next
do = dh * tanh(C)
do = dsigmoid(o) * do
dW_o += np.dot(do, z.T)
db_o += do
dC = np.copy(dC_next)
dC += dh * o * dtanh(tanh(C))
dC_bar = dC * i
dC_bar = dC_bar * dtanh(C_bar)
dW_C += np.dot(dC_bar, z.T)
db_C += dC_bar
di = dC * C_bar
di = dsigmoid(i) * di
dW_i += np.dot(di, z.T)
db_i += di
df = dC * C_prev
df = dsigmoid(f) * df
dW_f += np.dot(df, z.T)
db_f += df
dz = np.dot(W_f.T, df) \
+ np.dot(W_i.T, di) \
+ np.dot(W_C.T, dC_bar) \
+ np.dot(W_o.T, do)
dh_prev = dz[:H_size, :]
dC_prev = f * dC
return dh_prev, dC_prev
def forward_backward(inputs, targets, h_prev, C_prev):
# To store the values for each time step
x_s, z_s, f_s, i_s, C_bar_s, C_s, o_s, h_s, y_s, p_s = {}, {}, {}, {}, {}, {}, {}, {}, {}, {}
# Values at t - 1
h_s[-1] = np.copy(h_prev)
C_s[-1] = np.copy(C_prev)
loss = 0
# Loop through time steps
assert len(inputs) == T_steps
for t in range(len(inputs)):
x_s[t] = np.zeros((X_size, 1))
x_s[t][inputs[t]] = 1 # Input character
z_s[t], f_s[t], i_s[t], C_bar_s[t], C_s[t], o_s[t], h_s[t], y_s[t], p_s[t] \
= forward(x_s[t], h_s[t - 1], C_s[t - 1]) # Forward pass
loss += -np.log(p_s[t][targets[t], 0]) # Loss for at t
for dparam in [dW_f, dW_i, dW_C, dW_o, dW_y, db_f, db_i, db_C, db_o, db_y]:
dparam.fill(0)
dh_next = np.zeros_like(h_s[0]) #dh from the next character
dC_next = np.zeros_like(C_s[0]) #dh from the next character
for t in reversed(range(len(inputs))):
# Backward pass
dh_next, dC_next = backward(target = targets[t], dh_next = dh_next, dC_next = dC_next, C_prev = C_s[t-1],
z = z_s[t], f = f_s[t], i = i_s[t], C_bar = C_bar_s[t], C = C_s[t], o = o_s[t],
h = h_s[t], y = y_s[t], p = p_s[t])
# Clip gradients to mitigate exploding gradients
for dparam in [dW_f, dW_i, dW_C, dW_o, dW_y, db_f, db_i, db_C, db_o, db_y]:
np.clip(dparam, -1, 1, out=dparam)
return loss, h_s[len(inputs) - 1], C_s[len(inputs) - 1]
def sample(h_prev, C_prev, first_char_idx, sentence_length):
x = np.zeros((X_size, 1))
x[first_char_idx] = 1
h = h_prev
C = C_prev
indexes = []
for t in range(sentence_length):
_, _, _, _, C, _, h, _, p = forward(x, h, C)
idx = np.random.choice(range(X_size), p=p.ravel())
x = np.zeros((X_size, 1))
x[idx] = 1
indexes.append(idx)
return indexes
def update_status(inputs, h_prev, C_prev):
#initialized later
global plot_iter, plot_loss
global smooth_loss
# Get predictions for 200 letters with current model
display.clear_output(wait=True)
sample_idx = sample(h_prev, C_prev, inputs[0], 2000)
txt = ''.join(idx_to_char[idx] for idx in sample_idx)
# Clear and plot
plt.plot(plot_iter, plot_loss)
display.display(plt.gcf())
#Print prediction and loss
print("----\n %s \n----" % (txt, ))
print("iter %d, loss %f" % (iteration, smooth_loss))
# Memory variables for Adagrad
mW_f = np.zeros_like(W_f)
mW_i = np.zeros_like(W_i)
mW_C = np.zeros_like(W_C)
mW_o = np.zeros_like(W_o)
mW_y = np.zeros_like(W_y)
mb_f = np.zeros_like(b_f)
mb_i = np.zeros_like(b_i)
mb_C = np.zeros_like(b_C)
mb_o = np.zeros_like(b_o)
mb_y = np.zeros_like(b_y)
# Exponential average of loss
# Initialize to a error of a random model
smooth_loss = -np.log(1.0 / X_size) * T_steps
iteration, p = 0, 0
# For the graph
plot_iter = np.zeros((0))
plot_loss = np.zeros((0))
while True:
# Try catch for interruption
try:
# Reset
if p + T_steps >= len(data) or iteration == 0:
g_h_prev = np.zeros((H_size, 1))
g_C_prev = np.zeros((H_size, 1))
p = 0
inputs = [char_to_idx[ch] for ch in data[p: p + T_steps]]
targets = [char_to_idx[ch] for ch in data[p + 1: p + T_steps + 1]]
# print("INPUTS typ({}) len({}) ins({})".format(type(inputs), len(inputs), inputs))
loss, g_h_prev, g_C_prev = forward_backward(inputs, targets, g_h_prev, g_C_prev)
smooth_loss = smooth_loss * 0.999 + loss * 0.001
# Print every hundred steps
if iteration % 100 == 0:
update_status(inputs, g_h_prev, g_C_prev)
# Update weights
for param, dparam, mem in zip([W_f, W_i, W_C, W_o, W_y, b_f, b_i, b_C, b_o, b_y],
[dW_f, dW_i, dW_C, dW_o, dW_y, db_f, db_i, db_C, db_o, db_y],
[mW_f, mW_i, mW_C, mW_o, mW_y, mb_f, mb_i, mb_C, mb_o, mb_y]):
mem += dparam * dparam # Calculate sum of gradients
#print(learning_rate * dparam)
param += -(learning_rate * dparam / np.sqrt(mem + 1e-8))
plot_iter = np.append(plot_iter, [iteration])
plot_loss = np.append(plot_loss, [loss])
p += T_steps
iteration += 1
except KeyboardInterrupt:
update_status(inputs, g_h_prev, g_C_prev)
plt.show()
break