
    2VhO\                     t   d dl 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
  G d de      Z ed	d
g       G d de             Z eddg       G d de             Z eddg       G d de             Z eddg       G d de             Z eddg       G d de             Z eddg       G d d e             Z ed!d"g       G d# d$e             Z ed%d&g       G d' d(e             Z ed)d*g       G d+ d,e             Z ed-d.g       G d/ d0e             Zd1 Z eg d2       G d3 d4e             Zy)5    N)ops)keras_export)random)Initializer)serialization_libc                       e Zd ZddZd Zy)RandomInitializerNc                    || _         |t        j                         }|| _
        y t        |t              rt        j                  |      }|| _
        y t        |t        t        j                  f      st        d|       || _
        y )Nzf`seed` argument should be an instance of `keras.random.SeedGenerator()` or an integer. Received: seed=)
_init_seedr   make_default_seed
isinstancedictr   deserialize_keras_objectintSeedGenerator
ValueErrorseed)selfr   s     Z/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/initializers/random_initializers.py__init__zRandomInitializer.__init__   s    <++-D 	 d#$==dCD 	 D3(<(<"=>""&) 
 	    c                 H    t        j                  | j                        }d|iS Nr   r   serialize_keras_objectr   )r   seed_configs     r   
get_configzRandomInitializer.get_config   s!    '>>tO$$r   N)__name__
__module____qualname__r   r    r   r   r	   r	   
   s    %r   r	   zkeras.initializers.RandomNormalz keras.initializers.random_normalc                   6     e Zd ZdZd fd	ZddZ fdZ xZS )RandomNormalaE  Random normal initializer.

    Draws samples from a normal distribution for given parameters.

    Examples:

    >>> # Standalone usage:
    >>> initializer = RandomNormal(mean=0.0, stddev=1.0)
    >>> values = initializer(shape=(2, 2))

    >>> # Usage in a Keras layer:
    >>> initializer = RandomNormal(mean=0.0, stddev=1.0)
    >>> layer = Dense(3, kernel_initializer=initializer)

    Args:
        mean: A python scalar or a scalar keras tensor. Mean of the random
            values to generate.
        stddev: A python scalar or a scalar keras tensor. Standard deviation of
           the random values to generate.
        seed: A Python integer or instance of
            `keras.backend.SeedGenerator`.
            Used to make the behavior of the initializer
            deterministic. Note that an initializer seeded with an integer
            or `None` (unseeded) will produce the same random values
            across multiple calls. To get different random values
            across multiple calls, use as seed an instance
            of `keras.backend.SeedGenerator`.
    c                 B    || _         || _        t        |   |       y Nr   meanstddevsuperr   r   r)   r*   r   	__class__s       r   r   zRandomNormal.__init__B   !    	d#r   c                 r    t        j                  || j                  | j                  | j                  |      S N)shaper)   r*   r   dtype)r   normalr)   r*   r   r   r1   r2   s      r   __call__zRandomNormal.__call__G   s.    }};;
 	
