
    &Vf5l                        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dS )    )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)
    tanhsigmoidTglorot_uniform
orthogonalzerosN        c                 
   |dk    rt          d| d          |                    dd          } t                      j        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|                    | _        || _        t8          j                            |          | _        || _         | 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_dropoutr   r   randomSeedGeneratorseed_generatorreset_after
state_sizeoutput_size)selfr"   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r2   r3   r7   r   kwargsr   	__class__s                       U/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/layers/rnn/gru.pyr!   zGRUCell.__init__V   s   * A::=49= = =    $4a88""6""",
%/*55$/O4H$I$I! "."23E"F"F%1%56K%L%L" , 01A B B"."23E"F"F%1%56K%L%L" , 01A B B!,1B!C!C$/O4H$I$I!*??3C 1 122!$S#c3D*E*E!F!F	%n:::EE&*:    c                 
   t                                          |           |d         }|                     || j        dz  fd| j        | j        | j                  | _        |                     | j        | j        dz  fd| j        | j	        | j
                  | _        | j        rN| j        sd| j        z  f}ndd| j        z  f}|                     |d| j        | j        | j                  | _        nd | _        d| _        d S )	N   kernel)shapenameinitializerregularizer
constraintrecurrent_kernelr   biasT)r    build
add_weightr"   r'   r*   r-   rB   r(   r+   r.   rH   r&   r7   r)   r,   r/   rI   built)r:   input_shape	input_dim
bias_shaper<   s       r=   rJ   zGRUCell.build   s"   k"""O	oodj1n-//- & 
 
 !%:tzA~.#220 !0 !
 !
 = 	# 1$*n.

  TZ0
  1 1/ (  DII DI


r>   Fc                 t	   t          j        |          r|d         n|}|                     |          }|                     |          }| j        rJ| j        s
| j        d }}n9d t          j        | j        | j        j	        d         d          D             \  }}|rd| j
        cxk     rdk     rn n||z  }|rd| j        cxk     rdk     rn n||z  }| j        dk    rJ|}	|}
|}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        rA||d | j                 z  }||| j        | j        dz           z  }||| j        dz  d          z  }|}|}|}t          j        || j        d d d | j        f                   }t          j        || j        d d | j        | j        dz  f                   }| j        r3| j        r,||d | j                 z  }||| j        | j        dz           z  }|                     ||z             }|                     ||z             }| j        rPt          j        || j        d d | j        dz  d f                   }| j        r||| j        dz  d          z  }||z  }n1t          j        ||z  | j        d d | j        dz  d f                   }|                     ||z             }ngt          j        || j                  }| j        r||z  }t          j        |dd	          \  }}}| j        r't          j        || j                  }| j        r||z  }n.t          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         }|                     ||z             }|                     ||z             }| j        r||z  }n1t          j        ||z  | j        d d d| j        z  d f                   }|                     ||z             }||z  d|z
  |z  z   }t          j        |          r|gn|}||fS )
Nr   c              3   B   K   | ]}t          j        |d           V  dS )r   axisN)r   squeeze).0es     r=   	<genexpr>zGRUCell.call.<locals>.<genexpr>   sE       . . K***. . . . . .r>   rR   r   r      r   rA   r@   )r	   	is_nestedget_dropout_maskget_recurrent_dropout_maskr&   r7   rI   r   splitrC   r2   r3   r   matmulrB   r"   rH   r%   r$   )r:   inputsstatestrainingh_tm1dp_maskrec_dp_mask
input_biasrecurrent_biasinputs_zinputs_rinputs_hx_zx_rx_h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   //;F1IIV 	 ''//55e<<= 	# -1YN

. . Yty$)/!2D1MMM. . .*
N
  	&dl0000S00000g%F 	(d4::::s:::::K'E!##HHH*Xt{111l
l?'CDDC*$+aaadj1n)D&DE C *Xt{111dj1n6F6F3F'GHHC} 4z,DJ,//z$*tzA~"=>>z$*q."2"233GGG*.qqq,DJ,? K *.qqq$*tzA~2M/MN K  KDM K~l
l;;~dj4:>.IJJ))#*;<<A))#*;<<A  
!jT2111dj1n6F6F3FG  = D>$*q.2B2B#CCK+o!jK!6qqq$*q.:J:J7J!K  {!233BB z&$+66H} 'J&Ih;;;MCc 	"z%1FGG= 3 N2L  #z404Da$*n4D1DE    'qqq,DJ,7K&qqq$*tzA~*E'EFK&qqq$*q.*:*:':;K))#*;<<A))#*;<<A +o!jIt4QQQDJ8H8H5HI  {!233B IQ"$>&118QCCq	)|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/   r2   r3   r7   r   )r"   r   	serializer$   r%   r&   r   r'   r(   r)   r   r*   r+   r,   r   r-   r.   r/   r2   r3   r7   r   r    
get_configr:   configbase_configr<   s      r=   r|   zGRUCell.get_config!  s   
TZ
+/@@
 #K$9)% %
 
 !,"8'# #
 $\%;*& &
  6t7L M M
 !,"8'# #
" $\%;*& &#
(  6t7L M M)
*  !6t7M!N!N+
, #K$9)% %-
2 {4T5IJJ3
4 t|5
6  !77
8 4+9
: DI;
> gg((**(+(((r>   c                 H    t          j        || j        f| j                  gS )N)dtype)r   r   r8   compute_dtype)r:   
batch_sizes     r=   get_initial_statezGRUCell.get_initial_stateD  s(    Iz4?34;MNNN
 	
r>   )r   r   Tr   r   r   NNNNNNr   r   TNFN)
__name__
__module____qualname____doc__r!   rJ   ry   r|   r   __classcell__r<   s   @r=   r   r      s        C CP &+* "!%5& 5& 5& 5& 5& 5&n$ $ $ $ $Ll l l 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. `dropout` == 0 and `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           
      0   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|} t                      j        |f||||||d| t	          d          | _        |dvrt          d|           || _        t          j                    dk    r:t          j	        |j
        |j        | j        |j        |          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/   r2   r3   r7   r   	trainableTrD   gru_cellr   )return_sequencesreturn_statego_backwardsstatefulunrollactivity_regularizerrA   )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   
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/   r2   r3   r   r   r   r   r   r   r7   r   r;   cellr<   s                              r=   r!   zGRU.__init__  s/   8 
 
 
!z
 "6!5
 X	

  21
 #8"7
 .-
  21
 #8"7
 .-
 0/
 "6!5
 ,O
 G
 0/
  $!
" **Wd+++#
$ jjd333%
& '
( )
, 			
-%%!5		
 		
 		
 		
 		
 $+++1113'03 3  
 #O-- )'   . ^++ %D .--- ,+r>   c                 T   t          j        |          r|d         }t          j        |          r|d         }| j        dv r| j        s| j        s	 t          j        |||| j        j        | j        j	        | j        j
        | j        j        | j        j        | j        | j        | j        | j        j                  }t          j                    dk    rd| _        |S # t$          $ r Y nw xY w| j        du rt'          d          t)                                          ||||          S )	Nr   )r   T)	rB   rH   rI   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.)maskr`   )r	   rY   r   r2   r3   r   grur   rB   rH   rI   r$   r%   r   r   r   r7   r   NotImplementedErrorr   r    
inner_loop)r:   	sequencesinitial_stater   r`   outr<   s         r=   r   zGRU.inner_loop  sS   >-(( 	-)!,M>$ 	7D>^++< (> 
 "+!%#y/)-)C!Y^#'9#7-1Y-K)-)>%)%6#{$(I$9  C  ((L88,1)J*   D>T!!1   ww!!}4( " 
 
 	
s   B
C 
C*)C*c                 N    t                                          ||||          S )N)r   r`   r   )r    ry   )r:   r   r   r   r`   r<   s        r=   ry   zGRU.callC  s+    ww||D8=  
 
 	
r>   c                     | j         j        S r   )r   r"   r:   s    r=   r"   z	GRU.unitsH  s    yr>   c                     | j         j        S r   )r   r$   r   s    r=   r$   zGRU.activationL  s    y##r>   c                     | j         j        S r   )r   r%   r   s    r=   r%   zGRU.recurrent_activationP      y--r>   c                     | j         j        S r   )r   r&   r   s    r=   r&   zGRU.use_biasT  s    y!!r>   c                     | j         j        S r   )r   r'   r   s    r=   r'   zGRU.kernel_initializerX      y++r>   c                     | j         j        S r   )r   r(   r   s    r=   r(   zGRU.recurrent_initializer\      y..r>   c                     | j         j        S r   )r   r)   r   s    r=   r)   zGRU.bias_initializer`      y))r>   c                     | j         j        S r   )r   r*   r   s    r=   r*   zGRU.kernel_regularizerd  r   r>   c                     | j         j        S r   )r   r+   r   s    r=   r+   zGRU.recurrent_regularizerh  r   r>   c                     | j         j        S r   )r   r,   r   s    r=   r,   zGRU.bias_regularizerl  r   r>   c                     | j         j        S r   )r   r-   r   s    r=   r-   zGRU.kernel_constraintp      y**r>   c                     | j         j        S r   )r   r.   r   s    r=   r.   zGRU.recurrent_constraintt  r   r>   c                     | j         j        S r   )r   r/   r   s    r=   r/   zGRU.bias_constraintx  s    y((r>   c                     | j         j        S r   )r   r2   r   s    r=   r2   zGRU.dropout|  s    y  r>   c                     | j         j        S r   )r   r3   r   s    r=   r3   zGRU.recurrent_dropout  r   r>   c                     | j         j        S r   )r   r7   r   s    r=   r7   zGRU.reset_after  s    y$$r>   c                 6   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}t1                                                      }|d= i ||S )Nr"   r$   r%   r&   r'   r(   r)   r*   r+   r,   r   r-   r.   r/   r2   r3   r7   r   r   )r"   r   r{   r$   r%   r&   r   r'   r(   r)   r   r*   r+   r,   r   r   r-   r.   r/   r2   r3   r7   r   r   r    r|   r}   s      r=   r|   zGRU.get_config  s   !
TZ!
+/@@!
 #K$9)% %!
 !
 !,"8'# #!
 $\%;*& &!
  6t7L M M!
 !,"8'# #!
" $\%;*& &#!
(  6t7L M M)!
* #L$:)% %+!
0  !6t7M!N!N1!
2 #K$9)% %3!
8 {4T5IJJ9!
: t|;!
<  !7=!
> 4+?!
@ DINA!
 !
D gg((**(+(((r>   c                      | di |S )Nr   r   )clsr~   s     r=   from_configzGRU.from_config  s    s}}V}}r>   )r   r   Tr   r   r   NNNNNNNr   r   NFFFFFTr   r   )NNF)r   r   r   r   r!   r   ry   propertyr"   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r2   r3   r7   r|   classmethodr   r   r   s   @r=   r   r   J  s       x xz &+* "!!3O& O& O& O& O& O&b*
 *
 *
 *
 *
 *
X
 
 
 
 
 

   X $ $ X$ . . X. " " X" , , X, / / X/ * * X* , , X, / / X/ * * X* + + X+ . . X. ) ) X) ! ! X! + + X+ % % X%%) %) %) %) %)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      s   ! ! ! ! ! !       ! ! ! ! ! ! " " " " " "       " " " " " "       - - - - - - 1 1 1 1 1 1 ( ( ( ( ( ( @ @ @ @ @ @ ( ( ( ( ( ( $%%w
 w
 w
 w
 w
e^ w
 w
 &%w
t	  !!f f f f f# f f "!f f fr>   