def test_NNET_train(self): struct = 'w2vec(/project/nakamura-lab01/Work/truong-dq/chainer/vidnn/exp/word2vec_truecase_200/vectors.bin):lstm(200-2):linear(2-2)' # struct = 'embed(2-200):lstm(200-2):linear(2-2)' nnet = NNET_Model.parse_structure(struct) # Testing variable test_var = chainer.Variable(np.asarray([1], dtype=np.int32)) output_before_train = nnet(test_var).data inp = chainer.Variable(np.asarray([1], dtype=np.int32)) target = chainer.Variable(np.asarray([0], dtype=np.int32)) output = nnet(inp) loss = F.softmax_cross_entropy(output, target) optimizer = optimizers.SGD(lr=0.1) optimizer.setup(nnet) optimizer.zero_grads() loss.backward() optimizer.update() inp = chainer.Variable(np.asarray([0], dtype=np.int32)) target = chainer.Variable(np.asarray([0], dtype=np.int32)) output = nnet(inp) loss = F.softmax_cross_entropy(output, target) optimizer = optimizers.SGD(lr=0.1) optimizer.setup(nnet) optimizer.zero_grads() loss.backward() optimizer.update() nnet.save('test_output') nnet_2 = NNET_Model.load('test_output') nnet.forget_history() nnet_2.forget_history() np.testing.assert_equal(nnet[0][1].W.data, nnet_2[0][1].W.data) np.testing.assert_equal(nnet[1][1].upward.W.data, nnet_2[1][1].upward.W.data) np.testing.assert_equal(nnet[1][1].lateral.W.data, nnet_2[1][1].lateral.W.data) np.testing.assert_equal(nnet[1][1].upward.b.data, nnet_2[1][1].upward.b.data) output_after_train = nnet(test_var).data output_after_load = nnet_2(test_var).data after_first_layer_nnet = nnet[0][1](test_var) after_first_layer_nnet_2 = nnet_2[0][1](test_var) np.testing.assert_equal(after_first_layer_nnet.data, after_first_layer_nnet_2.data) after_first_layer_nnet.volatile = False after_first_layer_nnet_2.volatile = False after_second_layer_nnet = nnet[1][1](after_first_layer_nnet) after_second_layer_nnet_2 = nnet_2[1][1](after_first_layer_nnet_2) np.testing.assert_equal(after_second_layer_nnet.data, after_second_layer_nnet_2.data) assert (output_before_train != output_after_train).any() assert (output_before_train != output_after_load).any() np.testing.assert_equal(output_after_train, output_after_load)
def decode_mode(): nnet_model = NNET_Model.load(opts.fname_in_model) for row, words in zip(data, text_data): row_result = [] for w in row: x = Variable(xp.asarray([w], dtype=np.int32)) row_result.append(nnet_model(x).data.argmax(axis=1)[0]) if opts.forget_on_new_utt: nnet_model.forget_history() nice_print(row_result, words)
def test_NNET_save(self): struct = 'embed(2-2):lstm(2-2)' nnet = NNET_Model.parse_structure(struct) nnet.save('test_output') nnet2 = NNET_Model.load('test_output') inp = chainer.Variable(np.asarray([1], dtype=np.int32)) np.testing.assert_equal(nnet[0][1].W.data, nnet2[0][1].W.data) np.testing.assert_equal(nnet[0][1](inp).data, nnet2[0][1](inp).data) np.testing.assert_equal(nnet(inp).data, nnet2(inp).data) struct = 'embed(2-2):lstm(2-3):linear(3-2)' nnet = NNET_Model.parse_structure(struct) nnet.save('test_output') nnet2 = NNET_Model.load('test_output') inp = chainer.Variable(np.asarray([1], dtype=np.int32)) np.testing.assert_equal(nnet(inp).data, nnet2(inp).data)
def test_NNET_load(self): struct = 'linear(2-4):sigmoid:tanh:dropout:lstm(10-10)' nnet = NNET_Model.parse_structure(struct) nnet.save('test_save_model') another = NNET_Model.load('test_save_model')
def test_NNET_parse_struct(self): struct = 'linear(2-4):sigmoid:tanh:dropout:lstm(10-10)' nnet = NNET_Model.parse_structure(struct) self.assertIsInstance(nnet['linear(2-4)-c0'], L.Linear) self.assertIsInstance(nnet['lstm(10-10)-c4'], L.LSTM)
def setUp(self): np.random.seed(0) if not os.path.exists('test_save_model'): os.makedirs('test_save_model') self.nnet_model = NNET_Model() pass
class Test_NNET_Model(unittest.TestCase): def setUp(self): np.random.seed(0) if not os.path.exists('test_save_model'): os.makedirs('test_save_model') self.nnet_model = NNET_Model() pass def test_NNET_add_comp(self): self.nnet_model.add_component('linear(2-2)', L.Linear(2, 2)) def test_NNET_parse_struct(self): struct = 'linear(2-4):sigmoid:tanh:dropout:lstm(10-10)' nnet = NNET_Model.parse_structure(struct) self.assertIsInstance(nnet['linear(2-4)-c0'], L.Linear) self.assertIsInstance(nnet['lstm(10-10)-c4'], L.LSTM) def test_NNET_load(self): struct = 'linear(2-4):sigmoid:tanh:dropout:lstm(10-10)' nnet = NNET_Model.parse_structure(struct) nnet.save('test_save_model') another = NNET_Model.load('test_save_model') def test_NNET_save(self): struct = 'embed(2-2):lstm(2-2)' nnet = NNET_Model.parse_structure(struct) nnet.save('test_output') nnet2 = NNET_Model.load('test_output') inp = chainer.Variable(np.asarray([1], dtype=np.int32)) np.testing.assert_equal(nnet[0][1].W.data, nnet2[0][1].W.data) np.testing.assert_equal(nnet[0][1](inp).data, nnet2[0][1](inp).data) np.testing.assert_equal(nnet(inp).data, nnet2(inp).data) struct = 'embed(2-2):lstm(2-3):linear(3-2)' nnet = NNET_Model.parse_structure(struct) nnet.save('test_output') nnet2 = NNET_Model.load('test_output') inp = chainer.Variable(np.asarray([1], dtype=np.int32)) np.testing.assert_equal(nnet(inp).data, nnet2(inp).data) def test_NNET_train(self): xp = cuda.cupy # struct = 'w2vec(/project/nakamura-lab01/Work/truong-dq/chainer/vidnn/exp/word2vec_truecase_200/vectors.bin):lstm(200-2):linear(2-2)' struct = 'embed(2-200):lstm(200-2):linear(2-2)' nnet = NNET_Model.parse_structure(struct) nnet.to_gpu() # Testing variable test_var = chainer.Variable(xp.asarray([1], dtype=np.int32)) output_before_train = cuda.to_cpu(nnet(test_var).data) inp = chainer.Variable(xp.asarray([1], dtype=np.int32)) target = chainer.Variable(xp.asarray([0], dtype=np.int32)) output = nnet(inp) loss = F.softmax_cross_entropy(output, target) optimizer = optimizers.SGD(lr=0.1) optimizer.setup(nnet) optimizer.zero_grads() loss.backward() optimizer.update() inp = chainer.Variable(xp.asarray([0], dtype=np.int32)) target = chainer.Variable(xp.asarray([0], dtype=np.int32)) output = nnet(inp) loss = F.softmax_cross_entropy(output, target) optimizer = optimizers.SGD(lr=0.1) optimizer.setup(nnet) optimizer.zero_grads() loss.backward() optimizer.update() nnet.save('test_output') nnet.to_gpu() nnet_2 = NNET_Model.load('test_output') nnet_2.to_gpu() nnet.forget_history() nnet_2.forget_history() np.testing.assert_equal(cuda.to_cpu(nnet[0][1].W.data), cuda.to_cpu(nnet_2[0][1].W.data)) np.testing.assert_equal(cuda.to_cpu(nnet[1][1].upward.W.data), cuda.to_cpu(nnet_2[1][1].upward.W.data)) np.testing.assert_equal(cuda.to_cpu(nnet[1][1].lateral.W.data), cuda.to_cpu(nnet_2[1][1].lateral.W.data)) np.testing.assert_equal(cuda.to_cpu(nnet[1][1].upward.b.data), cuda.to_cpu(nnet_2[1][1].upward.b.data)) output_after_train = cuda.to_cpu(nnet(test_var).data) output_after_load = cuda.to_cpu(nnet_2(test_var).data) after_first_layer_nnet = nnet[0][1](test_var) after_first_layer_nnet_2 = nnet_2[0][1](test_var) np.testing.assert_equal(cuda.to_cpu(after_first_layer_nnet.data), cuda.to_cpu(after_first_layer_nnet_2.data)) after_first_layer_nnet.volatile = False after_first_layer_nnet_2.volatile = False after_second_layer_nnet = nnet[1][1](after_first_layer_nnet) after_second_layer_nnet_2 = nnet_2[1][1](after_first_layer_nnet_2) np.testing.assert_equal(cuda.to_cpu(after_second_layer_nnet.data), cuda.to_cpu(after_second_layer_nnet_2.data)) assert (output_before_train != output_after_train).any() assert (output_before_train != output_after_load).any() np.testing.assert_equal(output_after_train, output_after_load) # def test_NNET_W2vec(self): # np.random.seed(0) # struct = 'w2vec(/project/nakamura-lab01/Work/truong-dq/chainer/vidnn/exp/word2vec_truecase_200/vectors.bin):lstm(200-3):linear(3-2)' # nnet = NNET_Model.parse_structure(struct) # nnet.save('test_save_model_w2vec') # another = NNET_Model.load('test_save_model_w2vec') # inp = chainer.Variable(np.asarray([1], dtype=np.int32)) # np.testing.assert_equal(nnet(inp).data, another(inp).data) def tearDown(self): pass