r   c                 ^    t         |          }| j                  | j                  d}i ||S N)r)   r*   r+   r   r)   r*   r   base_configconfigr-   s      r   r   zRandomNormal.get_configP   2    g(*))t{{;(+(((r           皙?Nr   r   r    r!   __doc__r   r5   r   __classcell__r-   s   @r   r$   r$          :$

) )r   r$   z"keras.initializers.TruncatedNormalz#keras.initializers.truncated_normalc                   6     e Zd ZdZd fd	ZddZ fdZ xZS )TruncatedNormala  Initializer that generates a truncated normal distribution.

    The values generated are similar to values from a
    `RandomNormal` initializer, except that values more
    than two standard deviations from the mean are
    discarded and re-drawn.

    Examples:

    >>> # Standalone usage:
    >>> initializer = TruncatedNormal(mean=0., stddev=1.)
    >>> values = initializer(shape=(2, 2))

    >>> # Usage in a Keras layer:
    >>> initializer = TruncatedNormal(mean=0., stddev=1.)
    >>> layer = Dense(3, kernel_initializer=initializer)

    Args:
        mean: A python scalar or a scalar keras tensor. Mean of the random
            values to generate.
        stddev: A python scalar or a scalar keras tensor. Standard deviation of
           the random values to generate.
        seed: A Python integer or instance of
            `keras.backend.SeedGenerator`.
            Used to make the behavior of the initializer
            deterministic. Note that an initializer seeded with an integer
            or `None` (unseeded) will produce the same random values
            across multiple calls. To get different random values
            across multiple calls, use as seed an instance
            of `keras.backend.SeedGenerator`.
    c                 B    || _         || _        t        |   |       y r&   r(   r,   s       r   r   zTruncatedNormal.__init__}   r.   r   c                 r    t        j                  || j                  | j                  | j                  |      S r0   )r   truncated_normalr)   r*   r   r4   s      r   r5   zTruncatedNormal.__call__   s0    &&;;
 	
r   c                 ^    t         |          }| j                  | j                  d}i ||S r7   r8   r9   s      r   r   zTruncatedNormal.get_config   r<   r   r=   r   r@   rC   s   @r   rF   rF   V   s    @$

) )r   rF   z keras.initializers.RandomUniformz!keras.initializers.random_uniformc                   6     e Zd ZdZd fd	ZddZ fdZ xZS )RandomUniformaz  Random uniform initializer.

    Draws samples from a uniform distribution for given parameters.

    Examples:

    >>> # Standalone usage:
    >>> initializer = RandomUniform(minval=0.0, maxval=1.0)
    >>> values = initializer(shape=(2, 2))

    >>> # Usage in a Keras layer:
    >>> initializer = RandomUniform(minval=0.0, maxval=1.0)
    >>> layer = Dense(3, kernel_initializer=initializer)

    Args:
        minval: A python scalar or a scalar keras tensor. Lower bound of the
            range of random values to generate (inclusive).
        maxval: A python scalar or a scalar keras tensor. Upper bound of the
            range of random values to generate (exclusive).
        seed: A Python integer or instance of
            `keras.backend.SeedGenerator`.
            Used to make the behavior of the initializer
            deterministic. Note that an initializer seeded with an integer
            or `None` (unseeded) will produce the same random values
            across multiple calls. To get different random values
            across multiple calls, use as seed an instance
            of `keras.backend.SeedGenerator`.
    c                 B    || _         || _        t        |   |       y r&   )minvalmaxvalr+   r   )r   rN   rO   r   r-   s       r   r   zRandomUniform.__init__   s!    d#r   c                 r    t        j                  || j                  | j                  | j                  |      S )N)r1   rN   rO   r   r2   )r   uniformrN   rO   r   r4   s      r   r5   zRandomUniform.__call__   s.    ~~;;;;
 	
