
    2Vh&                     <   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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g d       G d de             Z eg d       G d de             Zy)    )ops)keras_export)standardize_dtype)Initializer)serialization_lib)scipyzkeras.initializers.Constantzkeras.initializers.constantc                   6    e Zd ZdZddZddZd Zed        Zy)	Constanta  Initializer that generates tensors with constant values.

    Only scalar values are allowed.
    The constant value provided must be convertible to the dtype requested
    when calling the initializer.

    Examples:

    >>> # Standalone usage:
    >>> initializer = Constant(10.)
    >>> values = initializer(shape=(2, 2))

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

    Args:
        value: A Python scalar.
    c                     || _         y N)value)selfr   s     \/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/initializers/constant_initializers.py__init__zConstant.__init__   s	    
    Nc                     t        |      }t        j                  | j                  |      t        j                  ||      z  S )Ndtype)shaper   )r   r   castr   onesr   r   r   s      r   __call__zConstant.__call__"   s8    !%(xx

%0388u4
 
 	
r   c                 D    dt        j                  | j                        iS Nr   )r   serialize_keras_objectr   r   s    r   
get_configzConstant.get_config(   s    *AA$**MNNr   c                 B    t        j                  |d         } | |      S r   )r   deserialize_keras_object)clsconfigr   s      r   from_configzConstant.from_config+   s     !::6'?K5zr   )g        r   )	__name__
__module____qualname____doc__r   r   r   classmethodr#    r   r   r
   r
   	   s+    (
O  r   r
   zkeras.initializers.Zeroszkeras.initializers.zerosc                       e Zd ZdZddZy)Zerosa.  Initializer that generates tensors initialized to 0.

    Examples:

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

    >>> # Usage in a Keras layer:
    >>> initializer = Zeros()
    >>> layer = Dense(units=3, kernel_initializer=initializer)
    Nc                 F    t        |      }t        j                  ||      S   Returns a tensor object initialized as specified by the initializer.

        Args:
            shape: Shape of the tensor.
            dtype: Optional dtype of the tensor. Only numeric or boolean dtypes
                are supported. If not specified, `keras.backend.floatx()`
                is used, which default to `float32` unless you configured it
                otherwise (via `keras.backend.set_floatx(float_dtype)`).
        r   )r   r   zerosr   s      r   r   zZeros.__call__@   s     "%(yye,,r   r   r$   r%   r&   r'   r   r)   r   r   r+   r+   1   s    -r   r+   zkeras.initializers.Oneszkeras.initializers.onesc                       e Zd ZdZddZy)Onesa\  Initializer that generates tensors initialized to 1.

    Also available via the shortcut function `ones`.

    Examples:

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

    >>> # Usage in a Keras layer:
    >>> initializer = Ones()
    >>> layer = Dense(3, kernel_initializer=initializer)
    Nc                 F    t        |      }t        j                  ||      S r-   )r   r   r   r   s      r   r   zOnes.__call___   s     "%(xxU++r   r   r0   r)   r   r   r2   r2   N   s    ,r   r2   )zkeras.initializers.Identityzkeras.initializers.identityz&keras.initializers.IdentityInitializerc                        e Zd ZdZddZddZy)Identitya  Initializer that generates the identity matrix.

    Only usable for generating 2D matrices.

    Examples:

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

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

    Args:
        gain: Multiplicative factor to apply to the identity matrix.
    c                     || _         y r   )gain)r   r7   s     r   r   zIdentity.__init__   s	    	r   Nc                     t        |      dk7  rt        d| dt        |       d      t        |      }| j                  t	        j
                  |d|iz  S )r.      zNIdentity matrix initializer can only be used for 2D matrices. Received: shape=z	 of rank .r   )len
