x = T.imatrix('x') y = T.ivector('y') classifier = RNTN( x, y, vocab_size=5, embed_dim=3, label_n=5, ) x_input = np.asarray([[1, -1, -1], [2, -1, -1], [3, 1, 2]], dtype=np.int32) y_input = labels[1:4] original_embedding = classifier.embedding.get_value() classifier.update_embedding(x_input) new_embedding = classifier.embedding.get_value() assert_matrix_neq(original_embedding, new_embedding, "update_embeding") original_params = [p.get_value() for p in classifier.params] classifier.train(x_input, y_input) updated_params = [p for p in classifier.params] for op, up in zip(original_params, updated_params): assert_matrix_neq(op, up.get_value(), up.name)
x = T.imatrix('x') y = T.ivector('y') th_model = TheanoRNTN(x, y, vocab_size, embed_dim, label_n) np_model = NumpyRNTN.load_from_theano_model( th_model, word2id) # (embedding = th_model.embedding.get_value(), # rntn_layer = RNTNLayer(th_model.rntn_layer.V.get_value(), th_model.rntn_layer.W.get_value()), # logreg_layer = LogisticRegression(th_model.logreg_layer.W.get_value(), th_model.logreg_layer.b.get_value()), # word2id = word2id) x_input = np.asarray([[4, 2, 5], [3, 1, 4]], dtype=np.int32) tree_input = (5, "love", (3, (3, "you"), (3, "bro"))) actual = np_model.get_node_vector(tree_input) th_model.update_embedding(x_input) expected = th_model.embedding.get_value()[3] assert_matrix_eq(actual, expected, "node vector") get_label = theano.function(inputs=[x], outputs=th_model.logreg_layer.pred_y) score = np_model.predict_top_node(tree_input) assert isinstance(score, np.int64) assert_matrix_eq(score, get_label(x_input[1:2, :]), 'logreg.predict')
classifier = RNTN( x, y, vocab_size = 5, embed_dim = 3, label_n = 5, ) x_input = np.asarray([[1,-1,-1], [2,-1,-1], [3, 1, 2]], dtype=np.int32) y_input = labels[1:4] original_embedding = classifier.embedding.get_value() classifier.update_embedding(x_input) new_embedding = classifier.embedding.get_value() assert_matrix_neq(original_embedding, new_embedding, "update_embeding") original_params = [p.get_value() for p in classifier.params] classifier.train(x_input, y_input) updated_params = [p for p in classifier.params] for op, up in zip(original_params, updated_params): assert_matrix_neq(op, up.get_value(), up.name)
th_model = TheanoRNTN(x, y, vocab_size, embed_dim, label_n) np_model = NumpyRNTN.load_from_theano_model(th_model, word2id)# (embedding = th_model.embedding.get_value(), # rntn_layer = RNTNLayer(th_model.rntn_layer.V.get_value(), th_model.rntn_layer.W.get_value()), # logreg_layer = LogisticRegression(th_model.logreg_layer.W.get_value(), th_model.logreg_layer.b.get_value()), # word2id = word2id) x_input = np.asarray([[4, 2, 5], [3, 1, 4]], dtype=np.int32) tree_input = (5, "love", (3, (3, "you"), (3, "bro"))) actual = np_model.get_node_vector(tree_input) th_model.update_embedding(x_input) expected = th_model.embedding.get_value()[3] assert_matrix_eq(actual, expected, "node vector") get_label = theano.function(inputs = [x], outputs = th_model.logreg_layer.pred_y) score = np_model.predict_top_node(tree_input) assert isinstance(score, np.int64) assert_matrix_eq(score, get_label(x_input[1:2,:]), 'logreg.predict')