Esempio n. 1
0
    def testNonConvNet(self):
        self.setSeeds()

        x = np.random.randint(self.n_vocab,
                              size=(self.batch_size, self.max_seq_len))

        model = models.Sequential()

        # input: (batch_size, max_seq_len)
        # output: (batch_size, max_seq_len, n_word_dims)
        model.add(embeddings.Embedding(self.n_vocab, self.n_word_dims))
        model.compile(loss='mse', optimizer='sgd')
        expected_shape_l1 = (self.batch_size, self.max_seq_len,
                             self.n_word_dims)
        output_l1 = model.predict(x)
        self.assertEqual(expected_shape_l1, output_l1.shape)

        # input: (batch_size, max_seq_len, n_word_dims)
        # output: (batch_size, max_seq_len, n_filters * filter_width)
        model.add(
            core.TimeDistributedDense(self.n_word_dims,
                                      self.n_filters * self.filter_width))
        model.compile(loss='mse', optimizer='sgd')
        expected_shape_l2 = (self.batch_size, self.max_seq_len,
                             self.n_filters * self.filter_width)
        output_l2 = model.predict(x)
        self.assertEqual(expected_shape_l2, output_l2.shape)

        # input: (batch_size, max_seq_len, n_filters * filter_width)
        # output: (batch_size, n_filters, max_seq_len, filter_width)
        model.add(SplitOutputByFilter(self.n_filters, self.filter_width))
        model.compile(loss='mse', optimizer='sgd')
        expected_shape_l3 = (self.batch_size, self.n_filters, self.max_seq_len,
                             self.filter_width)
        output_l3 = model.predict(x)
        self.assertEqual(expected_shape_l3, output_l3.shape)

        # input: (batch_size, n_filters, max_seq_len, filter_width)
        # output: (batch_size, n_filters, filter_width, filter_width)
        model.add(
            SlidingWindowL2MaxPooling(self.batch_size, self.n_filters,
                                      self.filter_width, self.max_seq_len))
        model.compile(loss='mse', optimizer='sgd')
        expected_shape_l4 = (self.batch_size, self.n_filters,
                             self.filter_width, self.filter_width)
        output_l4 = model.predict(x)
        self.assertEqual(expected_shape_l4, output_l4.shape)

        # input: (batch_size, n_filters, filter_width, filter_width)
        # output: (batch_size, n_filters, filter_width, filter_width)
        model.add(
            ZeroFillDiagonals(self.batch_size, self.n_filters,
                              self.filter_width))
        model.compile(loss='mse', optimizer='sgd')
        expected_shape_l5 = (self.batch_size, self.n_filters,
                             self.filter_width, self.filter_width)
        output_l5 = model.predict(x)
        self.assertEqual(expected_shape_l5, output_l5.shape)
Esempio n. 2
0
def test_time_dist_dense():
    layer = core.TimeDistributedDense(10, input_shape=(None, 10))
    _runner(layer)
Esempio n. 3
0
def test_TimeDistributed():
    # first, test with Dense layer
    model = Sequential()
    model.add(wrappers.TimeDistributed(core.Dense(2), input_shape=(3, 4)))
    model.add(core.Activation('relu'))
    model.compile(optimizer='rmsprop', loss='mse')
    model.fit(np.random.random((10, 3, 4)),
              np.random.random((10, 3, 2)),
              nb_epoch=1,
              batch_size=10)

    # test config
    model.get_config()

    # compare to TimeDistributedDense
    test_input = np.random.random((1, 3, 4))
    test_output = model.predict(test_input)
    weights = model.layers[0].get_weights()

    reference = Sequential()
    reference.add(
        core.TimeDistributedDense(2, input_shape=(3, 4), weights=weights))
    reference.add(core.Activation('relu'))
    reference.compile(optimizer='rmsprop', loss='mse')

    reference_output = reference.predict(test_input)
    assert_allclose(test_output, reference_output, atol=1e-05)

    # test when specifying a batch_input_shape
    reference = Sequential()
    reference.add(
        core.TimeDistributedDense(2,
                                  batch_input_shape=(1, 3, 4),
                                  weights=weights))
    reference.add(core.Activation('relu'))
    reference.compile(optimizer='rmsprop', loss='mse')

    reference_output = reference.predict(test_input)
    assert_allclose(test_output, reference_output, atol=1e-05)

    # test with Convolution2D
    model = Sequential()
    model.add(
        wrappers.TimeDistributed(convolutional.Convolution2D(
            5, 2, 2, border_mode='same'),
                                 input_shape=(2, 4, 4, 3)))
    model.add(core.Activation('relu'))
    model.compile(optimizer='rmsprop', loss='mse')
    model.train_on_batch(np.random.random((1, 2, 4, 4, 3)),
                         np.random.random((1, 2, 4, 4, 5)))

    model = model_from_json(model.to_json())
    model.summary()

    # test stacked layers
    model = Sequential()
    model.add(wrappers.TimeDistributed(core.Dense(2), input_shape=(3, 4)))
    model.add(wrappers.TimeDistributed(core.Dense(3)))
    model.add(core.Activation('relu'))
    model.compile(optimizer='rmsprop', loss='mse')

    model.fit(np.random.random((10, 3, 4)),
              np.random.random((10, 3, 3)),
              nb_epoch=1,
              batch_size=10)

    # test wrapping Sequential model
    model = Sequential()
    model.add(core.Dense(3, input_dim=2))
    outer_model = Sequential()
    outer_model.add(wrappers.TimeDistributed(model, input_shape=(3, 2)))
    outer_model.compile(optimizer='rmsprop', loss='mse')
    outer_model.fit(np.random.random((10, 3, 2)),
                    np.random.random((10, 3, 3)),
                    nb_epoch=1,
                    batch_size=10)

    # test with functional API
    x = Input(shape=(3, 2))
    y = wrappers.TimeDistributed(model)(x)
    outer_model = Model(x, y)
    outer_model.compile(optimizer='rmsprop', loss='mse')
    outer_model.fit(np.random.random((10, 3, 2)),
                    np.random.random((10, 3, 3)),
                    nb_epoch=1,
                    batch_size=10)
Esempio n. 4
0
 def test_time_dist_dense(self):
     layer = core.TimeDistributedDense(10, 10)
     self._runner(layer)