r   c                 ^    t         |          }| j                  | j                  d}i ||S )N)rN   rO   )r+   r   rN   rO   r9   s      r   r   zRandomUniform.get_config   s2    g(* KK4;;?(+(((r   )gr?   Nr   r@   rC   s   @r   rL   rL      rD   r   rL   z"keras.initializers.VarianceScalingz#keras.initializers.variance_scalingc                   >     e Zd ZdZ	 	 	 	 d fd	ZddZ fdZ xZS )VarianceScalinga  Initializer that adapts its scale to the shape of its input tensors.

    With `distribution="truncated_normal" or "untruncated_normal"`, samples are
    drawn from a truncated/untruncated normal distribution with a mean of zero
    and a standard deviation (after truncation, if used) `stddev = sqrt(scale /
    n)`, where `n` is:

    - number of input units in the weight tensor, if `mode="fan_in"`
    - number of output units, if `mode="fan_out"`
    - average of the numbers of input and output units, if `mode="fan_avg"`

    With `distribution="uniform"`, samples are drawn from a uniform distribution
    within `[-limit, limit]`, where `limit = sqrt(3 * scale / n)`.

    Examples:

    >>> # Standalone usage:
    >>> initializer = VarianceScaling(
        scale=0.1, mode='fan_in', distribution='uniform')
    >>> values = initializer(shape=(2, 2))

    >>> # Usage in a Keras layer:
    >>> initializer = VarianceScaling(
        scale=0.1, mode='fan_in', distribution='uniform')
    >>> layer = Dense(3, kernel_initializer=initializer)

    Args:
        scale: Scaling factor (positive float).
        mode: One of `"fan_in"`, `"fan_out"`, `"fan_avg"`.
        distribution: Random distribution to use.
            One of `"truncated_normal"`, `"untruncated_normal"`, or `"uniform"`.
        seed: A Python integer or instance of
            `keras.backend.SeedGenerator`.
            Used to make the behavior of the initializer
            deterministic. Note that an initializer seeded with an integer
            or `None` (unseeded) will produce the same random values
            across multiple calls. To get different random values
            across multiple calls, use as seed an instance
            of `keras.backend.SeedGenerator`.
    c                    |dk  rt        d|       h d}||vrt        d| d|       |j                         }|dk(  rd}h d}||vrt        d	| d
|       || _        || _        || _        t
        |   |       y )Nr>   z9Argument `scale` must be positive float. Received: scale=>   fan_infan_avgfan_outzInvalid `mode` argument: z. Please use one of r3   rI   >   rQ   rI   untruncated_normalz!Invalid `distribution` argument: z.Please use one of r'   )r   lowerscalemodedistributionr+   r   )r   r[   r\   r]   r   allowed_modesallowed_distributionsr-   s          r   r   zVarianceScaling.__init__   s     C<##('+  9}$+D6 2%%2O5  $))+8#-L!

 443L> B%%:$;=  
	(d#r   c                 |   | j                   }t        |      \  }}| j                  dk(  r|t        d|      z  }n4| j                  dk(  r|t        d|      z  }n|t        d||z   dz        z  }| j                  dk(  r<t        j                  |      dz  }t        j                  |d||| j                        S | j                  d	k(  r9t        j                  |      }t        j                  |d||| j                        S t        j                  d
|z        }t        j                  || ||| j                        S )NrV         ?rX          @rI   g۶%?r>   )r)   r*   r2   r   rY   g      @)rN   rO   r2   r   )r[   compute_fansr\   maxr]   mathsqrtr   rI   r   r3   rQ   )r   r1   r2   r[   rV   rX   r*   limits           r   r5   zVarianceScaling.__call__  s(   

&u-99 Sf%%EYY)#Sg&&ESv/3677E 22YYu%(;;F**Ce$))  "66YYu%F==Ce$))  IIcEk*E>>ufU%dii r   c                 t    t         |          }| j                  | j                  | j                  d}i ||S )N)r[   r\   r]   )r+   r   r[   r\   r]   r9   s      r   r   zVarianceScaling.get_config6  s@    g(*ZZII --

 )+(((r   )ra   rV   rI   Nr   r@   rC   s   @r   rT   rT      s,    'V '"$H2) )r   rT   z keras.initializers.GlorotUniformz!keras.initializers.glorot_uniformc                   *     e Zd ZdZd fd	Zd Z xZS )GlorotUniformah  The Glorot uniform initializer, also called Xavier uniform initializer.

    Draws samples from a uniform distribution within `[-limit, limit]`, where
    `limit = sqrt(6 / (fan_in + fan_out))` (`fan_in` is the number of input
    units in the weight tensor and `fan_out` is the number of output units).

    Examples:

    >>> # Standalone usage:
    >>> initializer = GlorotUniform()
    >>> values = initializer(shape=(2, 2))

    >>> # Usage in a Keras layer:
    >>> initializer = GlorotUniform()
    >>> layer = Dense(3, kernel_initializer=initializer)

    Args:
        seed: A Python integer or instance of
            `keras.backend.SeedGenerator`.
            Used to make the behavior of the initializer
            deterministic. Note that an initializer seeded with an integer
            or `None` (unseeded) will produce the same random values
            across multiple calls. To get different random values
            across multiple calls, use as seed an instance
            of `keras.backend.SeedGenerator`.

    Reference:

    - [Glorot et al., 2010](http://proceedings.mlr.press/v9/glorot10a.html)
    c                 ,    t         |   ddd|       y )Nra   rW   rQ   r[   r\   r]   r   r+   r   r   r   r-   s     r   r   zGlorotUniform.__init__f  s    IID 	 	
r   c                 D    dt        j                  | j                        iS r   r   r   s    r   r   zGlorotUniform.get_configk  !    %<<T__M
 	
r   r   r   r    r!   rA   r   r   rB   rC   s   @r   rj   rj   @      >


r   rj   zkeras.initializers.GlorotNormalz keras.initializers.glorot_normalc                   *     e Zd ZdZd fd	Zd Z xZS )GlorotNormala~  The Glorot normal initializer, also called Xavier normal initializer.

    Draws samples from a truncated normal distribution centered on 0 with
    `stddev = sqrt(2 / (fan_in + fan_out))` where `fan_in` is the number of
    input units in the weight tensor and `fan_out` is the number of output units
    in the weight tensor.

    Examples:

    >>> # Standalone usage:
    >>> initializer = GlorotNormal()
    >>> values = initializer(shape=(2, 2))

    >>> # Usage in a Keras layer:
    >>> initializer = GlorotNormal()
    >>> layer = Dense(3, kernel_initializer=initializer)

    Args:
        seed: A Python integer or instance of
            `keras.backend.SeedGenerator`.
            Used to make the behavior of the initializer
            deterministic. Note that an initializer seeded with an integer
            or `None` (unseeded) will produce the same random values
            across multiple calls. To get different random values
            across multiple calls, use as seed an instance
            of `keras.backend.SeedGenerator`.

    Reference:

    - [Glorot et al., 2010](http://proceedings.mlr.press/v9/glorot10a.html)
    c                 ,    t         |   ddd|       y )Nra   rW   rI   rl   rm   rn   s     r   r   zGlorotNormal.__init__  s!    +	 	 	
r   c                 D    dt        j                  | j                        iS r   r   rp   s    r   r   zGlorotNormal.get_config  rq   r   r   rr   rC   s   @r   ru   ru   q  s    @

r   ru   zkeras.initializers.LecunNormalzkeras.initializers.lecun_normalc                   *     e Zd ZdZd fd	Zd Z xZS )LecunNormala  Lecun normal initializer.

    Initializers allow you to pre-specify an initialization strategy, encoded in
    the Initializer object, without knowing the shape and dtype of the variable
    being initialized.

    Draws samples from a truncated normal distribution centered on 0 with
    `stddev = sqrt(1 / fan_in)` where `fan_in` is the number of input units in
    the weight tensor.

    Examples:

    >>> # Standalone usage:
    >>> initializer = LecunNormal()
    >>> values = initializer(shape=(2, 2))

    >>> # Usage in a Keras layer:
    >>> initializer = LecunNormal()
    >>> layer = Dense(3, kernel_initializer=initializer)

    Args:
        seed: A Python integer or instance of
            `keras.backend.SeedGenerator`.
            Used to make the behavior of the initializer
            deterministic. Note that an initializer seeded with an integer
            or `None` (unseeded) will produce the same random values
            across multiple calls. To get different random values
            across multiple calls, use as seed an instance
            of `keras.backend.SeedGenerator`.

    Reference:

    - [Klambauer et al., 2017](https://arxiv.org/abs/1706.02515)
    c                 ,    t         |   ddd|       y )Nra   rV   rI   rl   rm   rn   s     r   r   zLecunNormal.__init__      H3ED 	 	
