
    2Vh^p                         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ee             Z e	d       G d de             Zy)    )activations)backend)constraints)initializers)ops)regularizers)tree)keras_export)	InputSpec)Layer)DropoutRNNCell)RNNzkeras.layers.GRUCellc                   h     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Z fdZddZ fdZd	dZ x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)
    c                 D   |dk  rt        d| d      |j                  dd      }t        |   di | || _        || _        t        j                  |      | _        t        j                  |      | _	        || _
        t        j                  |      | _        t        j                  |      | _        t        j                  |      | _        t        j                  |      | _        t        j                  |	      | _        t        j                  |
      | _        t'        j                  |      | _        t'        j                  |      | _        t'        j                  |      | _        t/        dt1        d|            | _        t/        dt1        d|            | _        | j4                  dk7  rd| _        | j                  dk(  rd	| _        || _        t:        j<                  j?                  |
      | _         || _!        | j
                  | _"        | j
                  | _#        y )Nr   zQReceived an invalid value for argument `units`, expected a positive integer, got .implementation         ?              )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,   r/   r0   r5   r   kwargsr   	__class__s                       H/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/rnn/gru.pyr   zGRUCell.__init__V   s   * A:4497!=   $4a8"6",
%//*5$/OO4H$I! "."2"23E"F%1%5%56K%L" , 0 01A B"."2"23E"F%1%5%56K%L" , 0 01A B!,1B!C$/OO4H$I!*?3C 12!$S#c3D*E!F!!S("#D!#&'D#	%nn:::E&**::    c                 X   t         |   |       |d   }| j                  || j                  dz  fd| j                  | j
                  | j                        | _        | j                  | j                  | j                  dz  fd| j                  | j                  | j                        | _        | j                  rh| j                  sd| j                  z  f}ndd| j                  z  f}| j                  |d| j                  | j                  | j                         | _        y d | _        y )Nr   kernel)shapenameinitializerregularizer
constraintrecurrent_kernelr   bias)r   build
add_weightr   r$   r'   r*   r?   r%   r(   r+   rE   r#   r5   r&   r)   r,   rF   )r8   input_shape	input_dim
bias_shaper:   s       r;   rG   zGRUCell.build   s   k"O	oodjj1n-////-- & 
 !%::tzzA~.#222200 !0 !
 ==##$**n.
  TZZ0
  11 11// ( DI DIr<   c                 j   t        j                  |      r|d   n|}| j                  r^| j                  s| j                  d }}nCd t        j                  | j                  | j                  j                  d   d      D        \  }}| j                  dk(  r+|rBd| j                  cxk  rdk  r-n n*| j                  |      }||d   z  }||d   z  }	||d   z  }
n|}|}	|}
t        j                  || j                  d d d | j                  f         }t        j                  |	| j                  d d | j                  | j                  dz  f         }t        j                  |
| j                  d d | j                  dz  d f         }| j                  rF|d | j                   z  }||| j                  | j                  dz   z  }||| j                  dz  d  z  }|rBd| j                  cxk  rdk  r-n n*| j                  |      }||d   z  }||d   z  }||d   z  }n|}|}|}t        j                  || j                   d d d | j                  f         }t        j                  || j                   d d | j                  | j                  dz  f         }| j                  r=| j                  r1|d | j                   z  }||| j                  | j                  dz   z  }| j#                  ||z         }| j#                  ||z         }| j                  r]t        j                  || j                   d d | j                  dz  d f         }| j                  r|| j                  dz  d  z  }||z  }n9t        j                  ||z  | j                   d d | j                  dz  d f         }| j%                  ||z         }n|r.d| j                  cxk  rdk  rn n| j                  |      }||z  }t        j                  || j                        }| j                  r|z  }t        j                  |dd	      \  }}}| j                  r2t        j                  || j                         }| j                  r<|z  }n6t        j                  || j                   d d d d| j                  z  f         }|d d d | j                  f   }|d d | j                  | j                  dz  f   }|d d | j                  dz  d f   }| j#                  ||z         }| j#                  ||z         }| j                  r||z  }n9t        j                  ||z  | j                   d d d| j                  z  d f         }| j%                  ||z         }||z  d|z
  |z  z   }t        j                  |      r|gn|}||fS )
Nr   c              3   J   K   | ]  }t        j                  |d         yw)r   axisN)r   squeeze).0es     r;   	<genexpr>zGRUCell.call.<locals>.<genexpr>   s&      . KK**.s   !#rN   r   r   r   r   r   r>   )r	   	is_nestedr#   r5   rF   r   splitr@   r   r/   get_dropout_maskmatmulr?   r   r0   get_recurrent_dropout_maskrE   r"   r!   )r8   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_states                               r;   callzGRUCell.call   s^   /F1IV 	 ==##-1YYN
. YYtyy$))//!2D1M.*
N
 !#C$,,44//7!GAJ.!GAJ.!GAJ.!!!**Xt{{1l

