o
    2h)                     @   sL   d dl mZ d dl mZ d dlmZ d dlmZ edG dd deZdS )	    )backend)ops)keras_export)Layerzkeras.layers.AlphaDropoutc                       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 )AlphaDropouta  Applies Alpha Dropout to the input.

    Alpha Dropout is a `Dropout` that keeps mean and variance of inputs
    to their original values, in order to ensure the self-normalizing property
    even after this dropout.
    Alpha Dropout fits well to Scaled Exponential Linear Units (SELU) by
    randomly setting activations to the negative saturation value.

    Args:
        rate: Float between 0 and 1. The multiplicative noise will have
            standard deviation `sqrt(rate / (1 - rate))`.
        noise_shape: 1D integer tensor representing the shape of the
            binary alpha dropout mask that will be multiplied with the input.
            For instance, if your inputs have shape
            `(batch_size, timesteps, features)` and
            you want the alpha dropout mask to be the same for all timesteps,
            you can use `noise_shape=(batch_size, 1, features)`.
        seed: A Python integer to use as random seed.

    Call arguments:
        inputs: Input tensor (of any rank).
        training: Python boolean indicating whether the layer should behave in
            training mode (adding alpha dropout) or in inference mode
            (doing nothing).
    Nc                    sp   t  jdi | d|  krdksn td| || _|| _|| _|dkr/tj|| _	d| _
|   d S )Nr      zcInvalid value received for argument `rate`. Expected a float value between 0 and 1. Received: rate=T )super__init__
ValueErrorrateseednoise_shaper   randomSeedGeneratorseed_generatorsupports_masking_build_at_init)selfr   r   r   kwargs	__class__r   h/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/layers/regularization/alpha_dropout.pyr
   #   s   zAlphaDropout.__init__Fc                 C   s   |rT| j dkrT| || j}d}d}| | }ttjj|| jd| j }t||j	}d| j  d| j |d    d }| | | j  }	|| |d|   }
||
 |	 S |S )Nr   g,x?g2֫?)r   r      g      )
r   _get_concrete_noise_shaper   r   greater_equalr   uniformr   castdtype)r   inputstrainingr   alphascalealpha_pkept_idxabxr   r   r   call4   s"   
 zAlphaDropout.callc                 C   s   |S Nr   )r   input_shaper   r   r   compute_output_shapeM   s   z!AlphaDropout.compute_output_shapec                 C   sP   |d u r	t |S t |}g }t|D ]\}}||d u r"|| n| q|S r)   )r   shape	enumerateappend)r   r   r   concrete_inputs_shapeconcrete_noise_shapeivaluer   r   r   r   P   s   

z&AlphaDropout._get_concrete_noise_shapec                    s(   t   }| j| j| jd}i ||S )N)r   r   r   )r	   
get_configr   r   r   )r   base_configconfigr   r   r   r3   \   s   
zAlphaDropout.get_config)NN)F)
__name__
__module____qualname____doc__r
   r(   r+   r   r3   __classcell__r   r   r   r   r      s    
r   N)	keras.srcr   r   keras.src.api_exportr   keras.src.layers.layerr   r   r   r   r   r   <module>   s    