r   c                 D    dt        j                  | j                        iS r   r   rp   s    r   r   zLecunNormal.get_config  rq   r   r   rr   rC   s   @r   ry   ry     s    !F


r   ry   zkeras.initializers.LecunUniformz keras.initializers.lecun_uniformc                   *     e Zd ZdZd fd	Zd Z xZS )LecunUniforma  Lecun uniform initializer.

    Draws samples from a uniform distribution within `[-limit, limit]`, where
    `limit = sqrt(3 / fan_in)` (`fan_in` is the number of input units in the
    weight tensor).

    Examples:

    >>> # Standalone usage:
    >>> initializer = LecunUniform()
    >>> values = initializer(shape=(2, 2))

    >>> # Usage in a Keras layer:
    >>> initializer = LecunUniform()
    >>> layer = Dense(3, kernel_initializer=initializer)

    Args:
        seed: A Python integer or instance of
            `keras.backend.SeedGenerator`.
            Used to make the behavior of the initializer
            deterministic. Note that an initializer seeded with an integer
            or `None` (unseeded) will produce the same random values
            across multiple calls. To get different random values
            across multiple calls, use as seed an instance
            of `keras.backend.SeedGenerator`.

    Reference:

    - [Klambauer et al., 2017](https://arxiv.org/abs/1706.02515)
    c                 ,    t         |   ddd|       y )Nra   rV   rQ   rl   rm   rn   s     r   r   zLecunUniform.__init__      H94 	 	
