示例#1
0
    def test(self):
        direction = Direction(1, -1, -1, 1)

        position = (3, 5, 2, 1)
        actual = direction.get_previous_step_positions(position)
        expected = [(2, 5, 2, 1), (3, 6, 2, 1), (3, 5, 3, 1), (3, 5, 2, 0)]
        self.assertEqual(expected, actual)
示例#2
0
    def test_right_to_left(self):
        direction = Direction(-1)

        it = direction.iterate_over_positions(dim_lengths=[3])
        positions = list(it)

        expected = [(2,), (1,), (0,)]
        self.assertEqual(expected, positions)
 def get_expected_last_state(self):
     if self._direction == Direction.south_east():
         return self.south_east_state
     elif self._direction == Direction.south_west():
         return self.south_west_state
     elif self._direction == Direction.north_east():
         return self.north_east_state
     elif self._direction == Direction.north_west():
         return self.north_west_state
 def get_expected_result(self):
     if self._direction == Direction.south_east():
         return self.south_east_output
     elif self._direction == Direction.south_west():
         return self.south_west_output
     elif self._direction == Direction.north_east():
         return self.north_east_output
     elif self._direction == Direction.north_west():
         return self.north_west_output
示例#5
0
 def test_getting_8_directions_for_3_dimensional_space(self):
     directions = Direction.get_all_directions(ndims=3)
     expected = [Direction(1, 1, 1), Direction(1, 1, -1),
                 Direction(1, -1, 1), Direction(1, -1, -1),
                 Direction(-1, 1, 1), Direction(-1, 1, -1),
                 Direction(-1, -1, 1), Direction(-1, -1, -1)]
     self.assertEqual(expected, directions)
示例#6
0
def make_rnns(return_sequences, return_state, go_backwards=False):
    seed = 1
    kernel_initializer = initializers.glorot_uniform(seed)
    recurrent_initializer = initializers.he_normal(seed)
    bias_initializer = initializers.he_normal(seed)

    kwargs = dict(units=3,
                  input_shape=(None, 5),
                  activation='tanh',
                  recurrent_activation='sigmoid',
                  kernel_initializer=kernel_initializer,
                  recurrent_initializer=recurrent_initializer,
                  bias_initializer=bias_initializer,
                  return_sequences=return_sequences,
                  return_state=return_state)

    if go_backwards:
        kwargs.update(dict(direction=Direction(-1)))

    rnn = MDGRU(**kwargs)
    keras_rnn = tf.keras.layers.GRU(
        units=3,
        activation='tanh',
        recurrent_activation='sigmoid',
        implementation=1,
        kernel_initializer=kernel_initializer,
        recurrent_initializer=recurrent_initializer,
        bias_initializer=bias_initializer,
        return_sequences=return_sequences,
        reset_after=False,
        return_state=return_state,
        go_backwards=go_backwards)
    return rnn, keras_rnn
    def test_bidirectional_rnn_returns_result_with_correct_shape(self):
        rnn = self.create_mdrnn(direction=Direction(1))

        rnn = MultiDirectional(rnn)

        a = rnn.call(self.x, initial_state=self.initial_state, dtype=np.float)
        self.assertEqual((1, 3, 2), a.shape)
    def test_backward_direction(self):
        mdrnn = self.create_mdrnn(direction=Direction(-1))

        a = mdrnn.call(self.x, initial_state=self.initial_state, dtype=np.float)

        expected_result = np.array(self.backward_result).reshape((1, 3, 1))

        np.testing.assert_almost_equal(expected_result, a.numpy(), 8)
    def test_returns_list_of_correct_length(self):
        rnn_setup = Rnn2dTestSetup(direction=Direction.south_east())
        rnn = rnn_setup.make_rnn()

        rnn = MultiDirectional(rnn)
        x = rnn_setup.make_input()
        actual = rnn.call(x)

        # 1 element for output of RNN and 4 elements for states, 1 state per each direction
        self.assertEqual(5, len(actual))
    def test_bidirectional_rnn_returns_correct_result(self):
        rnn = self.create_mdrnn(direction=Direction(1))

        rnn = MultiDirectional(rnn)

        a = rnn.call(self.x, initial_state=self.initial_state, dtype=np.float)

        expected_result = np.array([
            [2, 8],
            [4, 4],
            [8, 2]
        ]).reshape((1, 3, 2))

        np.testing.assert_almost_equal(expected_result, a.numpy(), 8)
    def test_with_functor(self):
        rnn = self.create_mdrnn(direction=Direction(1))

        rnn = MultiDirectional(rnn)

        a = rnn(self.x, initial_state=self.initial_state)

        expected_result = np.array([
            [2, 8],
            [4, 4],
            [8, 2]
        ]).reshape((1, 3, 2))

        np.testing.assert_almost_equal(expected_result, a.numpy(), 8)
    def test_results(self):
        rnn_setup = Rnn2dTestSetup(direction=Direction.south_east())
        rnn = rnn_setup.make_rnn()

        rnn = MultiDirectional(rnn)
        x = rnn_setup.make_input()
        expected = rnn_setup.get_expected_result_for_multi_directional_rnn()
        actual = rnn.call(x)

        num_elements = 5
        for i in range(num_elements):
            actual_output = actual[i]
            expected_output = expected[i]
            np.testing.assert_almost_equal(expected_output,
                                           actual_output.numpy(), 6)
    def make_rnns(self, return_sequences, return_state, go_backwards=False):
        seed = 1
        kwargs = dict(units=3, input_shape=(None, 5),
                      kernel_initializer=initializers.glorot_uniform(seed),
                      recurrent_initializer=initializers.he_normal(seed),
                      bias_initializer=initializers.Constant(2),
                      return_sequences=return_sequences,
                      return_state=return_state,
                      activation='relu'
                      )

        mdrnn_kwargs = dict(kwargs)
        if go_backwards:
            mdrnn_kwargs.update(dict(direction=Direction(-1)))
        rnn = MDRNN(**mdrnn_kwargs)

        keras_rnn_kwargs = dict(kwargs)
        keras_rnn_kwargs.update(dict(go_backwards=go_backwards))
        keras_rnn = tf.keras.layers.SimpleRNN(**keras_rnn_kwargs)
        return rnn, keras_rnn