l?'CDC**$++adjj1n)D&DEC **Xt{{1djj1n6F3F'GHC}}z,DJJ//z$**tzzA~>>z$**q."233C$"8"8>3>"==eD+a.0+a.0+a.0**..q,DJJ,?K **..q$**tzzA~2M/MNK DMM~l

;;~djj4::>JJ))#*;<A))#*;<A !jjT221djj1n6F3FG ==>$**q.2B#CCK+o!jjK!6!6q$**q.:J7J!K {!23BC$,,44//7') zz&$++6H}}J&IIh;MCc"zz%1F1FG== N2L  #zz4004Da$**n4D1DE  'q,DJJ,7K&q$**tzzA~*E'EFK&q$**q.*:':;K))#*;<A))#*;<A+o!jjIt44QDJJ8H5HI {!23B IQ"$>>&1QCq	)|r<   c                    i d| j                   dt        j                  | j                        dt        j                  | j                        d| j
                  dt        j                  | j                        dt        j                  | j                        dt        j                  | j                        dt        j                  | j                        d	t        j                  | j                        d
t        j                  | j                        dt        j                  | j                        dt        j                  | j                         dt        j                  | 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,   r/   r0   r5   r   )r   r   	serializer!   r"   r#   r   r$   r%   r&   r   r'   r(   r)   r   r*   r+   r,   r/   r0   r5   r   r   
get_configr8   configbase_configr:   s      r;   rw   zGRUCell.get_config,  s   
TZZ
+//@
 #K$9$9))%
 
 !,"8"8''#
 $\%;%;**&
  6 6t7L7L M
 !,"8"8''#
