o
    b¦µipp  ã                   @   sÆ   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	 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eƒƒZe	dƒG dd„ deƒƒZdS )é    )Úactivations)Úbackend)Úconstraints)Úinitializers)Úops)Úregularizers)Útree)Úkeras_export)Ú	InputSpec)ÚLayer)ÚDropoutRNNCell)ÚRNNzkeras.layers.GRUCellc                       sn   e Zd ZdZ																		d‡ fd
d„	Z‡ fdd„Zddd„Z‡ fdd„Zddd„Z‡  Z	S )ÚGRUCellaà  Cell class for the GRU layer.

    This class processes one step within the whole time sequence input, whereas
    `keras.layer.GRU` processes the whole sequence.

    Args:
        units: Positive integer, dimensionality of the output space.
        activation: Activation function to use. Default: hyperbolic tangent
            (`tanh`). If you pass None, no activation is applied
            (ie. "linear" activation: `a(x) = x`).
        recurrent_activation: Activation function to use for the recurrent step.
            Default: sigmoid (`sigmoid`). If you pass `None`, no activation is
            applied (ie. "linear" activation: `a(x) = x`).
        use_bias: Boolean, (default `True`), whether the layer
            should use a bias vector.
        kernel_initializer: Initializer for the `kernel` weights matrix,
            used for the linear transformation of the inputs. Default:
            `"glorot_uniform"`.
        recurrent_initializer: Initializer for the `recurrent_kernel`
            weights matrix, used for the linear transformation
            of the recurrent state. Default: `"orthogonal"`.
        bias_initializer: Initializer for the bias vector. Default: `"zeros"`.
        kernel_regularizer: Regularizer function applied to the `kernel` weights
            matrix. Default: `None`.
        recurrent_regularizer: Regularizer function applied to the
            `recurrent_kernel` weights matrix. Default: `None`.
        bias_regularizer: Regularizer function applied to the bias vector.
            Default: `None`.
        kernel_constraint: Constraint function applied to the `kernel` weights
            matrix. Default: `None`.
        recurrent_constraint: Constraint function applied to the
            `recurrent_kernel` weights matrix. Default: `None`.
        bias_constraint: Constraint function applied to the bias vector.
            Default: `None`.
        dropout: Float between 0 and 1. Fraction of the units to drop for the
            linear transformation of the inputs. Default: 0.
        recurrent_dropout: Float between 0 and 1. Fraction of the units to drop
            for the linear transformation of the recurrent state. Default: 0.
        reset_after: GRU convention (whether to apply reset gate after or
            before matrix multiplication). False = "before",
            True = "after" (default and cuDNN compatible).
        seed: Random seed for dropout.

    Call arguments:
        inputs: A 2D tensor, with shape `(batch, features)`.
        states: A 2D tensor with shape `(batch, units)`, which is the state
            from the previous time step.
        training: Python boolean indicating whether the layer should behave in
            training mode or in inference mode. Only relevant when `dropout` or
            `recurrent_dropout` is used.

    Example:

    >>> inputs = np.random.random((32, 10, 8))
    >>> rnn = keras.layers.RNN(keras.layers.GRUCell(4))
    >>> output = rnn(inputs)
    >>> output.shape
    (32, 4)
    >>> rnn = keras.layers.RNN(
    ...    keras.layers.GRUCell(4),
    ...    return_sequences=True,
    ...    return_state=True)
    >>> whole_sequence_output, final_state = rnn(inputs)
    >>> whole_sequence_output.shape
    (32, 10, 4)
    >>> final_state.shape
    (32, 4)
    ÚtanhÚsigmoidTÚglorot_uniformÚ
orthogonalÚzerosNç        c                    s@  |dkrt d|› dƒ‚| dd¡}tƒ jdi |¤Ž || _|| _t |¡| _t |¡| _	|| _
t |¡| _t |¡| _t |¡| _t |¡| _t |	¡| _t |
¡| _t |¡| _t |¡| _t |¡| _tdtd|ƒƒ| _tdtd|ƒƒ| _| jdkr€d| _| jdkrˆd	| _|| _tjj|d
| _ || _!| j| _"| j| _#d S )Nr   zQReceived an invalid value for argument `units`, expected a positive integer, got Ú.Úimplementationé   ç      ð?r   é   é   )Úseed© )$Ú
ValueErrorÚpopÚsuperÚ__init__r   Úunitsr   ÚgetÚ
activationÚrecurrent_activationÚuse_biasr   Úkernel_initializerÚrecurrent_initializerÚbias_initializerr   Úkernel_regularizerÚrecurrent_regularizerÚbias_regularizerr   Úkernel_constraintÚrecurrent_constraintÚbias_constraintÚminÚmaxÚdropoutÚrecurrent_dropoutÚdropout_mask_countr   r   ÚrandomÚSeedGeneratorÚseed_generatorÚreset_afterÚ
state_sizeÚoutput_size)Úselfr!   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r1   r2   r7   r   Úkwargsr   ©Ú	__class__r   úL/home/ubuntu/.local/lib/python3.10/site-packages/keras/src/layers/rnn/gru.pyr    V   sB   ÿÿ