r   c                 D    dt        j                  | j                        iS r   r   rp   s    r   r   zLecunUniform.get_config  rq   r   r   rr   rC   s   @r   r~   r~     rs   r   r~   zkeras.initializers.HeNormalzkeras.initializers.he_normalc                   *     e Zd ZdZd fd	Zd Z xZS )HeNormala  He normal initializer.

    It draws samples from a truncated normal distribution centered on 0 with
    `stddev = sqrt(2 / fan_in)` where `fan_in` is the number of input units in
    the weight tensor.

    Examples:

    >>> # Standalone usage:
    >>> initializer = HeNormal()
    >>> values = initializer(shape=(2, 2))

    >>> # Usage in a Keras layer:
    >>> initializer = HeNormal()
    >>> layer = Dense(3, kernel_initializer=initializer)

    Args:
        seed: A Python integer or instance of
            `keras.backend.SeedGenerator`.
            Used to make the behavior of the initializer
            deterministic. Note that an initializer seeded with an integer
            or `None` (unseeded) will produce the same random values
            across multiple calls. To get different random values
            across multiple calls, use as seed an instance
            of `keras.backend.SeedGenerator`.

    Reference:

    - [He et al., 2015](https://arxiv.org/abs/1502.01852)
    c                 ,    t         |   ddd|       y )Nrb   rV   rI   rl   rm   rn   s     r   r   zHeNormal.__init__-  r{   r   c                 D    dt        j                  | j                        iS r   r   rp   s    r   r   zHeNormal.get_config2  rq   r   r   rr   rC   s   @r   r   r         >


r   r   zkeras.initializers.HeUniformzkeras.initializers.he_uniformc                   *     e Zd ZdZd fd	Zd Z xZS )	HeUniforma  He uniform variance scaling initializer.

    Draws samples from a uniform distribution within `[-limit, limit]`, where
    `limit = sqrt(6 / fan_in)` (`fan_in` is the number of input units in the
    weight tensor).

    Examples:

    >>> # Standalone usage:
    >>> initializer = HeUniform()
    >>> values = initializer(shape=(2, 2))

    >>> # Usage in a Keras layer:
    >>> initializer = HeUniform()
    >>> layer = Dense(3, kernel_initializer=initializer)

    Args:
        seed: A Python integer or instance of
            `keras.backend.SeedGenerator`.
            Used to make the behavior of the initializer
            deterministic. Note that an initializer seeded with an integer
            or `None` (unseeded) will produce the same random values
            across multiple calls. To get different random values
            across multiple calls, use as seed an instance
            of `keras.backend.SeedGenerator`.

    Reference:

    - [He et al., 2015](https://arxiv.org/abs/1502.01852)
    c                 ,    t         |   ddd|       y )Nrb   rV   rQ   rl   rm   rn   s     r   r   zHeUniform.__init__Y  r   r   c                 D    dt        j                  | j                        iS r   r   rp   s    r   r   zHeUniform.get_config^  rq   r   r   rr   rC   s   @r   r   r   8  r   r   r   c                 
   t        |       } t        |       dk  rdx}}nPt        |       dk(  r| d   x}}n:t        |       dk(  r| d   }| d   }n!d}| dd D ]  }||z  }	 | d   |z  }| d   |z  }t        |      t        |      fS )zComputes the number of input and output units for a weight shape.

    Args:
        shape: Integer shape tuple.

    Returns:
        A tuple of integer scalars: `(fan_in, fan_out)`.
       r      N)tuplelenr   )r1   rV   rX   receptive_field_sizedims        r   rc   rc   d  s     %LE