" $\%;%;**&#
(  6 6t7L7L M)
*  !6!6t7M7M!N+
, #K$9$9))%-
2 {44T5I5IJ3
4 t||5
6  !7!77
8 4++9
: DII;
> g(*(+(((r<   c                 ^    t        j                  || j                  f| j                        gS )N)dtype)r   zerosr6   compute_dtype)r8   
batch_sizes     r;   get_initial_statezGRUCell.get_initial_stateO  s)    IIz4??34;M;MN
 	
r<   )tanhsigmoidTglorot_uniform
orthogonalr}   NNNNNNr   r   TNFN)
__name__
__module____qualname____doc__r   rG   rt   rw   r   __classcell__r:   s   @r;   r   r      sZ    CP &+* "!%9&v#Jtl!)F
r<   r   zkeras.layers.GRUc                       e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zd fd	Zd fd	Zed        Zed        Z	ed        Z
ed        Zed	        Zed
        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Z fdZed        Z xZS )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`.
    c           
      B   t        |fi d|d|d|d|d|d|d|d|	d	|
d
|d|d|d|d|d|d|j                  dd       d|j                  dd      ddd|d|j                  dd      }t        |   |f||||||d| t        d      | _        |dvrt        d|       || _        t        j                         dk(  rPt        j                  |j                  |j                  | j                  |j                  |      r|dv rd | _        y y y y )!Nr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r/   r0   r5   r|   	trainableTrA   gru_cellr   r   r   )return_sequencesreturn_statego_backwardsstatefulunrollactivity_regularizerr   )ndim)autoTFzlInvalid valid received for argument `use_cudnn`. Expected one of {'auto', True, False}. Received: use_cudnn=
tensorflow)r5   )Tr   F)r   r    r   r   r   r   
input_specr   	use_cudnnr   cudnn_okr!   r"   r   r#   supports_jit)r8   r   r!   r"   r#   r$   r%   r&   r'   r(   r)   r   r*   r+   r,   r/   r0   r   r   r   r   r   r   r5   r   r9   cellr:   s                              r;   r   zGRU.__init__  s   8 
!
 "6
 	

  2
 #8
 .
  2
 #8
 .
 0
 "6
 ,
 
 0
  $!
" **Wd+#
$ jjd3%
& '
( )
* "::&6:+
. 			
-%%!5		
 		
 $+11''0k3 
 #OO-  ))' ^+ %D , .r<   c                    t        j                  |      r|d   }t        j                  |      r|d   }| j                  dv r`| j                  sS	 |rw| j                  rk| j
                  j                  |d d dd d f         }t        j                  |d      }t        j                  |t        j                  |            }||z  }n|}t        j                  |||| j
                  j                  | j
                  j                  | j
                  j                  | j
                  j                   | j
                  j"                  | j$                  | j&                  | j(                  | j
                  j*                        }t        j                         dk(  rd| _        |S | j                  du rt1        d	      t2        | i  ||||
      S # t.        $ r Y 7w xY w)Nr   )r   Tr   rN   )	r?   rE   rF   r!   r"   r   r   r   r5   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.)maskr[   )r	   rT   r   r0   r/   r   rV   r   expand_dimsbroadcast_tor@   r   grur?   rE   rF   r!   r"   r   r   r   r5   r   NotImplementedErrorr   r   
inner_loop)	r8   	sequencesinitial_stater   r[   r_   dp_sequencesoutr:   s	           r;   r   zGRU.inner_loop#  s   >>-()!,M>>$7D>>^+))"DLL"&))"<"<Yq!Qw=O"P"%//'"B"%"2"2#SYYy%9# (17':'0
 "++$%#yy//)-)C)C!YY^^#'99#7#7-1YY-K-K)-)>)>%)%6%6#{{$(II$9$9C  (L8,1)J >>T!1  w!}4( " 
 	
 + s   EG 	GGc                 *    t         |   ||||      S )N)r   r[   r   )r   rt   )r8   r   r   r   r[   r:   s        r;   rt   zGRU.callX  s"    w|D8=  
 	
r<   c                 .    | j                   j                  S r   )r   r   r8   s    r;   r   z	GRU.units]  s    yyr<   c                 .    | j                   j                  S r   )r   r!   r   s    r;   r!   zGRU.activationa  s    yy###r<   c                 .    | j                   j                  S r   )r   r"   r   s    r;   r"   zGRU.recurrent_activatione      yy---r<   c                 .    | j                   j                  S r   )r   r#   r   s    r;   r#   zGRU.use_biasi  s    yy!!!r<   c                 .    | j                   j                  S r   )r   r$   r   s    r;   r$   zGRU.kernel_initializerm      yy+++r<   c                 .    | j                   j                  S r   )r   r%   r   s    r;   r%   zGRU.recurrent_initializerq      yy...r<   c                 .    | j                   j                  S r   )r   r&   r   s    r;   r&   zGRU.bias_initializeru      yy)))r<   c                 .    | j                   j                  S r   )r   r'   r   s    r;   r'   zGRU.kernel_regularizery  r   r<   c                 .    | j                   j                  S r   )r   r(   r   s    r;   r(   zGRU.recurrent_regularizer}  r   r<   c                 .    | j                   j                  S r   )r   r)   r   s    r;   r)   zGRU.bias_regularizer  r   r<   c                 .    | j                   j                  S r   )r   r*   r   s    r;   r*   zGRU.kernel_constraint      yy***r<   c                 .    | j                   j                  S r   )r   r+   r   s    r;   r+   zGRU.recurrent_constraint  r   r<   c                 .    | j                   j                  S r   )r   r,   r   s    r;   r,   zGRU.bias_constraint  s    yy(((r<   c                 .    | j                   j                  S r   )r   r/   r   s    r;   r/   zGRU.dropout  s    yy   r<   c                 .    | j                   j                  S r   )r   r0   r   s    r;   r0   zGRU.recurrent_dropout  r   r<   c                 .    | j                   j                  S r   )r   r5   r   s    r;   r5   zGRU.reset_after  s    yy$$$r<   c                    i d| j                   dt        j                  | j                        dt        j                  | j                        d| j
                  dt        j                  | j                        dt        j                  | j                        dt        j                  | j                        dt        j                  | j                        d	t        j                  | j                        d
t        j                  | j                        dt        j                  | j                        dt        j                  | j                         dt        j                  | j"                        dt        j                  | j$                        d| j&                  d| j(                  d| j*                  d| j,                  j.                  i}t0        | e         }|d= i ||S )Nr   r!   r"   r#   r$   r%   r&   r'   r(   r)   r   r*   r+   r,   r/   r0   r5   r   r   )r   r   rv   r!   r"   r#   r   r$   r%   r&   r   r'   r(   r)   r   r   r*   r+   r,   r/   r0   r5   r   r   r   rw   rx   s      r;   rw   zGRU.get_config  s   !
TZZ!
+//@!
 #K$9$9))%!
 !
 !,"8"8''#!
 $\%;%;**&!
  6 6t7L7L M!
 !,"8"8''#!
" $\%;%;**&#!
(  6 6t7L7L M)!
* #L$:$:))%+!
0  !6!6t7M7M!N1!
2 #K$9$9))%3!
8 {44T5I5IJ9!
: t||;!
<  !7!7=!
> 4++?!
@ DIINNA!
D g(*(+(((r<   c                      | di |S )Nr   r   )clsry   s     r;   from_configzGRU.from_config  s    }V}r<   )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,   r/   r0   r5   rw   classmethodr   r   r   s   @r;   r   r   U  s   xz &+* "!!3P&d3
j

   $ $ . . " " , , / / * * , , / / * * + + . . ) ) ! ! + + % %%)N  r<   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;   <module>r      st    !  ! "  "  - 1 ( @ ( $%B
e^ B
 &B
J
  !p# p "pr<   