Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/core/masking.py: 46%
24 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Contains the Masking layer."""
18import tensorflow.compat.v2 as tf
20from keras.src.engine.base_layer import Layer
22# isort: off
23from tensorflow.python.util.tf_export import keras_export
26@keras_export("keras.layers.Masking")
27class Masking(Layer):
28 """Masks a sequence by using a mask value to skip timesteps.
30 For each timestep in the input tensor (dimension #1 in the tensor),
31 if all values in the input tensor at that timestep
32 are equal to `mask_value`, then the timestep will be masked (skipped)
33 in all downstream layers (as long as they support masking).
35 If any downstream layer does not support masking yet receives such
36 an input mask, an exception will be raised.
38 Example:
40 Consider a Numpy data array `x` of shape `(samples, timesteps, features)`,
41 to be fed to an LSTM layer. You want to mask timestep #3 and #5 because you
42 lack data for these timesteps. You can:
44 - Set `x[:, 3, :] = 0.` and `x[:, 5, :] = 0.`
45 - Insert a `Masking` layer with `mask_value=0.` before the LSTM layer:
47 ```python
48 samples, timesteps, features = 32, 10, 8
49 inputs = np.random.random([samples, timesteps, features]).astype(np.float32)
50 inputs[:, 3, :] = 0.
51 inputs[:, 5, :] = 0.
53 model = tf.keras.models.Sequential()
54 model.add(tf.keras.layers.Masking(mask_value=0.,
55 input_shape=(timesteps, features)))
56 model.add(tf.keras.layers.LSTM(32))
58 output = model(inputs)
59 # The time step 3 and 5 will be skipped from LSTM calculation.
60 ```
62 See [the masking and padding guide](
63 https://www.tensorflow.org/guide/keras/masking_and_padding)
64 for more details.
65 """
67 def __init__(self, mask_value=0.0, **kwargs):
68 super().__init__(**kwargs)
69 self.supports_masking = True
70 self.mask_value = mask_value
71 self._compute_output_and_mask_jointly = True
73 def compute_mask(self, inputs, mask=None):
74 return tf.reduce_any(tf.not_equal(inputs, self.mask_value), axis=-1)
76 def call(self, inputs):
77 boolean_mask = tf.reduce_any(
78 tf.not_equal(inputs, self.mask_value), axis=-1, keepdims=True
79 )
80 outputs = inputs * tf.cast(boolean_mask, inputs.dtype)
81 # Compute the mask and outputs simultaneously.
82 outputs._keras_mask = tf.squeeze(boolean_mask, axis=-1)
83 return outputs
85 def compute_output_shape(self, input_shape):
86 return input_shape
88 def get_config(self):
89 config = {"mask_value": self.mask_value}
90 base_config = super().get_config()
91 return dict(list(base_config.items()) + list(config.items()))