dmlc / keras

Deep Learning library for Python. Convnets, recurrent neural networks, and more. Runs on MXNet, Theano or TensorFlow.
http://keras.io/
Other
125 stars 34 forks source link

Add causal padding to Atrous Convolution 1D #97

Closed dmadeka closed 6 years ago

dmadeka commented 6 years ago

@sandeep-krishnamurthy Fixes the bugs the last commit caused in output shape inference for a Lambda layer

dmadeka commented 6 years ago

@sandeep-krishnamurthy Btw, the pytest.ini had some broken params, so to test I had to comment out a few things (I didnt push those changes):

addopts=-v
        #-n 2
        #--durations=10
        #--cov-report term-missing
        #--cov=keras
sandeep-krishnamurthy commented 6 years ago

@dmadeka - You need to install test dependencies to get those options working, they are not broken.

dmadeka commented 6 years ago

Done! Had to modify a good bit because of that:

py.test tests/keras/layers/test_convolutional.py
============================================================================= slowest 10 test durations =============================================================================
39.37s call     tests/keras/layers/test_convolutional.py::test_atrous_conv_1d
28.68s call     tests/keras/layers/test_convolutional.py::test_upsampling_3d
24.44s call     tests/keras/layers/test_convolutional.py::test_deconvolution_2d
22.72s call     tests/keras/layers/test_convolutional.py::test_separable_conv_2d
11.94s call     tests/keras/layers/test_convolutional.py::test_convolution_3d
10.24s call     tests/keras/layers/test_convolutional.py::test_convolution_2d
9.39s call     tests/keras/layers/test_convolutional.py::test_convolution_1d
3.65s call     tests/keras/layers/test_convolutional.py::test_globalpooling_3d
2.44s call     tests/keras/layers/test_convolutional.py::test_maxpooling_1d
2.28s call     tests/keras/layers/test_convolutional.py::test_globalpooling_2d
====================================================================== 22 passed, 3 skipped in 114.08 seconds =======================================================================

Running the full suite now

dmadeka commented 6 years ago

Full test Failures

___________________________________________________________________________ test_temporal_classification ____________________________________________________________________________
[gw0] darwin -- Python 3.6.1 /Users/maded/anaconda/bin/python

    @keras_test
    def test_temporal_classification():
        '''
        Classify temporal sequences of float numbers
        of length 3 into 2 classes using
        single layer of GRU units and softmax applied
        to the last activations of the units
        '''
        (X_train, y_train), (X_test, y_test) = get_test_data(nb_train=500,
                                                             nb_test=500,
                                                             input_shape=(3, 5),
                                                             classification=True,
                                                             nb_class=2)
        y_train = to_categorical(y_train)
        y_test = to_categorical(y_test)

        model = Sequential()
        model.add(GRU(y_train.shape[-1],
                      input_shape=(X_train.shape[1], X_train.shape[2]),
                      activation='softmax'))
        model.compile(loss='categorical_crossentropy',
                      optimizer='adagrad',
                      metrics=['accuracy'])
        history = model.fit(X_train, y_train, nb_epoch=20, batch_size=32,
                            validation_data=(X_test, y_test),
                            verbose=0)
>       assert(history.history['val_acc'][-1] >= 0.8)
E       assert 0.7260000009536743 >= 0.8

tests/integration_tests/test_temporal_data_tasks.py:44: AssertionError
_______________________________________________________________________________ TestBackend.test_rnn ________________________________________________________________________________
[gw1] darwin -- Python 3.6.1 /Users/maded/anaconda/bin/python