zGRUCell.__init__c                    s´   t ƒ  |¡ |d }| j|| jd fd| j| j| jd| _| j| j| jd fd| j| j	| j
d| _| jrU| js>d| j f}ndd| j f}| j|d| j| j| jd| _d S d | _d S )Néÿÿÿÿr   Úkernel)ÚshapeÚnameÚinitializerÚregularizerÚ
constraintÚrecurrent_kernelr   Úbias)r   ÚbuildÚ
add_weightr!   r&   r)   r,   r@   r'   r*   r-   rF   r%   r7   r(   r+   r.   rG   )r:   Úinput_shapeÚ	input_dimÚ
bias_shaper<   r   r>   rH   ‘   s8   ûûû
zGRUCell.buildFc                 C   sŒ  t  |¡r	|d n|}| jr,| js| jd }}ndd„ tj| j| jjd ddD ƒ\}}| jdkrx|rYd| j	  k r?dk rYn n|  
|¡}||d  }||d  }	||d  }
n|}|}	|}
t || jd d …d | j…f ¡}t |	| jd d …| j| jd …f ¡}t |
| jd d …| jd d …f ¡}| jr·||d | j… 7 }||| j| jd … 7 }||| jd d … 7 }|rÞd| j  k rÄdk rÞn n|  |¡}||d  }||d  }||d  }n|}|}|}t || jd d …d | j…f ¡}t || jd d …| j| jd …f ¡}| jr$| jr$||d | j… 7 }||| j| jd … 7 }|  || ¡}|  || ¡}| jr\t || jd d …| jd d …f ¡}| jrW||| jd d … 7 }|| }nt || | jd d …| jd d …f ¡}|  || ¡}nµ|r“d| j	  k rˆdk r“n n	|  
|¡}|| }t || j¡}| jr¤t ||¡}tj|d	d
d\}}}| jrÃt || j¡}| jrÂ||7 }nt || jd d …d d| j …f ¡}|d d …d | j…f }|d d …| j| jd …f }|d d …| jd d …f }|  || ¡}|  || ¡}| jr|| }nt || | jd d …d| j d …f ¡}|  || ¡}|| d| |  }t  |¡r@|gn|}||fS )Nr   c                 s   s    | ]
}t j|d dV  qdS )r   ©ÚaxisN)r   Úsqueeze)Ú.0Úer   r   r>   Ú	<genexpr>¿   s
   € ÿ
ÿzGRUCell.call.<locals>.<genexpr>rM   r   r   r   r   r   r?   )r   Ú	is_nestedr%   r7   rG   r   ÚsplitrA   r   r1   Úget_dropout_maskÚmatmulr@   r!   r2   Úget_recurrent_dropout_maskrF   r$   r#   Úadd)r:   ÚinputsÚstatesÚtrainingÚh_tm1Ú
input_biasÚrecurrent_biasÚdp_maskÚinputs_zÚinputs_rÚinputs_hÚx_zÚx_rÚx_hÚrec_dp_maskÚh_tm1_zÚh_tm1_rÚh_tm1_hÚrecurrent_zÚrecurrent_rÚzÚrÚrecurrent_hÚhhÚmatrix_xÚmatrix_innerÚhÚ	new_stater   r   r>   Úcall¶   sª   ÿ
þ
 ÿ$
ÿÿÿ
 ÿ$
€ÿ
 ÿzGRUCell.callc                    sä   i d| j “dt | j¡“dt | j¡“d| j“dt | j¡“dt | j¡“dt | j	¡“dt
 | j¡“d	t
 | j¡“d
t
 | j¡“dt | j¡“dt | j¡“dt | j¡“d| j“d| j“d| j“d| j“}tƒ  ¡ }i |¥|¥S )Nr!   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r1   r2   r7   r   )r!   r   Ú	serializer#   r$   r%   r   r&   r'   r(   r   r)   r*   r+   r   r,   r-   r.   r1   r2   r7   r   r   Ú
