B
    d                 @   sJ   d Z ddlm  mZ ddlmZ ddlmZ edG dd deZ	dS )zContains the Masking layer.    N)Layer)keras_exportzkeras.layers.Maskingc                   sH   e Zd ZdZd fdd	ZdddZdd	 Zd
d Z fddZ  Z	S )Maskinga  Masks a sequence by using a mask value to skip timesteps.

    For each timestep in the input tensor (dimension #1 in the tensor),
    if all values in the input tensor at that timestep
    are equal to `mask_value`, then the timestep will be masked (skipped)
    in all downstream layers (as long as they support masking).

    If any downstream layer does not support masking yet receives such
    an input mask, an exception will be raised.

    Example:

    Consider a Numpy data array `x` of shape `(samples, timesteps, features)`,
    to be fed to an LSTM layer. You want to mask timestep #3 and #5 because you
    lack data for these timesteps. You can:

    - Set `x[:, 3, :] = 0.` and `x[:, 5, :] = 0.`
    - Insert a `Masking` layer with `mask_value=0.` before the LSTM layer:

    ```python
    samples, timesteps, features = 32, 10, 8
    inputs = np.random.random([samples, timesteps, features]).astype(np.float32)
    inputs[:, 3, :] = 0.
    inputs[:, 5, :] = 0.

    model = tf.keras.models.Sequential()
    model.add(tf.keras.layers.Masking(mask_value=0.,
                                      input_shape=(timesteps, features)))
    model.add(tf.keras.layers.LSTM(32))

    output = model(inputs)
    # The time step 3 and 5 will be skipped from LSTM calculation.
    ```

    See [the masking and padding guide](
      https://www.tensorflow.org/guide/keras/masking_and_padding)
    for more details.
            c                s$   t  jf | d| _|| _d| _d S )NT)super__init__Zsupports_masking
mask_valueZ _compute_output_and_mask_jointly)selfr   kwargs)	__class__ K/var/www/html/venv/lib/python3.7/site-packages/keras/layers/core/masking.pyr   C   s    zMasking.__init__Nc             C   s   t jt || jddS )N)axis)tf
reduce_any	not_equalr   )r	   inputsmaskr   r   r   compute_maskI   s    zMasking.compute_maskc             C   s@   t jt || jddd}|t ||j }t j|dd|_|S )Nr   T)r   Zkeepdims)r   )r   r   r   r   castZdtypeZsqueezeZ_keras_mask)r	   r   Zboolean_maskoutputsr   r   r   callL   s
    zMasking.callc             C   s   |S )Nr   )r	   Zinput_shaper   r   r   compute_output_shapeU   s    zMasking.compute_output_shapec                s0   d| j i}t  }tt| t|  S )Nr   )r   r   
get_configdictlistitems)r	   configZbase_config)r   r   r   r   X   s    

zMasking.get_config)r   )N)
__name__
__module____qualname____doc__r   r   r   r   r   __classcell__r   r   )r   r   r      s   '
	r   )
r"   Ztensorflow.compat.v2compatZv2r   Zkeras.engine.base_layerr   Z tensorflow.python.util.tf_exportr   r   r   r   r   r   <module>   s
   