def test_non_zero(): '''Test masking with non-zero mask value''' layer = core.Masking(5) func = K.function([layer.input], [layer.get_output_mask()]) input_data = np.array([[[1, 1], [2, 1], [3, 1], [5, 5]], [[1, 5], [5, 0], [0, 0], [0, 0]]], dtype=np.int32) output = func([input_data])[0] expected = np.array([[1, 1, 1, 0], [1, 1, 1, 1]]) assert np.all(output == expected), 'Output not as expected'
def test_non_zero(self): """Test masking with non-zero mask value""" layer = core.Masking(5) func = theano.function([layer.input], layer.get_output_mask()) self.assertTrue(np.all( # get mask for this input, if not all the values are 5, shouldn't masked func(np.array([[[1, 1], [2, 1], [3, 1], [5, 5]], [[1, 5], [5, 0], [0, 0], [0, 0]]], dtype=np.int32)) == # This is the expected output mask, one dimension less np.array([[1, 1, 1, 0], [1, 1, 1, 1]])))
def test_non_zero_output(self): """Test output of masking layer with non-zero mask value""" layer = core.Masking(5) func = theano.function([layer.input], layer.get_output()) self.assertTrue(np.all( # get output for this input, replace padding with 0 func(np.array([[[1, 1], [2, 1], [3, 1], [5, 5]], [[1, 5], [5, 0], [0, 0], [0, 0]]], dtype=np.int32)) == # This is the expected output np.array([[[1, 1], [2, 1], [3, 1], [0, 0]], [[1, 5], [5, 0], [0, 0], [0, 0]]])))
def test_sequences(self): """Test masking sequences with zeroes as padding""" # integer inputs, one per timestep, like embeddings layer = core.Masking() func = theano.function([layer.input], layer.get_output_mask()) self.assertTrue(np.all( # get mask for this input func(np.array([[[1], [2], [3], [0]], [[0], [4], [5], [0]]], dtype=np.int32)) == # This is the expected output mask, one dimension less np.array([[1, 1, 1, 0], [0, 1, 1, 0]])))
def test_non_zero(self): """Test masking with non-zero mask value""" if K._BACKEND == "tensorflow": return layer = core.Masking(5) func = K.function([layer.input], [layer.get_output_mask()]) input_data = np.array([[[1, 1], [2, 1], [3, 1], [5, 5]], [[1, 5], [5, 0], [0, 0], [0, 0]]], dtype=np.int32) output = func([input_data])[0] expected = np.array([[1, 1, 1, 0], [1, 1, 1, 1]]) assert (np.all(output == expected))
def test_non_zero_output(): """Test output of masking layer with non-zero mask value""" layer = core.Masking(5) func = K.function([layer.input], [layer.get_output()]) input_data = np.array([[[1, 1], [2, 1], [3, 1], [5, 5]], [[1, 5], [5, 0], [0, 0], [0, 0]]], dtype=np.int32) output = func([input_data])[0] expected = np.array([[[1, 1], [2, 1], [3, 1], [0, 0]], [[1, 5], [5, 0], [0, 0], [0, 0]]]) assert np.all(output == expected) , "Output not as expected"
def test_sequences(): '''Test masking sequences with zeroes as padding''' # integer inputs, one per timestep, like embeddings layer = core.Masking() func = K.function([layer.input], [layer.get_output_mask()]) input_data = np.array([[[1], [2], [3], [0]], [[0], [4], [5], [0]]], dtype=np.int32) # This is the expected output mask, one dimension less expected = np.array([[1, 1, 1, 0], [0, 1, 1, 0]]) # get mask for this input output = func([input_data])[0] assert np.all(output == expected), 'Output not as expected'
def test_sequences(self): """Test masking sequences with zeroes as padding""" if K._BACKEND == "tensorflow": return # integer inputs, one per timestep, like embeddings layer = core.Masking() func = K.function([layer.input], [layer.get_output_mask()]) input_data = np.array([[[1], [2], [3], [0]], [[0], [4], [5], [0]]], dtype=np.int32) # This is the expected output mask, one dimension less expected = np.array([[1, 1, 1, 0], [0, 1, 1, 0]]) # get mask for this input output = func([input_data])[0] assert (np.all(output == expected))
def test_implicit_mask(self): attention_layer = dense_attention.Attention() q = core.Masking(1.1)(np.array([[[1.1], [1]]], dtype=np.float32)) v = core.Masking(1.2)(np.array([[[1.2], [1]]], dtype=np.float32)) actual = attention_layer([q, v]) self.assertAllClose([[[0], [1]]], actual)
def test_override_mask(self): attention_layer = dense_attention.Attention() q = core.Masking()(np.array([[[1.1]]], dtype=np.float32)) mask = np.array([[False]], dtype=np.bool_) actual = attention_layer([q, q], mask=[mask, mask]) self.assertAllClose([[[0]]], actual)