get_config©r:   ÚconfigÚbase_configr<   r   r>   rv   ,  sb   ÿþÿýúÿù
ÿöóÿòÿïìëÿêçæåäã
zGRUCell.get_configc                 C   s   t j|| jf| jdgS )N)Údtype)r   r   r8   Úcompute_dtype)r:   Ú
batch_sizer   r   r>   Úget_initial_stateO  s   ÿzGRUCell.get_initial_state)r   r   Tr   r   r   NNNNNNr   r   TN©F©N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    rH   rt   rv   r}   Ú__classcell__r   r   r<   r>   r      s.    Hî;
%v#r   zkeras.layers.GRUc                       sD  e Zd ZdZ																			
	
	
	
	
		d6‡ fdd„	Zd7‡ fdd„	Zd8‡ fdd„	Zedd„ ƒZedd„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZed d!„ ƒZed"d#„ ƒZed$d%„ ƒZed&d'„ ƒZed(d)„ ƒZed*d+„ ƒZed,d-„ ƒZed.d/„ ƒZed0d1„ ƒZ‡ fd2d3„Zed4d5„ ƒZ‡  ZS )9ÚGRUa©  Gated Recurrent Unit - Cho et al. 2014.

    Based on available runtime hardware and constraints, this layer
    will choose different implementations (cuDNN-based or backend-native)
    to maximize the performance. If a GPU is available and all
    the arguments to the layer meet the requirement of the cuDNN kernel
    (see below for details), the layer will use a fast cuDNN implementation
    when using the TensorFlow backend.

    The requirements to use the cuDNN implementation are:

    1. `activation` == `tanh`
    2. `recurrent_activation` == `sigmoid`
    3. `recurrent_dropout` == 0
    4. `unroll` is `False`
    5. `use_bias` is `True`
    6. `reset_after` is `True`
    7. Inputs, if use masking, are strictly right-padded.
    8. Eager execution is enabled in the outermost context.

    There are two variants of the GRU implementation. The default one is based
    on [v3](https://arxiv.org/abs/1406.1078v3) and has reset gate applied to
    hidden state before matrix multiplication. The other one is based on
    [original](https://arxiv.org/abs/1406.1078v1) and has the order reversed.

    The second variant is compatible with CuDNNGRU (GPU-only) and allows
    inference on CPU. Thus it has separate biases for `kernel` and
    `recurrent_kernel`. To use this variant, set `reset_after=True` and
    `recurrent_activation='sigmoid'`.

    For example:

    >>> inputs = np.random.random((32, 10, 8))
    >>> gru = keras.layers.GRU(4)
    >>> output = gru(inputs)
    >>> output.shape
    (32, 4)
    >>> gru = keras.layers.GRU(4, return_sequences=True, return_state=True)
    >>> whole_sequence_output, final_state = gru(inputs)
    >>> whole_sequence_output.shape
    (32, 10, 4)
    >>> final_state.shape
    (32, 4)

    Args:
        units: Positive integer, dimensionality of the output space.
        activation: Activation function to use.
            Default: hyperbolic tangent (`tanh`).
            If you pass `None`, no activation is applied
            (ie. "linear" activation: `a(x) = x`).
        recurrent_activation: Activation function to use
            for the recurrent step.
            Default: sigmoid (`sigmoid`).
            If you pass `None`, no activation is applied
            (ie. "linear" activation: `a(x) = x`).
        use_bias: Boolean, (default `True`), whether the layer
            should use a bias vector.
        kernel_initializer: Initializer for the `kernel` weights matrix,
            used for the linear transformation of the inputs. Default:
            `"glorot_uniform"`.
        recurrent_initializer: Initializer for the `recurrent_kernel`
            weights matrix, used for the linear transformation of the recurrent
            state. Default: `"orthogonal"`.
        bias_initializer: Initializer for the bias vector. Default: `"zeros"`.
        kernel_regularizer: Regularizer function applied to the `kernel` weights
            matrix. Default: `None`.
        recurrent_regularizer: Regularizer function applied to the
            `recurrent_kernel` weights matrix. Default: `None`.
        bias_regularizer: Regularizer function applied to the bias vector.
            Default: `None`.
        activity_regularizer: Regularizer function applied to the output of the
            layer (its "activation"). Default: `None`.
        kernel_constraint: Constraint function applied to the `kernel` weights
            matrix. Default: `None`.
        recurrent_constraint: Constraint function applied to the
            `recurrent_kernel` weights matrix. Default: `None`.
        bias_constraint: Constraint function applied to the bias vector.
            Default: `None`.
        dropout: Float between 0 and 1. Fraction of the units to drop for the
            linear transformation of the inputs. Default: 0.
        recurrent_dropout: Float between 0 and 1. Fraction of the units to drop
            for the linear transformation of the recurrent state. Default: 0.
        seed: Random seed for dropout.
        return_sequences: Boolean. Whether to return the last output
            in the output sequence, or the full sequence. Default: `False`.
        return_state: Boolean. Whether to return the last state in addition
            to the output. Default: `False`.
        go_backwards: Boolean (default `False`).
            If `True`, process the input sequence backwards and return the
            reversed sequence.
        stateful: Boolean (default: `False`). If `True`, the last state
            for each sample at index i in a batch will be used as initial
            state for the sample of index i in the following batch.
        unroll: Boolean (default: `False`).
            If `True`, the network will be unrolled,
            else a symbolic loop will be used.
            Unrolling can speed-up a RNN,
            although it tends to be more memory-intensive.
            Unrolling is only suitable for short sequences.
        reset_after: GRU convention (whether to apply reset gate after or
            before matrix multiplication). `False` is `"before"`,
            `True` is `"after"` (default and cuDNN compatible).
        use_cudnn: Whether to use a cuDNN-backed implementation. `"auto"` will
            attempt to use cuDNN when feasible, and will fallback to the
            default implementation if not.

    Call arguments:
        inputs: A 3D tensor, with shape `(batch, timesteps, feature)`.
        mask: Binary tensor of shape `(samples, timesteps)` indicating whether
            a given timestep should be masked  (optional).
            An individual `True` entry indicates that the corresponding timestep
            should be utilized, while a `False` entry indicates that the
            corresponding timestep should be ignored. Defaults to `None`.
        training: Python boolean indicating whether the layer should behave in
            training mode or in inference mode. This argument is passed to the
            cell when calling it. This is only relevant if `dropout` or
            `recurrent_dropout` is used  (optional). Defaults to `None`.
        initial_state: List of initial state tensors to be passed to the first
            call of the cell (optional, `None` causes creation
            of zero-filled initial state tensors). Defaults to `None`.
    r   r   Tr   r   r   Nr   FÚautoc              	      s,  t |fi d|“d|“d|“d|“d|“d|“d|“d|	“d	|
“d
|“d|“d|“d|“d|“d|“d| dd ¡“d| dd¡“dd“d|“d| dd¡“Ž}tƒ j|f||||||dœ|¤Ž tdd| _|dvrptd|› ƒ‚|| _t	 	¡ dkrt	j
|j|j| j|j|dr’|dv r”d | _d S d S d S d S )!Nr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r1   r2   r7   rz   Ú	trainableTrB   Úgru_cellr   r   r   )Úreturn_sequencesÚreturn_stateÚgo_backwardsÚstatefulÚunrollÚactivity_regularizerr   )Úndim)r†   TFzlInvalid valid received for argument `use_cudnn`. Expected one of {'auto', True, False}. Received: use_cudnn=Ú
tensorflow)r7   )Tr†   F)r   r"   r   r   r    r
   Ú
