def test_shape_of_output_of_6d_rnn(self): units = 7 last_dim_size = 12 rnn = MDRNN(units=units, input_shape=(None, None, None, None, None, None, last_dim_size), return_sequences=True, activation='tanh') x = tf.zeros(shape=(2, 3, 1, 2, 2, 1, 5, last_dim_size)) result = rnn.call(x) self.assertEqual((2, 3, 1, 2, 2, 1, 5, units), result.shape)
def test_shape(self): rnn2d = MDRNN(units=5, input_shape=(None, None, 1), kernel_initializer=initializers.Constant(1), recurrent_initializer=initializers.Constant(1), bias_initializer=initializers.Constant(-1), return_sequences=True, activation='tanh') x = np.arange(6).reshape((1, 2, 3, 1)) res = rnn2d.call(x) self.assertEqual((1, 2, 3, 5), res.shape)
def test_for_two_step_sequence(self): kernel_initializer = initializers.Zeros() recurrent_initializer = kernel_initializer mdrnn = MDRNN(units=2, input_shape=(None, 1), kernel_initializer=kernel_initializer, recurrent_initializer=recurrent_initializer, bias_initializer=initializers.Constant(5), return_sequences=True) x = np.zeros((1, 3, 1)) a = mdrnn.call(x) expected_result = np.ones((1, 3, 2)) * 0.9999 np.testing.assert_almost_equal(expected_result, a.numpy(), 4)
def test_result(self): rnn2d = MDRNN(units=1, input_shape=(None, None, 1), kernel_initializer=initializers.Identity(), recurrent_initializer=initializers.Identity(1), bias_initializer=initializers.Constant(-1), return_sequences=True, activation=None) x = np.arange(6).reshape((1, 2, 3, 1)) actual = rnn2d.call(x) desired = np.array([ [-1, -1, 0], [1, 3, 7] ]).reshape((1, 2, 3, 1)) np.testing.assert_almost_equal(desired, actual.numpy(), 6)
def test_2drnn_output_when_providing_initial_state(self): rnn2d = MDRNN(units=1, input_shape=(None, None, 1), kernel_initializer=initializers.Identity(), recurrent_initializer=initializers.Identity(1), bias_initializer=initializers.Constant(-1), return_sequences=True, activation=None) x = np.arange(6).reshape((1, 2, 3, 1)) initial_state = [tf.ones(shape=(1, 1)), tf.ones(shape=(1, 1))] actual = rnn2d.call(x, initial_state=initial_state) desired = np.array([ [1, 1, 2], [3, 7, 13] ]).reshape((1, 2, 3, 1)) np.testing.assert_almost_equal(desired, actual.numpy(), 6)
def test_result_after_running_rnn_on_3d_input(self): rnn3d = MDRNN(units=1, input_shape=(None, None, None, 1), kernel_initializer=initializers.Identity(), recurrent_initializer=initializers.Identity(1), bias_initializer=initializers.Constant(1), return_sequences=True, return_state=True, activation=None) x = np.arange(2 * 2 * 2).reshape((1, 2, 2, 2, 1)) outputs, state = rnn3d.call(x) desired = np.array([[[1, 3], [4, 11]], [[6, 15], [17, 51]]]).reshape( (1, 2, 2, 2, 1)) np.testing.assert_almost_equal(desired, outputs.numpy(), 6) desired_state = desired[:, -1, -1, -1] np.testing.assert_almost_equal(desired_state, state.numpy(), 6)
def test_1d_rnn_produces_correct_output_for_2_steps(self): kernel_initializer = initializers.identity() recurrent_initializer = kernel_initializer bias = 3 bias_initializer = initializers.Constant(bias) mdrnn = MDRNN(units=3, input_shape=(None, 3), kernel_initializer=kernel_initializer, recurrent_initializer=recurrent_initializer, bias_initializer=bias_initializer, activation=None, return_sequences=True) x1 = np.array([1, 2, 4]) x2 = np.array([9, 8, 6]) x = np.array([x1, x2]) a = mdrnn.call(x.reshape((1, 2, -1))) expected_result = np.array([[x1 + bias, x1 + x2 + 2 * bias]]) np.testing.assert_almost_equal(expected_result, a.numpy(), 8)
def test_feeding_layer_created_with_default_initializer(self): mdrnn = MDRNN(units=2, input_shape=(None, 1)) x = np.zeros((1, 1, 1)) * 0.5 mdrnn.call(x)