(is_binary=True)
| 249 | |
| 250 | |
| 251 | def main(is_binary=True): |
| 252 | train, test, word2idx = get_ptb_data() |
| 253 | |
| 254 | for t in train: |
| 255 | add_idx_to_tree(t, 0) |
| 256 | train = [tree2list(t, -1, is_binary) for t in train] |
| 257 | if is_binary: |
| 258 | train = [t for t in train if t[3][-1] >= 0] # for filtering binary labels |
| 259 | |
| 260 | # sanity check |
| 261 | # check that last node has no parent |
| 262 | # for t in train: |
| 263 | # assert(t[1][-1] == -1 and t[2][-1] == -1) |
| 264 | |
| 265 | for t in test: |
| 266 | add_idx_to_tree(t, 0) |
| 267 | test = [tree2list(t, -1, is_binary) for t in test] |
| 268 | if is_binary: |
| 269 | test = [t for t in test if t[3][-1] >= 0] # for filtering binary labels |
| 270 | |
| 271 | train = shuffle(train) |
| 272 | # train = train[:2000] |
| 273 | n_pos = sum(t[3][-1] for t in train) |
| 274 | # print("num pos train:", n_pos) |
| 275 | # idx2word = {v:k for k, v in word2idx.items()} |
| 276 | # for i in range(4): |
| 277 | # words, _, _, labels = train[i] |
| 278 | # print_sentence(words, idx2word) |
| 279 | # print("label:", labels[-1]) |
| 280 | test = shuffle(test) |
| 281 | test = test[:1000] |
| 282 | |
| 283 | V = len(word2idx) |
| 284 | print("vocab size:", V) |
| 285 | D = 10 |
| 286 | K = 2 if is_binary else 5 |
| 287 | |
| 288 | model = RecursiveNN(V, D, K) |
| 289 | model.fit(train, learning_rate=1e-2, reg=1e-2, mu=0, epochs=20, activation=T.tanh, train_inner_nodes=False) |
| 290 | print("train accuracy:", model.score(train)) |
| 291 | print("test accuracy:", model.score(test)) |
| 292 | |
| 293 | # make sure program doesn't end until we close the plot |
| 294 | plt.show() |
| 295 | |
| 296 | |
| 297 | if __name__ == '__main__': |
no test coverage detected