5zA~	Uq 8#	Uqq(  !": 	(C C' 	(r11)22v;G$$r   )zkeras.initializers.Orthogonalzkeras.initializers.orthogonalz(keras.initializers.OrthogonalInitializerc                   6     e Zd ZdZd fd	ZddZ fdZ xZS )
Orthogonala  Initializer that generates an orthogonal matrix.

    If the shape of the tensor to initialize is two-dimensional, it is
    initialized with an orthogonal matrix obtained from the QR decomposition of
    a matrix of random numbers drawn from a normal distribution. If the matrix
    has fewer rows than columns then the output will have orthogonal rows.
    Otherwise, the output will have orthogonal columns.

    If the shape of the tensor to initialize is more than two-dimensional,
    a matrix of shape `(shape[0] * ... * shape[n - 2], shape[n - 1])`
    is initialized, where `n` is the length of the shape vector.
    The matrix is subsequently reshaped to give a tensor of the desired shape.

    Examples:

    >>> # Standalone usage:
    >>> initializer = keras.initializers.Orthogonal()
    >>> values = initializer(shape=(2, 2))

    >>> # Usage in a Keras layer:
    >>> initializer = keras.initializers.Orthogonal()
    >>> layer = keras.layers.Dense(3, kernel_initializer=initializer)

    Args:
        gain: Multiplicative factor to apply to the orthogonal matrix.
        seed: A Python integer. Used to make the behavior of the initializer
            deterministic.

    Reference:

    - [Saxe et al., 2014](https://openreview.net/forum?id=_wzZwKpTDF_9C)
    c                 4    || _         t        | 	  |       y r&   )gainr+   r   )r   r   r   r-   s      r   r   zOrthogonal.__init__  s    	d#r   c                    t        |      dk  rt        d| dt        |       d      d}|d d D ]  }||z  }	 |d   }t        ||      t        ||      f}t	        j
                  || j                  |      }t        j                  |      \  }}	t        j                  |	      }
|t        j                  |
      z  }||k  rt        j                  |      }| j                  t        j                  ||      z  S )Nr   zKThe tensor to initialize must be at least two-dimensional. Received: shape=z	 of rank .r   r   )r   r2   )r   r   rd   minr   r3   r   r   qrdiagsign	transposer   reshape)r   r1   r2   num_rowsr   num_cols
flat_shapeaqrds              r   r5   zOrthogonal.__call__  s    u:>yUA7  ": 	COH	9(H-s8X/FG
 MM*499EBvvay1HHQK	SXXa[ha Ayy3;;q%000r   c                 H    t         |          }d| j                  i}i ||S )Nr   )r+   r   r   r9   s      r   r   zOrthogonal.get_config  s.    g(*$))$(+(((r   )ra   Nr   r@   rC   s   @r   r   r     s    B$16) )r   r   )re   	keras.srcr   keras.src.api_exportr   keras.src.backendr   "keras.src.initializers.initializerr   keras.src.savingr   r	   r$   rF   rL   rT   rj   ru   ry   r~   r   r   rc   r   r"   r   r   <module>r      s
     - $ : .% %( )*/)$ /)/)d ,-2)' 2)2)j *+/)% /)/)d ,-n)' n)n)b *+(
O (
(
V )*,
? ,
,
^ (),
/ ,
,
^ )*(
? (
(
V ,.LMN(
 (
 O(
V -/NOP(
 (
 Q(
V%8 D)" D)D)r   