
    &Vf                     n   d dl mZ d dl mZ d dlmZ  ed           G d d                      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	dS )    )backend)ops)keras_exportzkeras.constraints.Constraintc                   4    e Zd ZdZd Zd Zed             ZdS )
ConstraintaG  Base class for weight constraints.

    A `Constraint` instance works like a stateless function.
    Users who subclass this
    class should override the `__call__()` method, which takes a single
    weight parameter and return a projected version of that parameter
    (e.g. normalized or clipped). Constraints can be used with various Keras
    layers via the `kernel_constraint` or `bias_constraint` arguments.

    Here's a simple example of a non-negative weight constraint:

    >>> class NonNegative(keras.constraints.Constraint):
    ...
    ...  def __call__(self, w):
    ...    return w * ops.cast(ops.greater_equal(w, 0.), dtype=w.dtype)

    >>> weight = ops.convert_to_tensor((-1.0, 1.0))
    >>> NonNegative()(weight)
    [0.,  1.]

    Usage in a layer:

    >>> keras.layers.Dense(4, kernel_constraint=NonNegative())
    c                     |S )ak  Applies the constraint to the input weight variable.

        By default, the inputs weight variable is not modified.
        Users should override this method to implement their own projection
        function.

        Args:
            w: Input weight variable.

        Returns:
            Projected variable (by default, returns unmodified inputs).
         selfws     ^/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/constraints/constraints.py__call__zConstraint.__call__!   s	         c                     i S )a  Returns a Python dict of the object config.

        A constraint config is a Python dictionary (JSON-serializable) that can
        be used to reinstantiate the same object.

        Returns:
            Python dict containing the configuration of the constraint object.
        r	   r   s    r   
get_configzConstraint.get_config0   s	     	r   c                      | di |S )a  Instantiates a weight constraint from a configuration dictionary.

        Example:

        ```python
        constraint = UnitNorm()
        config = constraint.get_config()
        constraint = UnitNorm.from_config(config)
        ```

        Args:
            config: A Python dictionary, the output of `get_config()`.

        Returns:
            A `keras.constraints.Constraint` instance.
        r	   r	   )clsconfigs     r   from_configzConstraint.from_config;   s    $ s}}V}}r   N)__name__
__module____qualname____doc__r   r   classmethodr   r	   r   r   r   r      sW         2  	 	 	   [  r   r   zkeras.constraints.MaxNormzkeras.constraints.max_normc                   &    e Zd ZdZddZd Zd ZdS )	MaxNormaz  MaxNorm weight constraint.

    Constrains the weights incident to each hidden unit
    to have a norm less than or equal to a desired value.

    Also available via the shortcut function `keras.constraints.max_norm`.

    Args:
        max_value: the maximum norm value for the incoming weights.
        axis: integer, axis along which to calculate weight norms.
            For instance, in a `Dense` layer the weight matrix
            has shape `(input_dim, output_dim)`,
            set `axis` to `0` to constrain each weight vector
            of length `(input_dim,)`.
            In a `Conv2D` layer with `data_format="channels_last"`,
            the weight tensor has shape
            `(rows, cols, input_depth, output_depth)`,
            set `axis` to `[0, 1, 2]`
            to constrain the weights of each filter tensor of size
            `(rows, cols, input_depth)`.

       r   c                 "    || _         || _        d S N	max_valueaxis)r   r"   r#   s      r   __init__zMaxNorm.__init__i   s    "			r   c                    t          j        |          }t          j        t          j        t          j        |          | j        d                    }t          j        |d| j                  }||t          j	                    |z   z  z  S )NTr#   keepdimsr   )
