o
    2h^p                     @   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rd| _| jdkrd	| _|| _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   S/var/www/html/chatgem/venv/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 )Nr   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 }|rd| j  k rdk rn 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|rd| j	  k rdk rn n	| 
|}|| }t|| j}| jr||7 }tj|d	d
d\}}}| jrt|| 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#   )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>   ru   ,  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   rs   ru   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 rd | _d S d S d S d S )!Nr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r1   r2   r7   ry   	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 rtd	t j||||d
S )Nr   )r   Tr   rM   )	r@   rF   rG   r#   r$   r   r   r   r7   r   FTzuse_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.)maskrZ   )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   rZ   r^   dp_sequencesoutr<   r   r>   r   #  sT   






zGRU.inner_loopc                    s   t  j||||dS )N)r   rZ   r   )r   rs   )r:   r   r   r   rZ   r<   r   r>   rs   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   rt   r#   r$   r%   r   r&   r'   r(   r   r)   r*   r+   r   r   r,   r-   r.   r1   r2   r7   r   r   r   ru   rv   r<   r   r>   ru     sl   
 
"zGRU.get_configc                 C   s   | di |S )Nr   r   )clsrw   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   rs   propertyr!   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r1   r2   r7   ru   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