diff --git a/keras/layers/core.py b/keras/layers/core.py index 7eecf29f3a0..1f772e4957a 100644 --- a/keras/layers/core.py +++ b/keras/layers/core.py @@ -119,8 +119,8 @@ def get_output_mask(self, train=False): class Masking(MaskedLayer): """Mask an input sequence by using a mask value to identify padding. - This layer copies the input to the output layer, - while creating an output mask in the process. + This layer copies the input to the output layer with identified padding + replaced with 0s and creates an output mask in the process. At each timestep, if the values all equal `mask_value`, then the corresponding mask value for the timestep is 0 (skipped), @@ -136,6 +136,10 @@ def get_output_mask(self, train=False): X = self.get_input(train) return T.any(T.ones_like(X) * (1. - T.eq(X, self.mask_value)), axis=-1) + def get_output(self, train=False): + X = self.get_input(train) + return X * T.shape_padright(T.any((1. - T.eq(X, self.mask_value)), axis=-1)) + def get_config(self): return {"name": self.__class__.__name__, "mask_value": self.mask_value} diff --git a/tests/auto/keras/layers/test_core.py b/tests/auto/keras/layers/test_core.py index 9d7bbd536e9..e8034c55c53 100644 --- a/tests/auto/keras/layers/test_core.py +++ b/tests/auto/keras/layers/test_core.py @@ -144,6 +144,19 @@ def test_non_zero(self): # 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]]]))) + if __name__ == '__main__': unittest.main()