r   convert_to_tensorr   sqrtsumsquarer#   clipr"   epsilonr   r   normsdesireds       r   r   zMaxNorm.__call__m   sn    %a((ATYNNNOO(5!T^44Gw00589::r   c                      | j         | j        dS )Nr!   r!   r   s    r   r   zMaxNorm.get_configs   s    !^TY???r   N)r   r   r   r   r   r   r$   r   r   r	   r   r   r   r   P   sU         .   ; ; ;@ @ @ @ @r   r   zkeras.constraints.NonNegzkeras.constraints.non_negc                       e Zd ZdZd ZdS )NonNegz*Constrains the weights to be non-negative.c                     t          j        |          }|t          j        t          j        |d          |j                  z  S )N        )dtype)r   r(   r   castgreater_equalr7   r
   s     r   r   zNonNeg.__call__{   s;    %a((38C-a55QWEEEEEr   N)r   r   r   r   r   r	   r   r   r4   r4   w   s.        44F F F F Fr   r4   zkeras.constraints.UnitNormzkeras.constraints.unit_normc                   &    e Zd ZdZddZd Zd ZdS )UnitNorma  Constrains the weights incident to each hidden unit to have unit norm.

    Args:
        axis: integer, axis along which to calculate weight norms.
            For instance, in a `Dense` layer the weight matrix
            has shape `(input_dim, output_dim)`,
            set `axis` to `0` to constrain each weight vector
            of length `(input_dim,)`.
            In a `Conv2D` layer with `data_format="channels_last"`,
            the weight tensor has shape
            `(rows, cols, input_depth, output_depth)`,
            set `axis` to `[0, 1, 2]`
            to constrain the weights of each filter tensor of size
            `(rows, cols, input_depth)`.
    r   c                     || _         d S r    r#   )r   r#   s     r   r$   zUnitNorm.__init__   s    			r   c           	          t          j        |          }|t          j                    t          j        t          j        t          j        |          | j        d                    z   z  S )NTr&   )r   r(   r-   r   r)   r*   r+   r#   r
   s     r   r   zUnitNorm.__call__   sX    %a((Ohswsz!}}49tLLLMMN
 	
r   c                     d| j         iS )Nr#   r=   r   s    r   r   zUnitNorm.get_config   s    	""r   N)r   r2   r	   r   r   r;   r;      sP             
 
 
# # # # #r   r;   zkeras.constraints.MinMaxNormzkeras.constraints.min_max_normc                   &    e Zd ZdZd	dZd Zd ZdS )

MinMaxNorma  MinMaxNorm weight constraint.

    Constrains the weights incident to each hidden unit
    to have the norm between a lower bound and an upper bound.

    Args:
        min_value: the minimum norm for the incoming weights.
        max_value: the maximum norm for the incoming weights.
        rate: rate for enforcing the constraint: weights will be
            rescaled to yield
            `(1 - rate) * norm + rate * norm.clip(min_value, max_value)`.
            Effectively, this means that rate=1.0 stands for strict
            enforcement of the constraint, while rate<1.0 means that
            weights will be rescaled at each step to slowly move
            towards a value inside the desired interval.
        axis: integer, axis along which to calculate weight norms.
            For instance, in a `Dense` layer the weight matrix
            has shape `(input_dim, output_dim)`,
            set `axis` to `0` to constrain each weight vector
            of length `(input_dim,)`.
            In a `Conv2D` layer with `data_format="channels_last"`,
            the weight tensor has shape
            `(rows, cols, input_depth, output_depth)`,
            set `axis` to `[0, 1, 2]`
            to constrain the weights of each filter tensor of size
            `(rows, cols, input_depth)`.
    r6         ?r   c                 >    || _         || _        || _        || _        d S r    	min_valuer"   rater#   )r   rE   r"   rF   r#   s        r   r$   zMinMaxNorm.__init__   s"    ""				r   c                 N   t          j        |          }t          j        t          j        t          j        |          | j        d                    }| j        t          j        || j	        | j
                  z  d| j        z
  |z  z   }||t          j                    |z   z  z  S )NTr&      )r   r(   r   r)   r*   r+   r#   rF   r,   rE   r"   r-   r.   s       r   r   zMinMaxNorm.__call__   s    %a((ATYNNNOOIGGG49}%& 	 Gw00589::r   c                 8    | j         | j        | j        | j        dS )NrD   rD   r   s    r   r   zMinMaxNorm.get_config   s%    II	
 
 	
r   N)r6   rB   rB   r   r2   r	   r   r   rA   rA      sP         8   ; ; ;
 
 
 
 
r   rA   N)
	keras.srcr   r   keras.src.api_exportr   r   r   r4   r;   rA   r	   r   r   <module>rL      s               - - - - - - ,--F F F F F F F .-FR *,HIJJ#@ #@ #@ #@ #@j #@ #@ KJ#@L )+FGHHF F F F FZ F F IHF +-JKLL# # # # #z # # ML#> #%EF 2
 2
 2
 2
 2
 2
 2
 2
 2
 2
r   