o
    ={cí  ã                   @   s€   d Z ddlmZ ddlmZ ddlmZ ddlmZ 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 )z2Parametric Rectified Linear Unit activation layer.é    )Úbackend)Úconstraints)Úinitializers)Úregularizers)ÚLayer)Ú	InputSpec)Útf_utils)Úkeras_exportzkeras.layers.PReLUc                       sZ   e Zd ZdZ				d‡ fdd„	Zejdd„ ƒZdd	„ Z‡ fd
d„Z	ejdd„ ƒZ
‡  ZS )ÚPReLUaü  Parametric Rectified Linear Unit.

    It follows:

    ```
      f(x) = alpha * x for x < 0
      f(x) = x for x >= 0
    ```

    where `alpha` is a learned array with the same shape as x.

    Input shape:
      Arbitrary. Use the keyword argument `input_shape`
      (tuple of integers, does not include the samples axis)
      when using this layer as the first layer in a model.

    Output shape:
      Same shape as the input.

    Args:
      alpha_initializer: Initializer function for the weights.
      alpha_regularizer: Regularizer for the weights.
      alpha_constraint: Constraint for the weights.
      shared_axes: The axes along which to share learnable
        parameters for the activation function.
        For example, if the incoming feature maps
        are from a 2D convolution
        with output shape `(batch, height, width, channels)`,
        and you wish to share parameters across space
        so that each filter only has one set of parameters,
        set `shared_axes=[1, 2]`.
    ÚzerosNc                    sv   t ƒ jdi |¤Ž d| _t |¡| _t |¡| _t |¡| _	|d u r'd | _
d S t|ttfƒs4|g| _
d S t|ƒ| _
d S )NT© )ÚsuperÚ__init__Zsupports_maskingr   ÚgetÚalpha_initializerr   Úalpha_regularizerr   Úalpha_constraintÚshared_axesÚ
isinstanceÚlistÚtuple)Úselfr   r   r   r   Úkwargs©Ú	__class__r   ú=lib/python3.10/site-packages/keras/layers/activation/prelu.pyr   A   s   
zPReLU.__init__c                 C   sž   t |dd … ƒ}| jd ur| jD ]}d||d < q| j|d| j| j| jd| _i }| jrAtdt|ƒƒD ]}|| jvr@|| ||< q3t	t|ƒ|d| _
d| _d S )Né   Úalpha)ÚshapeÚnameZinitializerZregularizerZ
constraint)ÚndimÚaxesT)r   r   Z
add_weightr   r   r   r   ÚrangeÚlenr   Z
input_specZbuilt)r   Úinput_shapeZparam_shapeÚir!   r   r   r   ÚbuildU   s&   

û
€
zPReLU.buildc                 C   s&   t  |¡}| j t  | ¡ }|| S ©N)r   Zrelur   )r   ZinputsÚposÚnegr   r   r   Úcallk   s   
z
PReLU.callc                    sN   t  | j¡t | j¡t | j¡| jdœ}tƒ  	¡ }t
t| ¡ ƒt| ¡ ƒ ƒS )N)r   r   r   r   )r   Z	serializer   r   r   r   r   r   r   Ú
get_configÚdictr   Úitems)r   ZconfigZbase_configr   r   r   r+   p   s   


ü
zPReLU.get_configc                 C   s   |S r'   r   )r   r$   r   r   r   Úcompute_output_shapez   s   zPReLU.compute_output_shape)r   NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Zshape_type_conversionr&   r*   r+   r.   Ú__classcell__r   r   r   r   r
      s    #û

r
   N)r2   Zkerasr   r   r   r   Zkeras.engine.base_layerr   Zkeras.engine.input_specr   Zkeras.utilsr   Z tensorflow.python.util.tf_exportr	   r
   r   r   r   r   Ú<module>   s   