feature_n_prev = 3 input_feature_map = np.random.rand(2, feature_n_prev, 2, 2) # shape: (2,3,2,2) filters = np.random.rand(2, feature_n_prev, 2, 2) numpy_output = conv2d(input_feature_map, filters) print numpy_output ######################## # Theano part# ######################## import theano input_feature_map_sym = theano.tensor.dtensor4("input_feature_map") filters_sym = theano.tensor.dtensor4("filters") f = theano.function( inputs=[input_feature_map_sym, filters_sym], outputs=theano.tensor.nnet.conv.conv2d(input_feature_map_sym, filters_sym, border_mode="full")) theano_output = f(input_feature_map, filters) print theano_output assert_matrix_eq(numpy_output, theano_output, "Conv2d")
##################### Testing #################### from test_util import (assert_matrix_eq, assert_about_eq) x = np.asarray(np.random.randint(vocab_size, size = (3, 6)), dtype=np.int32 ) y = np.asarray(np.random.randint(5, size = 3), dtype=np.int32 ) ########### Embedding layer ############## actual = f_el(x) expected = dcnn.e_layer.output(x) assert_matrix_eq(actual, expected, "Embedding") ########## Conv layer ################### actual = dcnn._c_layer_output(x) expected = f_cl(x) assert_matrix_eq(actual, expected, "Conv") ########## Output layer ################### actual = dcnn._p_y_given_x(x) expected = f3(x) assert_matrix_eq(actual, expected, "p_y_given_x") ########## errors ###########
layer = TheanoConvFoldingPoolLayer(rng = np.random.RandomState(1234), input = x_symbol, filter_shape = filter_shape, k = k, activation = "tanh", norm_w = True, fold = fold, W = theano.shared(value = W, borrow = True, name="W" ), b = theano.shared(value = b, borrow = True, name="b" ) ) f = theano.function(inputs = [x_symbol], outputs = layer.output) ########## Test ################ x = np.asarray(np.random.rand(2,2,3,3), dtype=theano.config.floatX) actual = np_layer.output(x) expected = f(x) assert_matrix_eq(actual, expected, "Conv")
np_l = LogisticRegression(W, b) ######################### # THEANO PART ######################### x_symbol = theano.tensor.dmatrix('x') y_symbol = theano.tensor.ivector('y') th_l = TheanoLogisticRegression(rng=np.random.RandomState(1234), input=x_symbol, n_in=10, n_out=5, W=theano.shared(value=W, name="W"), b=theano.shared(value=b, name="b")) f1 = theano.function(inputs=[x_symbol, y_symbol], outputs=th_l.nnl(y_symbol)) actual = np_l.nnl(x, y) expected = f1(x, y) assert_matrix_eq(actual, expected, "nnl") f2 = theano.function(inputs=[x_symbol, y_symbol], outputs=th_l.errors(y_symbol)) actual = np_l.errors(x, y) expected = f2(x, y) assert_matrix_eq(actual, expected, "errors")
3, V=theano.shared(value=V_val, name="V", borrow=True), W=theano.shared(value=W_val, name="W", borrow=True)) left_input = np.asarray([[0, 0, 1]], dtype=theano.config.floatX) right_input = np.asarray([[0, 1, 0]], dtype=theano.config.floatX) ################ # NUMPY IMPML ## ################ from recnn import RNTNLayer as NumpyRNTNLayer numpy_l = NumpyRNTNLayer(theano_l.V.get_value(), theano_l.W.get_value()) actual = numpy_l.output(left_input, right_input) actual1 = numpy_l.output(np.squeeze(left_input), np.squeeze(right_input)) #passing 1d array ################ # THEANO PART # ################ left = theano.tensor.drow("left") right = theano.tensor.drow("right") f = theano.function(inputs=[left, right], outputs=theano_l.output(left, right)) expected = f(left_input, right_input) assert_matrix_eq(actual, expected, "output") assert_matrix_eq(actual1, expected, "output(1d passed in)")
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')
p.b_logreg = b_logreg dcnn = DCNN(p) ##################### Testing #################### from test_util import (assert_matrix_eq, assert_about_eq) x = np.asarray(np.random.randint(vocab_size, size=(3, 6)), dtype=np.int32) y = np.asarray(np.random.randint(5, size=3), dtype=np.int32) ########### Embedding layer ############## actual = f_el(x) expected = dcnn.e_layer.output(x) assert_matrix_eq(actual, expected, "Embedding") ########## Conv layer ################### actual = dcnn._c_layer_output(x) expected = f_cl(x) assert_matrix_eq(actual, expected, "Conv") ########## Output layer ################### actual = dcnn._p_y_given_x(x) expected = f3(x) assert_matrix_eq(actual, expected, "p_y_given_x") ########## errors ########### actual = dcnn._errors(x, y) expected = f2(x, y)
grads = theano.function(inputs = dummy_params, outputs = [T.grad(dummy_cost, param) for param in dummy_params]) def numpy_update(xs, egs, exs): grad_xs = grads(*xs) new_egs = [rho * eg + (1-rho) * (grad_x ** 2) for grad_x, eg in zip(grad_xs, egs)] delta_xs = [- np.sqrt(ex + epsilon) / np.sqrt(new_eg + epsilon) * grad_x for grad_x, new_eg, ex in zip(grad_xs, new_egs, exs)] new_exs = [rho * ex + (1-rho) * (delta_x ** 2) for delta_x, ex in zip(delta_xs, exs)] new_xs = [x + delta_x for x, delta_x in zip(xs, delta_xs)] return new_xs, new_egs, new_exs egs = [np.zeros(shape, dtype = theano.config.floatX) for shape in param_shapes] exs = [np.zeros(shape, dtype = theano.config.floatX) for shape in param_shapes] for i in range(n_iter): np_params, egs, exs = numpy_update(np_params, egs, exs) # if the updates are te same for np_param, param in zip(np_params, params): assert_matrix_eq(np_param, param.get_value(), param.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')
feature_n_prev = 3 input_feature_map = np.random.rand(2,feature_n_prev,2,2) # shape: (2,3,2,2) filters = np.random.rand(2,feature_n_prev,2,2) numpy_output = conv2d(input_feature_map, filters) print numpy_output ######################## # Theano part# ######################## import theano input_feature_map_sym = theano.tensor.dtensor4("input_feature_map") filters_sym = theano.tensor.dtensor4("filters") f = theano.function(inputs = [input_feature_map_sym, filters_sym], outputs = theano.tensor.nnet.conv.conv2d(input_feature_map_sym, filters_sym, border_mode = "full") ) theano_output = f(input_feature_map, filters) print theano_output assert_matrix_eq(numpy_output, theano_output, "Conv2d")
th_l = TheanoLogisticRegression(rng = np.random.RandomState(1234), input = x_symbol, n_in = 10, n_out = 5, W = theano.shared(value = W, name = "W"), b = theano.shared(value = b, name = "b") ) f1 = theano.function(inputs = [x_symbol, y_symbol], outputs = th_l.nnl(y_symbol) ) actual = np_l.nnl(x, y) expected = f1(x, y) assert_matrix_eq(actual, expected, "nnl") f2 = theano.function(inputs = [x_symbol, y_symbol], outputs = th_l.errors(y_symbol) ) actual = np_l.errors(x, y) expected = f2(x, y) assert_matrix_eq(actual, expected, "errors")
grads = theano.function(inputs = dummy_params, outputs = [T.grad(dummy_cost, param) for param in dummy_params]) def numpy_update(xs, egs, exs): grad_xs = grads(*xs) new_egs = [rho * eg + (1-rho) * (grad_x ** 2) for grad_x, eg in zip(grad_xs, egs)] delta_xs = [- np.sqrt(ex + epsilon) / np.sqrt(new_eg + epsilon) * grad_x for grad_x, new_eg, ex in zip(grad_xs, new_egs, exs)] new_exs = [rho * ex + (1-rho) * (delta_x ** 2) for delta_x, ex in zip(delta_xs, exs)] new_xs = [x + delta_x for x, delta_x in zip(xs, delta_xs)] return new_xs, new_egs, new_exs egs = [np.zeros(shape, dtype = theano.config.floatX) for shape in param_shapes] exs = [np.zeros(shape, dtype = theano.config.floatX) for shape in param_shapes] for i in xrange(n_iter): np_params, egs, exs = numpy_update(np_params, egs, exs) # if the updates are te same for np_param, param in zip(np_params, params): assert_matrix_eq(np_param, param.get_value(), param.name)
left_input = np.asarray([[0, 0, 1]], dtype=theano.config.floatX) right_input = np.asarray([[0, 1, 0]], dtype=theano.config.floatX) ################ # NUMPY IMPML ## ################ from recnn import RNTNLayer as NumpyRNTNLayer numpy_l = NumpyRNTNLayer(theano_l.V.get_value(), theano_l.W.get_value()) actual = numpy_l.output(left_input, right_input) actual1 = numpy_l.output(np.squeeze(left_input), np.squeeze(right_input)) # passing 1d array ################ # THEANO PART # ################ left = theano.tensor.drow("left") right = theano.tensor.drow("right") f = theano.function(inputs=[left, right], outputs=theano_l.output(left, right)) expected = f(left_input, right_input) assert_matrix_eq(actual, expected, "output") assert_matrix_eq(actual1, expected, "output(1d passed in)")
vocab_size, embed_dm = 10, 5 embeddings = np.random.rand(vocab_size, embed_dm) sents = np.asarray(np.random.randint(10, size = (3, 6)), dtype = np.int32) np_l = WordEmbeddingLayer(embeddings) actual = np_l.output(sents) ########### THEANO ########### x_symbol = theano.tensor.imatrix('x') # the word indices matrix th_l = TheanoWordEmbeddingLayer(rng = np.random.RandomState(1234), input = x_symbol, vocab_size = vocab_size, embed_dm = embed_dm, embeddings = theano.shared(value = embeddings, name = "embeddings" ) ) f = theano.function(inputs = [x_symbol], outputs = th_l.output) expected = f(sents) assert_matrix_eq(actual, expected, "Embedding")
import numpy as np from dcnn import WordEmbeddingLayer from dcnn_train import WordEmbeddingLayer as TheanoWordEmbeddingLayer from test_util import assert_matrix_eq ########### NUMPY ########### vocab_size, embed_dm = 10, 5 embeddings = np.random.rand(vocab_size, embed_dm) sents = np.asarray(np.random.randint(10, size=(3, 6)), dtype=np.int32) np_l = WordEmbeddingLayer(embeddings) actual = np_l.output(sents) ########### THEANO ########### x_symbol = theano.tensor.imatrix('x') # the word indices matrix th_l = TheanoWordEmbeddingLayer(rng=np.random.RandomState(1234), input=x_symbol, vocab_size=vocab_size, embed_dm=embed_dm, embeddings=theano.shared(value=embeddings, name="embeddings")) f = theano.function(inputs=[x_symbol], outputs=th_l.output) expected = f(sents) assert_matrix_eq(actual, expected, "Embedding")
######################### ## THEANO PART ######################### from dcnn_train import ConvFoldingPoolLayer as TheanoConvFoldingPoolLayer x_symbol = theano.tensor.dtensor4('x') layer = TheanoConvFoldingPoolLayer(rng=np.random.RandomState(1234), input=x_symbol, filter_shape=filter_shape, k=k, activation="tanh", norm_w=True, fold=fold, W=theano.shared(value=W, borrow=True, name="W"), b=theano.shared(value=b, borrow=True, name="b")) f = theano.function(inputs=[x_symbol], outputs=layer.output) ########## Test ################ x = np.asarray(np.random.rand(2, 2, 3, 3), dtype=theano.config.floatX) actual = np_layer.output(x) expected = f(x) assert_matrix_eq(actual, expected, "Conv")