示例#14
0
 def test_with_valid_direction(self):
     self.assert_valid_direction(Direction(-1, 1))
示例#15
0
 def test_pass_invalid_direction_to_2drnn(self):
     self.assert_invalid_direction(Direction(1))
示例#16
0
 def test_cannot_instantiate_direction_with_arbitrary_number(self):
     self.assertRaises(TypeError, lambda: Direction(8))
     self.assertRaises(TypeError, lambda: Direction(0))
     self.assertRaises(TypeError, lambda: Direction(-2, 1, -1))
示例#17
0
 def test_length_of_direction_vector_must_match_dimensionality_of_input(self):
     self.assert_invalid_direction(Direction())
     self.assert_invalid_direction(Direction(1, 1))
示例#18
0
 def test_no_duplicate_directions(self):
     n = 7
     directions = Direction.get_all_directions(ndims=n)
     unique = set(directions)
     self.assertEqual(len(directions), len(unique))
示例#19
0
 def test_getting_2_to_the_n_directions_for_n_dimensional_space(self):
     n = 5
     directions = Direction.get_all_directions(ndims=n)
     self.assertEqual(2**n, len(directions))
示例#20
0
 def test_getting_2_directions_for_1_dimensional_space(self):
     directions = Direction.get_all_directions(ndims=1)
     self.assertEqual([Direction(1), Direction(-1)], directions)
示例#21
0
 def get_direction(self):
     return Direction(1, 1)
示例#22
0
 def test_going_right_to_left(self):
     direction = Direction(-1)
     position = (3,)
     self.assertEqual([(4,)], direction.get_previous_step_positions(position))
示例#23
0
 def create_direction(self):
     return Direction(-1, -1)
示例#24
0
 def create_direction(self):
     return Direction(1, 1)
示例#25
0
 def get_direction(self):
     return Direction(-1, -1)