input_specr   Ú	use_cudnnr   Úcudnn_okr#   r$   r   r%   Úsupports_jit)r:   r!   r#   r$   r%   r&   r'   r(   r)   r*   r+   rŽ   r,   r-   r.   r1   r2   r   r‰   rŠ   r‹   rŒ   r   r7   r’   r;   Úcellr<   r   r>   r    Ñ  s˜   ÿþýüûúùø	÷
öõôóòñðïîíìëÿùø
þÿûÿ
özGRU.__init__c                    s  t  |¡r	|d }t  |¡r|d }| jdv r{| js{zW|rD| jrD| j |d d …dd d …f ¡}tj|dd}t 	|t 
|¡¡}|| }n|}tj|||| jj| jj| jj| jj| jj| j| j| j| jjd}t ¡ dkrod| _|W S  tyz   Y nw | jdu r„td	ƒ‚tƒ j||||d
S )Nr   )r†   Tr   rM   )	r@   rF   rG   r#   r$   r‰   r‹   r   r7   r   FTz©use_cudnn=True was specified, but cuDNN is not supported for this layer configuration with this backend. Pass use_cudnn='auto' to fallback to a non-cuDNN implementation.)Úmaskr[   )r   rS   r’   r2   r1   r•   rU   r   Úexpand_dimsÚbroadcast_torA   r   Úgrur@   rF   rG   r#   r$   r‰   r‹   r   r7   r”   ÚNotImplementedErrorr   r   Ú
inner_loop)r:   Ú	sequencesÚinitial_stater–   r[   r_   Údp_sequencesÚoutr<   r   r>   r›   #  sT   




ÿ
ôÿ
ÿÿzGRU.inner_loopc                    s   t ƒ j||||dS )N)r–   r[   r   )r   rt   )r:   rœ   r   r–   r[   r<   r   r>   rt   X  s   ÿzGRU.callc                 C   ó   | j jS r   )r•   r!   ©r:   r   r   r>   r!   ]  ó   z	GRU.unitsc                 C   r    r   )r•   r#   r¡   r   r   r>   r#   a  r¢   zGRU.activationc                 C   r    r   )r•   r$   r¡   r   r   r>   r$   e  r¢   zGRU.recurrent_activationc                 C   r    r   )r•   r%   r¡   r   r   r>   r%   i  r¢   zGRU.use_biasc                 C   r    r   )r•   r&   r¡   r   r   r>   r&   m  r¢   zGRU.kernel_initializerc                 C   r    r   )r•   r'   r¡   r   r   r>   r'   q  r¢   zGRU.recurrent_initializerc                 C   r    r   )r•   r(   r¡   r   r   r>   r(   u  r¢   zGRU.bias_initializerc                 C   r    r   )r•   r)   r¡   r   r   r>   r)   y  r¢   zGRU.kernel_regularizerc                 C   r    r   )r•   r*   r¡   r   r   r>   r*   }  r¢   zGRU.recurrent_regularizerc                 C   r    r   )r•   r+   r¡   r   r   r>   r+     r¢   zGRU.bias_regularizerc                 C   r    r   )r•   r,   r¡   r   r   r>   r,   …  r¢   zGRU.kernel_constraintc                 C   r    r   )r•   r-   r¡   r   r   r>   r-   ‰  r¢   zGRU.recurrent_constraintc                 C   r    r   )r•   r.   r¡   r   r   r>   r.     r¢   zGRU.bias_constraintc                 C   r    r   )r•   r1   r¡   r   r   r>   r1   ‘  r¢   zGRU.dropoutc                 C   r    r   )r•   r2   r¡   r   r   r>   r2   •  r¢   zGRU.recurrent_dropoutc                 C   r    r   )r•   r7   r¡   r   r   r>   r7   ™  r¢   zGRU.reset_afterc                    sü   i d| j “dt | j¡“dt | j¡“d| j“dt | j¡“dt | j¡“dt | j	¡“dt
 | j¡“d	t
 | j¡“d
