Exemplo n.º 1
0
    def test_stateful_lstm2(self):
        weights = np.ones(
            (self.lstm.get_shape_in(), self.lstm.get_shape_out()))
        for _ in range(10):
            X = np.random.normal(size=(10, 1, 1))

            for s in range(1, 3):
                state = np.zeros((1, self.lstm.get_shape_out()))
                out = np.zeros((1, self.lstm.get_shape_out()))
                for i in range(s):
                    out, state = self.lstm_forward(X[i], out, state, weights)

                lstm = Sequence(Vector(1, 1), s) >> LSTM(1, 1, stateful=True)
                self.set_weights(lstm.right, 1)
                lstm_out = lstm.predict(X[:s])[-1]
                lstm_hidden = T.get_value(lstm.right.states[0])
                lstm_state = T.get_value(lstm.right.states[1])

                np.testing.assert_almost_equal(lstm_hidden, lstm_out, 5)
                np.testing.assert_almost_equal(lstm_out, out, 5)
                np.testing.assert_almost_equal(out, lstm_hidden, 5)
                np.testing.assert_almost_equal(lstm_state, state, 5)
Exemplo n.º 2
0
    def test_stateful_lstm2(self):
        weights = np.ones((self.lstm.get_shape_in(), self.lstm.get_shape_out()))
        for _ in range(10):
            X = np.random.normal(size=(10, 1, 1))

            for s in range(1, 3):
                state = np.zeros((1, self.lstm.get_shape_out()))
                out = np.zeros((1, self.lstm.get_shape_out()))
                for i in range(s):
                    out, state = self.lstm_forward(X[i], out, state, weights)

                lstm = Sequence(Vector(1, 1), s) >> LSTM(1, 1, stateful=True)
                self.set_weights(lstm.right, 1)
                lstm_out = lstm.predict(X[:s])[-1]
                lstm_hidden = T.get_value(lstm.right.states[0])
                lstm_state = T.get_value(lstm.right.states[1])


                np.testing.assert_almost_equal(lstm_hidden, lstm_out, 5)
                np.testing.assert_almost_equal(lstm_out, out, 5)
                np.testing.assert_almost_equal(out, lstm_hidden, 5)
                np.testing.assert_almost_equal(lstm_state, state, 5)
Exemplo n.º 3
0
    def test_stateful_lstm(self):
        self.lstm.reset_states()
        self.set_weights(self.lstm.right, 2)

        X = np.random.normal(size=(1, 1, 1))
        weights = np.ones((self.lstm.get_shape_in(), self.lstm.get_shape_out())) * 2
        state = np.zeros((1, self.lstm.get_shape_out()))
        out = np.zeros((1, self.lstm.get_shape_out()))

        for i in range(1000):
            out, state = self.lstm_forward(X, out, state, weights)

            lstm_out = self.lstm.predict(X)
            lstm_state = T.get_value(self.lstm.right.states[1])

            np.testing.assert_almost_equal(lstm_out[0], out[0], 4)
            np.testing.assert_almost_equal(lstm_state, state[0], 4)
Exemplo n.º 4
0
    def test_stateful_lstm(self):
        self.lstm.reset_states()
        self.set_weights(self.lstm.right, 2)

        X = np.random.normal(size=(1, 1, 1))
        weights = np.ones(
            (self.lstm.get_shape_in(), self.lstm.get_shape_out())) * 2
        state = np.zeros((1, self.lstm.get_shape_out()))
        out = np.zeros((1, self.lstm.get_shape_out()))

        for i in range(1000):
            out, state = self.lstm_forward(X, out, state, weights)

            lstm_out = self.lstm.predict(X)
            lstm_state = T.get_value(self.lstm.right.states[1])

            np.testing.assert_almost_equal(lstm_out[0], out[0], 4)
            np.testing.assert_almost_equal(lstm_state, state[0], 4)