MCPcopy Index your code
hub / github.com/lazyprogrammer/machine_learning_examples / main

Function main

nlp_class2/rntn_theano.py:296–353  ·  view source on GitHub ↗
(is_binary=True)

Source from the content-addressed store, hash-verified

294
295
296def main(is_binary=True):
297 train, test, word2idx = get_ptb_data()
298
299 for t in train:
300 add_idx_to_tree(t, 0)
301 train = [tree2list(t, -1, is_binary) for t in train]
302 if is_binary:
303 train = [t for t in train if t[3][-1] >= 0] # for filtering binary labels
304
305 for t in test:
306 add_idx_to_tree(t, 0)
307 test = [tree2list(t, -1, is_binary) for t in test]
308 if is_binary:
309 test = [t for t in test if t[3][-1] >= 0] # for filtering binary labels
310
311 # check imbalance
312 # pos = 0
313 # neg = 0
314 # mid = 0
315 # label_counts = np.zeros(5)
316 # for t in train + test:
317 # words, left_child, right_child, labels = t
318 # # for l in labels:
319 # # if l == 0:
320 # # neg += 1
321 # # elif l == 1:
322 # # pos += 1
323 # # else:
324 # # mid += 1
325 # for l in labels:
326 # label_counts[l] += 1
327 # # print("pos / total:", float(pos) / (pos + neg + mid))
328 # # print("mid / total:", float(mid) / (pos + neg + mid))
329 # # print("neg / total:", float(neg) / (pos + neg + mid))
330 # print("label proportions:", label_counts / label_counts.sum())
331 # exit()
332
333
334 train = shuffle(train)
335 # train = train[:5000]
336 # n_pos = sum(t[3][-1] for t in train)
337 # print("n_pos train:", n_pos)
338 test = shuffle(test)
339 smalltest = test[:1000]
340 # n_pos = sum(t[3][-1] for t in test)
341 # print("n_pos test:", n_pos)
342
343 V = len(word2idx)
344 print("vocab size:", V)
345 D = 20
346 K = 2 if is_binary else 5
347
348 model = RecursiveNN(V, D, K)
349 model.fit(train, smalltest, epochs=20, train_inner_nodes=True)
350 print("train accuracy:", model.score(train))
351 print("test accuracy:", model.score(test))
352 print("train f1:", model.f1_score(train))
353 print("test f1:", model.f1_score(test))

Callers 1

rntn_theano.pyFile · 0.70

Calls 7

fitMethod · 0.95
scoreMethod · 0.95
f1_scoreMethod · 0.95
get_ptb_dataFunction · 0.90
add_idx_to_treeFunction · 0.70
tree2listFunction · 0.70
RecursiveNNClass · 0.70

Tested by

no test coverage detected