t
 | j¡“dt
 | j¡“dt | j¡“dt | j¡“dt | j¡“d| j“d| j“d| j“d| jji¥}tƒ  ¡ }|d= i |¥|¥S )Nr!   r#   r$   r%   r&   r'   r(   r)   r*   r+   rŽ   r,   r-   r.   r1   r2   r7   r   r•   )r!   r   ru   r#   r$   r%   r   r&   r'   r(   r   r)   r*   r+   rŽ   r   r,   r-   r.   r1   r2   r7   r•   r   r   rv   rw   r<   r   r>   rv     sl   ÿþÿýúÿù
ÿöóÿòÿïìÿëèÿçäãâá à
"zGRU.get_configc                 C   s   | di |¤ŽS )Nr   r   )Úclsrx   r   r   r>   Úfrom_configÄ  s   zGRU.from_config)r   r   Tr   r   r   NNNNNNNr   r   NFFFFFTr†   r~   )NNF)r€   r   r‚   rƒ   r    r›   rt   Úpropertyr!   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r1   r2   r7   rv   Úclassmethodr¤   r„   r   r   r<   r>   r…   U  s~    }çR5















'r…   N)Ú	keras.srcr   r   r   r   r   r   r   Úkeras.src.api_exportr	   Úkeras.src.layers.input_specr
   Úkeras.src.layers.layerr   Ú%keras.src.layers.rnn.dropout_rnn_cellr   Úkeras.src.layers.rnn.rnnr   r   r…   r   r   r   r>   Ú<module>   s$      G