self = <test_backends.TestBackend object at 0x1216b4da0>

    def test_rnn(self):
        # implement a simple RNN
        input_dim = 8
        output_dim = 4
        timesteps = 5

        input_val = np.random.random((32, timesteps, input_dim))
        init_state_val = np.random.random((32, output_dim))
        W_i_val = np.random.random((input_dim, output_dim))
        W_o_val = np.random.random((output_dim, output_dim))

        def rnn_step_fn(input_dim, output_dim, K):
            W_i = K.variable(W_i_val)
            W_o = K.variable(W_o_val)

            def step_function(x, states):
                assert len(states) == 1
                prev_output = states[0]
                output = K.dot(x, W_i) + K.dot(prev_output, W_o)
                return output, [output]
            return step_function

        # test default setup
        th_rnn_step_fn = rnn_step_fn(input_dim, output_dim, KTH)
        th_inputs = KTH.variable(input_val)
        th_initial_states = [KTH.variable(init_state_val)]
        last_output, outputs, new_states = KTH.rnn(th_rnn_step_fn, th_inputs,
                                                   th_initial_states,
                                                   go_backwards=False,
                                                   mask=None)
        th_last_output = KTH.eval(last_output)
        th_outputs = KTH.eval(outputs)
        assert len(new_states) == 1
        th_state = KTH.eval(new_states[0])

        tf_rnn_step_fn = rnn_step_fn(input_dim, output_dim, KTF)
        tf_inputs = KTF.variable(input_val)
        tf_initial_states = [KTF.variable(init_state_val)]
        last_output, outputs, new_states = KTF.rnn(tf_rnn_step_fn, tf_inputs,
                                                   tf_initial_states,
                                                   go_backwards=False,
                                                   mask=None)
        tf_last_output = KTF.eval(last_output)
        tf_outputs = KTF.eval(outputs)
        assert len(new_states) == 1
        tf_state = KTF.eval(new_states[0])

        assert_allclose(tf_last_output, th_last_output, atol=1e-04)
        assert_allclose(tf_outputs, th_outputs, atol=1e-04)
        assert_allclose(tf_state, th_state, atol=1e-04)

        # test unroll
        unrolled_last_output, unrolled_outputs, unrolled_new_states = KTH.rnn(
            th_rnn_step_fn, th_inputs,
            th_initial_states,
            go_backwards=False,
            mask=None,
            unroll=True,
            input_length=timesteps)

        unrolled_th_last_output = KTH.eval(unrolled_last_output)
        unrolled_th_outputs = KTH.eval(unrolled_outputs)
        assert len(unrolled_new_states) == 1
        unrolled_th_state = KTH.eval(unrolled_new_states[0])
        assert_allclose(th_last_output, unrolled_th_last_output, atol=1e-04)
        assert_allclose(th_outputs, unrolled_th_outputs, atol=1e-04)
        assert_allclose(th_state, unrolled_th_state, atol=1e-04)

        # test go_backwards
        th_rnn_step_fn = rnn_step_fn(input_dim, output_dim, KTH)
        th_inputs = KTH.variable(input_val)
        th_initial_states = [KTH.variable(init_state_val)]
        last_output, outputs, new_states = KTH.rnn(th_rnn_step_fn, th_inputs,
                                                   th_initial_states,
                                                   go_backwards=True,
                                                   mask=None)
        th_last_output = KTH.eval(last_output)
        th_outputs = KTH.eval(outputs)
        assert len(new_states) == 1
        th_state = KTH.eval(new_states[0])

        tf_rnn_step_fn = rnn_step_fn(input_dim, output_dim, KTF)
        tf_inputs = KTF.variable(input_val)
        tf_initial_states = [KTF.variable(init_state_val)]
        last_output, outputs, new_states = KTF.rnn(tf_rnn_step_fn, tf_inputs,
                                                   tf_initial_states,
                                                   go_backwards=True,
                                                   mask=None)
        tf_last_output = KTF.eval(last_output)
        tf_outputs = KTF.eval(outputs)
        assert len(new_states) == 1
        tf_state = KTF.eval(new_states[0])

        assert_allclose(tf_last_output, th_last_output, atol=1e-04)
        assert_allclose(tf_outputs, th_outputs, atol=1e-04)
        assert_allclose(tf_state, th_state, atol=1e-04)

        # test unroll with backwards = True
        bwd_last_output, bwd_outputs, bwd_new_states = KTH.rnn(
            th_rnn_step_fn, th_inputs,
            th_initial_states,
            go_backwards=True,
            mask=None)
        bwd_th_last_output = KTH.eval(bwd_last_output)
        bwd_th_outputs = KTH.eval(bwd_outputs)
        assert len(bwd_new_states) == 1
        bwd_th_state = KTH.eval(bwd_new_states[0])

        bwd_unrolled_last_output, bwd_unrolled_outputs, bwd_unrolled_new_states = KTH.rnn(
            th_rnn_step_fn, th_inputs,
            th_initial_states,
            go_backwards=True,
            mask=None,
            unroll=True,
            input_length=timesteps)

        bwd_unrolled_th_last_output = KTH.eval(bwd_unrolled_last_output)
        bwd_unrolled_th_outputs = KTH.eval(bwd_unrolled_outputs)
        assert len(bwd_unrolled_new_states) == 1
        bwd_unrolled_th_state = KTH.eval(bwd_unrolled_new_states[0])
        assert_allclose(bwd_th_last_output, bwd_unrolled_th_last_output, atol=1e-04)
        assert_allclose(bwd_th_outputs, bwd_unrolled_th_outputs, atol=1e-04)
        assert_allclose(bwd_th_state, bwd_unrolled_th_state, atol=1e-04)

pick 63894994 Fix bug in Lambda output layer shape
Fix bug in Lambda output layer shape
        # test unroll with masking
        np_mask = np.random.randint(2, size=(32, timesteps))
        th_mask = KTH.variable(np_mask)

        masked_last_output, masked_outputs, masked_new_states = KTH.rnn(
            th_rnn_step_fn, th_inputs,
            th_initial_states,
            go_backwards=False,
            mask=th_mask)
        masked_th_last_output = KTH.eval(masked_last_output)
        masked_th_outputs = KTH.eval(masked_outputs)
        assert len(masked_new_states) == 1
        masked_th_state = KTH.eval(masked_new_states[0])

        unrolled_masked_last_output, unrolled_masked_outputs, unrolled_masked_new_states = KTH.rnn(
            th_rnn_step_fn, th_inputs,
            th_initial_states,
            go_backwards=False,
            mask=th_mask,
            unroll=True,
            input_length=timesteps)
        unrolled_masked_th_last_output = KTH.eval(unrolled_masked_last_output)
        unrolled_masked_th_outputs = KTH.eval(unrolled_masked_outputs)
        assert len(unrolled_masked_new_states) == 1
        unrolled_masked_th_state = KTH.eval(unrolled_masked_new_states[0])

>       assert_allclose(unrolled_masked_th_last_output, masked_th_last_output, atol=1e-04)
E       AssertionError:
E       Not equal to tolerance rtol=1e-07, atol=0.0001
E
E       (mismatch 6.25%)
E        x: array([[16.388945, 19.627626, 19.422802, 20.497753],
E              [27.255352, 32.8809  , 33.373707, 36.79008 ],
E              [ 6.731161,  7.887591,  8.563793,  8.687819],...
E        y: array([[1.638894e+01, 1.962763e+01, 1.942280e+01, 2.049775e+01],
E              [2.725535e+01, 3.288090e+01, 3.337371e+01, 3.679008e+01],
E              [6.731161e+00, 7.887591e+00, 8.563793e+00, 8.687819e+00],...

tests/keras/backend/test_backends.py:459: AssertionError
====================================================== 2 failed, 218 passed, 35 skipped, 6 xpassed, 2 error in 896.64 seconds =======================================================