-
Notifications
You must be signed in to change notification settings - Fork 4
/
rntn.py
executable file
·281 lines (233 loc) · 9.35 KB
/
rntn.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
"""
Implementation of the Recursive Neural Tensor Network (RNTN) model
"""
import collections
import csv
import pickle
import time
from datetime import datetime
import numpy as np
import tree as tr
class RNTN:
def __init__(self, dim=10, output_dim=5, batch_size=30, reg=10,
learning_rate=1e-2, max_epochs=2, optimizer='adagrad'):
self.dim = dim
self.output_dim = output_dim
self.batch_size = batch_size
self.reg = reg
self.learning_rate = learning_rate
self.max_epochs = max_epochs
self.optimizer_algorithm = optimizer
def fit(self, trees, export_filename='models/RNTN.pickle', verbose=False):
import sgd
self.word_map = tr.load_word_map()
self.num_words = len(self.word_map)
self.init_params()
self.optimizer = sgd.SGD(self, self.learning_rate, self.batch_size,
self.optimizer_algorithm)
test_trees = tr.load_trees('test')
with open("log.csv", "a", newline='') as csvfile:
csvwriter = csv.writer(csvfile)
fieldnames = ["Timestamp", "Vector size", "Learning rate",
"Batch size", "Regularization", "Epoch",
"Train cost", "Train accuracy",
"Test cost", "Test accuracy"]
if csvfile.tell() == 0:
csvwriter.writerow(fieldnames)
for epoch in range(self.max_epochs):
print("Running epoch {} ...".format(epoch))
start = time.time()
self.optimizer.optimize(trees)
end = time.time()
print(" Time per epoch = {:.4f}".format(end-start))
# Save the model
self.save(export_filename)
# Test the model on train and test set
train_cost, train_result = self.test(trees)
train_accuracy = 100.0 * train_result.trace() / train_result.sum()
test_cost, test_result = self.test(test_trees)
test_accuracy = 100.0 * test_result.trace() / test_result.sum()
# Append data to CSV file
row = [datetime.now(), self.dim, self.learning_rate,
self.batch_size, self.reg, epoch,
train_cost, train_accuracy,
test_cost, test_accuracy]
csvwriter.writerow(row)
def test(self, trees):
"""
TODO: This should return the confusion matrix
"""
return self.cost_and_grad(trees, test=True)
def predict(self, tree):
if tr.isleaf(tree):
# output = word vector
try:
tree.vector = self.L[:, self.word_map[tree[0]]]
except:
tree.vector = self.L[:, self.word_map[tr.UNK]]
else:
# calculate output of child nodes
self.predict(tree[0])
self.predict(tree[1])
# compute output
lr = np.hstack([tree[0].vector, tree[1].vector])
tree.vector = np.tanh(
np.tensordot(self.V, np.outer(lr, lr), axes=([1, 2], [0, 1])) +
np.dot(self.W, lr) + self.b)
# softmax
import util
tree.output = util.softmax(np.dot(self.Ws, tree.vector) + self.bs)
label = np.argmax(tree.output)
tree.set_label(str(label))
return tree
def save(self, filename):
with open(filename, 'wb') as f:
pickle.dump(self.dim, f)
pickle.dump(self.output_dim, f)
pickle.dump(self.batch_size, f)
pickle.dump(self.reg, f)
pickle.dump(self.learning_rate, f)
pickle.dump(self.max_epochs, f)
pickle.dump(self.stack, f)
pickle.dump(self.word_map, f)
def load(filename):
with open(filename, 'rb') as f:
dim = pickle.load(f)
output_dim = pickle.load(f)
batch_size = pickle.load(f)
reg = pickle.load(f)
learning_rate = pickle.load(f)
max_epochs = pickle.load(f)
stack = pickle.load(f)
model = RNTN(dim=dim, output_dim=output_dim, batch_size=batch_size,
reg=reg, learning_rate=learning_rate, max_epochs=max_epochs)
model.stack = stack
model.L, model.V, model.W, model.b, model.Ws, model.bs = model.stack
model.word_map = pickle.load(f)
return model
def init_params(self):
print("Initializing RNTN parameters...")
# word vectors
self.L = 0.01 * np.random.randn(self.dim, self.num_words)
# RNTN parameters
self.V = 0.01 * np.random.randn(self.dim, 2*self.dim, 2*self.dim)
self.W = 0.01 * np.random.randn(self.dim, 2*self.dim)
self.b = 0.01 * np.random.randn(self.dim)
# Softmax parameters
self.Ws = 0.01 * np.random.randn(self.output_dim, self.dim)
self.bs = 0.01 * np.random.randn(self.output_dim)
self.stack = [self.L, self.V, self.W, self.b, self.Ws, self.bs]
# Gradients
self.dV = np.empty_like(self.V)
self.dW = np.empty_like(self.W)
self.db = np.empty_like(self.b)
self.dWs = np.empty_like(self.Ws)
self.dbs = np.empty_like(self.bs)
def cost_and_grad(self, trees, test=False):
cost, result = 0.0, np.zeros((5,5))
self.L, self.V, self.W, self.b, self.Ws, self.bs = self.stack
# Forward propagation
for tree in trees:
_cost, _result = self.forward_prop(tree)
cost += _cost
result += _result
if test:
return cost / len(trees), result
# Initialize gradients
self.dL = collections.defaultdict(lambda: np.zeros((self.dim,)))
self.dV[:] = 0
self.dW[:] = 0
self.db[:] = 0
self.dWs[:] = 0
self.dbs[:] = 0
# Back propagattion
for tree in trees:
self.back_prop(tree)
# Scale cost and gradients by minibatch size
scale = 1.0 / self.batch_size
for v in self.dL.values():
v *= scale
# Add L2 reguralization
cost += 0.5 * self.reg * np.sum(self.V ** 2)
cost += 0.5 * self.reg * np.sum(self.W ** 2)
cost += 0.5 * self.reg * np.sum(self.Ws ** 2)
cost *= scale
grad = [self.dL,
scale * (self.dV + (self.reg * self.V)),
scale * (self.dW + (self.reg * self.W)),
scale * self.db,
scale * (self.dWs + (self.reg * self.Ws)),
scale * self.dbs]
return cost, grad
def forward_prop(self, tree):
cost = 0.0
result = np.zeros((5,5))
if tr.isleaf(tree):
# output = word vector
try:
tree.vector = self.L[:, self.word_map[tree[0]]]
except:
tree.vector = self.L[:, self.word_map[tr.UNK]]
tree.fprop = True
else:
# calculate output of child nodes
lcost, lresult = self.forward_prop(tree[0])
rcost, rresult = self.forward_prop(tree[1])
cost += lcost + rcost
result += lresult + rresult
# compute output
lr = np.hstack([tree[0].vector, tree[1].vector])
tree.vector = np.tanh(
np.tensordot(self.V, np.outer(lr, lr), axes=([1, 2], [0, 1])) +
np.dot(self.W, lr) + self.b)
# softmax
tree.output = np.dot(self.Ws, tree.vector) + self.bs
tree.output -= np.max(tree.output)
tree.output = np.exp(tree.output)
tree.output /= np.sum(tree.output)
tree.frop = True
# cost
cost -= np.log(tree.output[int(tree.label())])
true_label = int(tree.label())
predicted_label = np.argmax(tree.output)
result[true_label, predicted_label] += 1
return cost, result
def back_prop(self, tree, error=None):
# clear nodes
tree.frop = False
# softmax grad
deltas = tree.output
deltas[int(tree.label())] -= 1.0
self.dWs += np.outer(deltas, tree.vector)
self.dbs += deltas
deltas = np.dot(self.Ws.T, deltas)
if error is not None:
deltas += error
deltas *= (1 - tree.vector**2)
# leaf node => update word vectors
if tr.isleaf(tree):
try:
index = self.word_map[tree[0]]
except KeyError:
index = self.word_map[tr.UNK]
self.dL[index] += deltas
return
# Hidden gradients
else:
lr = np.hstack([tree[0].vector, tree[1].vector])
outer = np.outer(deltas, lr)
self.dV += (np.outer(lr, lr)[..., None] * deltas).T
self.dW += outer
self.db += deltas
# Compute error for children
deltas = np.dot(self.W.T, deltas)
deltas += np.tensordot(self.V.transpose((0,2,1)) + self.V, outer.T,
axes=([1,0], [0,1]))
self.back_prop(tree[0], deltas[:self.dim])
self.back_prop(tree[1], deltas[self.dim:])
def update_params(self, scale, update):
self.stack[1:] = [P+scale*dP for P, dP in zip(self.stack[1:], update[1:])]
# Update L separately
dL = update[0]
for j in dL.keys():
self.L[:,j] += scale*dL[j]