ValueErrorr   r7   r   eyer   s      r   r   zIdentity.__call__   sa     u:?##('3u:,aA  "%(yy377E7777r   )g      ?r   )r$   r%   r&   r'   r   r   r)   r   r   r5   r5   m   s    $8r   r5   )zkeras.initializers.STFTzkeras.initializers.stftz"keras.initializers.STFTInitializerc                   (    e Zd ZdZ	 ddZddZd Zy)STFTa  Initializer of Conv kernels for Short-term Fourier Transformation (STFT).

    Since the formula involves complex numbers, this class compute either the
    real or the imaginary components of the final output.

    Additionally, this initializer supports windowing functions across the time
    dimension as commonly used in STFT. Windowing functions from the module
    `scipy.signal.windows` are supported, including the common `hann` and
    `hamming` windowing functions. This layer supports periodic windows and
    scaling-based normalization.

    This is primarily intended for use in the `STFTSpectrogram` layer.

    Examples:

    >>> # Standalone usage:
    >>> initializer = STFTInitializer("real", "hann", "density", False)
    >>> values = initializer(shape=(128, 1, 513))

    Args:
        side: String, `"real"` or `"imag"` deciding if the kernel will compute
            the real side or the imaginary side of the output. Defaults to
            `"real"`.
        window: String for the name of the windowing function in the
            `scipy.signal.windows` module, or array_like for the window values,
            or `None` for no windowing.
        scaling: String, `"density"` or `"spectrum"` for scaling of the window
            for normalization, either L2 or L1 normalization.
            `None` for no scaling.
        periodic: Boolean, if True, the window function will be treated as
            periodic. Defaults to `False`.
    c                     |dvrt        d|       t        |t              r t        j                  j                  |d       ||dvrt        d|       || _        || _        || _        || _	        y )N)realimagz%side should be 'real' or 'imag', not    )densityspectrumzQScaling is invalid, it must be `None`, 'density' or 'spectrum'. Received scaling=)
r<   
isinstancestrr   signal
get_windowsidewindowscalingperiodic)r   rJ   rK   rL   rM   s        r   r   zSTFT.__init__   s     ''DTFKLLfc"LL##FA.72I#I33:)=  	 r   Nc                    t        |      }|\  }}}d}d}| j                  ;| j                  }t        |t              r+t        j
                  j                  ||| j                        }t        j                  ||      }t        |j                        dk7  s|j                  d   |k7  rt        d|j                         t        j                  ||ddg      }| j                  dk(  r<t        j                  t        j                   t        j"                  |                  }n7| j                  dk(  r(t        j                   t        j$                  |            }|dz
  dz  }t        j                  t        j&                  ||      dd|f      |z  }	t        j                  t        j&                  ||      |ddf      }
d	|
z  |	z  t        j(                  t        j*                  d|            z  }| j,                  d
k(  r*t        j*                  t        j.                  |      |      }n)t        j*                  t        j0                  |      |      }|||z  |z  }|S )a  Returns a tensor object initialized as specified by the initializer.

        The shape is assumed to be `(T, 1, F // 2 + 1)`, where `T` is the size
        of the given window, and `F` is the number of frequency bands. Only half
        the frequency bands are used, which is a common practice in STFT,
        because the second half are the conjugates of the first half in
        a reversed order.

        Args:
            shape: Shape of the tensor.
            dtype: Optional dtype of the tensor. Only numeric or boolean dtypes
                are supported. If not specified, `keras.backend.floatx()`
                is used, which default to `float32` unless you configured it
                otherwise (via `keras.backend.set_floatx(float_dtype)`).
        NrC   r   zMThe shape of `window` must be equal to [frame_length].Received: window shape=rD   rE   r9   rA   )r   rK   rF   rG   r   rH   rI   rM   r   convert_to_tensorr;   r   r<   reshaperL   sqrtsumsquareabsarangearccosr   rJ   cossin)r   r   r   frame_lengthinput_channels
fft_lengthwinrL   _fft_lengthfreqtimeargskernels                r   r   zSTFT.__call__   s     "%(380nj;;"++C#s# ll--c<O''59C399~"ciim|&C ..1ii[:  ++cL!Q#78C||y(((3773::c?#;<+''#''#,/!A~*KK

:U;aJ=OP 	 {{JJ|51L!Q3G
 Dy4#**SXXb%-@"AA99XXcggdmU3FXXcggdmU3F?c\G+Fr   c                 `    | j                   | j                  | j                  | j                  dS )NrJ   rK   rM   rL   re   r   s    r   r   zSTFT.get_config  s(    IIkk||	
 	
r   )rA   hannrD   Fr   )r$   r%   r&   r'   r   r   r   r)   r   r   r?   r?      s    D GL!$8t
r   r?   N)	keras.srcr   keras.src.api_exportr   keras.src.backendr   "keras.src.initializers.initializerr   keras.src.savingr   keras.src.utils.module_utilsr   r
   r+   r2   r5   r?   r)   r   r   <module>rm      s     - / : . . ,.KLM${ $ N$N )+EFG-K - H-8 (*CDE,; , F,< &8{ &8&8R t
; t
t
r   