Example #1
0
    def test_fprop_faster(self):
        seed = 1234
        repeat = 100

        lstm = LSTM(input_size=DATA['features_size'],
                    hidden_sizes=[DATA['hidden_size']],
                    )

        lstm.initialize(initer.UniformInitializer(seed))

        lstm2 = LSTMFaster(input_size=DATA['features_size'],
                           hidden_sizes=[DATA['hidden_size']],
                           )
        # Wi, Wo, Wf, Wm
        # Make sure the weights are the same.
        lstm2.layers_lstm[0].W.set_value(np.concatenate([lstm.layers_lstm[0].Wi.get_value(), lstm.layers_lstm[0].Wo.get_value(), lstm.layers_lstm[0].Wf.get_value(), lstm.layers_lstm[0].Wm.get_value()], axis=1))
        lstm2.layers_lstm[0].U.set_value(np.concatenate([lstm.layers_lstm[0].Ui.get_value(), lstm.layers_lstm[0].Uo.get_value(), lstm.layers_lstm[0].Uf.get_value(), lstm.layers_lstm[0].Um.get_value()], axis=1))

        input = T.tensor3('input')
        input.tag.test_value = DATA['batch']

        fprop = theano.function([input], lstm.get_output(input))
        fprop2 = theano.function([input], lstm2.get_output(input))
        fprop_time = measure("out = fprop(DATA['batch'])", repeat)
        print("fprop time: {:.2f} sec.", fprop_time)
        fprop2_time = measure("out = fprop2(DATA['batch'])", repeat)
        print("fprop faster time: {:.2f} sec.", fprop2_time)
        print("Speedup: {:.2f}x".format(fprop_time/fprop2_time))

        out = fprop(DATA['batch'])
        out2 = fprop2(DATA['batch'])
        assert_array_equal(out, out2)
Example #2
0
    def test_fprop_mask_vs_not_mask(self):
        activation = "tanh"
        seed = 1234
        repeat = 100

        lstm = LSTM(input_size=DATA['features_size'],
                    hidden_sizes=[DATA['hidden_size']],
                    )

        lstm.initialize(initer.UniformInitializer(seed))

        lstm2 = LSTMFast(input_size=DATA['features_size'],
                         hidden_sizes=[DATA['hidden_size']],
                         )
        lstm2.mask = sharedX(DATA['mask'])
        # Wi, Wo, Wf, Wm
        # Make sure the weights are the same.
        lstm2.layers_lstm[0].W.set_value(np.concatenate([lstm.layers_lstm[0].Wi.get_value(), lstm.layers_lstm[0].Wo.get_value(), lstm.layers_lstm[0].Wf.get_value(), lstm.layers_lstm[0].Wm.get_value()], axis=1))
        lstm2.layers_lstm[0].U.set_value(np.concatenate([lstm.layers_lstm[0].Ui.get_value(), lstm.layers_lstm[0].Uo.get_value(), lstm.layers_lstm[0].Uf.get_value(), lstm.layers_lstm[0].Um.get_value()], axis=1))

        input = T.tensor3('input')
        input.tag.test_value = DATA['batch']

        fprop = theano.function([input], lstm.get_output(input))
        fprop2 = theano.function([input], lstm2.get_output(input))
        # fprop_time = measure("out = fprop(DATA['batch'])", repeat)
        # print("fprop time: {:.2f} sec.", fprop_time)
        out = fprop(DATA['batch'])
        out2 = fprop2(DATA['batch'])

        assert_true(out.sum != out2.sum())
        assert_array_equal((out * DATA['mask'][:, :, None]),
                           (out2 * DATA['mask'][:, :, None]))
Example #3
0
    def test_fprop_mask_vs_not_mask(self):
        activation = "tanh"
        seed = 1234
        repeat = 100

        lstm = LSTM(
            input_size=DATA['features_size'],
            hidden_sizes=[DATA['hidden_size']],
        )

        lstm.initialize(initer.UniformInitializer(seed))

        lstm2 = LSTMFast(
            input_size=DATA['features_size'],
            hidden_sizes=[DATA['hidden_size']],
        )
        lstm2.mask = sharedX(DATA['mask'])
        # Wi, Wo, Wf, Wm
        # Make sure the weights are the same.
        lstm2.layers_lstm[0].W.set_value(
            np.concatenate([
                lstm.layers_lstm[0].Wi.get_value(),
                lstm.layers_lstm[0].Wo.get_value(),
                lstm.layers_lstm[0].Wf.get_value(),
                lstm.layers_lstm[0].Wm.get_value()
            ],
                           axis=1))
        lstm2.layers_lstm[0].U.set_value(
            np.concatenate([
                lstm.layers_lstm[0].Ui.get_value(),
                lstm.layers_lstm[0].Uo.get_value(),
                lstm.layers_lstm[0].Uf.get_value(),
                lstm.layers_lstm[0].Um.get_value()
            ],
                           axis=1))

        input = T.tensor3('input')
        input.tag.test_value = DATA['batch']

        fprop = theano.function([input], lstm.get_output(input))
        fprop2 = theano.function([input], lstm2.get_output(input))
        # fprop_time = measure("out = fprop(DATA['batch'])", repeat)
        # print("fprop time: {:.2f} sec.", fprop_time)
        out = fprop(DATA['batch'])
        out2 = fprop2(DATA['batch'])

        assert_true(out.sum != out2.sum())
        assert_array_equal((out * DATA['mask'][:, :, None]),
                           (out2 * DATA['mask'][:, :, None]))
Example #4
0
    def test_fprop_faster(self):
        seed = 1234
        repeat = 100

        lstm = LSTM(
            input_size=DATA['features_size'],
            hidden_sizes=[DATA['hidden_size']],
        )

        lstm.initialize(initer.UniformInitializer(seed))

        lstm2 = LSTMFaster(
            input_size=DATA['features_size'],
            hidden_sizes=[DATA['hidden_size']],
        )
        # Wi, Wo, Wf, Wm
        # Make sure the weights are the same.
        lstm2.layers_lstm[0].W.set_value(
            np.concatenate([
                lstm.layers_lstm[0].Wi.get_value(),
                lstm.layers_lstm[0].Wo.get_value(),
                lstm.layers_lstm[0].Wf.get_value(),
                lstm.layers_lstm[0].Wm.get_value()
            ],
                           axis=1))
        lstm2.layers_lstm[0].U.set_value(
            np.concatenate([
                lstm.layers_lstm[0].Ui.get_value(),
                lstm.layers_lstm[0].Uo.get_value(),
                lstm.layers_lstm[0].Uf.get_value(),
                lstm.layers_lstm[0].Um.get_value()
            ],
                           axis=1))

        input = T.tensor3('input')
        input.tag.test_value = DATA['batch']

        fprop = theano.function([input], lstm.get_output(input))
        fprop2 = theano.function([input], lstm2.get_output(input))
        fprop_time = measure("out = fprop(DATA['batch'])", repeat)
        print("fprop time: {:.2f} sec.", fprop_time)
        fprop2_time = measure("out = fprop2(DATA['batch'])", repeat)
        print("fprop faster time: {:.2f} sec.", fprop2_time)
        print("Speedup: {:.2f}x".format(fprop_time / fprop2_time))

        out = fprop(DATA['batch'])
        out2 = fprop2(DATA['batch'])
        assert_array_equal(out, out2)