a
    .Sic§1  ã                   @   sz   d 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
G dd„ dejƒƒZdS )aQ  One-line documentation for rmsprop module.

rmsprop algorithm [tieleman2012rmsprop]

A detailed description of rmsprop.

- maintain a moving (discounted) average of the square of gradients
- divide gradient by the root of this average

mean_square = decay * mean_square{t-1} + (1-decay) * gradient ** 2
mom = momentum * mom{t-1} + learning_rate * g_t / sqrt(mean_square + epsilon)
delta = - mom

This implementation of RMSProp uses plain momentum, not Nesterov momentum.

The centered version additionally maintains a moving (discounted) average of the
gradients, and uses that average to estimate the variance:

mean_grad = decay * mean_grad{t-1} + (1-decay) * gradient
mean_square = decay * mean_square{t-1} + (1-decay) * gradient ** 2
mom = momentum * mom{t-1} + learning_rate * g_t /
    sqrt(mean_square - mean_grad**2 + epsilon)
delta = - mom
é    )Úops)Ú	array_ops)Úinit_ops)Úmath_ops)Ú	optimizer)Útraining_ops)Ú	tf_exportztrain.RMSPropOptimizer)Úv1c                       sR   e Zd ZdZd‡ fdd„	Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
‡  ZS )ÚRMSPropOptimizera  Optimizer that implements the RMSProp algorithm (Tielemans et al.

  2012).

  References:
    Coursera slide 29:
    Hinton, 2012
    ([pdf](http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf))

  @compatibility(TF2)
  tf.compat.v1.train.RMSPropOptimizer is compatible with eager mode and
  `tf.function`.
  When eager execution is enabled, `learning_rate`, `decay`, `momentum`,
  and `epsilon` can each be a callable that
  takes no arguments and returns the actual value to use. This can be useful
  for changing these values across different invocations of optimizer
  functions.

  To switch to native TF2 style, use [`tf.keras.optimizers.RMSprop`]
  (https://www.tensorflow.org/api_docs/python/tf/keras/optimizers/RMSprop)
  instead. Please notice that due to the implementation differences,
  `tf.keras.optimizers.RMSprop` and
  `tf.compat.v1.train.RMSPropOptimizer` may have slight differences in
  floating point numerics even though the formula used for the variable
  updates still matches.

  #### Structural mapping to native TF2

  Before:

  ```python
  optimizer = tf.compat.v1.train.RMSPropOptimizer(
    learning_rate=learning_rate,
    decay=decay,
    momentum=momentum,
    epsilon=epsilon)
  ```

  After:

  ```python
  optimizer = tf.keras.optimizers.RMSprop(
    learning_rate=learning_rate,
    rho=decay,
    momentum=momentum,
    epsilon=epsilon)
  ```

  #### How to map arguments
  | TF1 Arg Name       | TF2 Arg Name   | Note                             |
  | ------------------ | -------------  | -------------------------------  |
  | `learning_rate`    | `learning_rate`| Be careful of setting           |
  : : : learning_rate tensor value computed from the global step.          :
  : : : In TF1 this was usually meant to imply a dynamic learning rate and :
  : : : would recompute in each step. In TF2 (eager + function) it will    :
  : : : treat it as a scalar value that only gets computed once instead of :
  : : : a symbolic placeholder to be computed each time.                   :
  | `decay`            | `rho`          | -                                |
  | `momentum`         | `momentum`     | -                                |
  | `epsilon`          | `epsilon`      | Default value is 1e-10 in TF1,   |
  :                    :                : but 1e-07 in TF2.                :
  | `use_locking`      | -              | Not applicable in TF2.           |

  #### Before & after usage example
  Before:

  ```python
  x = tf.Variable([1,2,3], dtype=tf.float32)
  grad = tf.constant([0.1, 0.2, 0.3])
  optimizer = tf.compat.v1.train.RMSPropOptimizer(learning_rate=0.001)
  optimizer.apply_gradients(zip([grad], [x]))
  ```

  After:

  ```python
  x = tf.Variable([1,2,3], dtype=tf.float32)
  grad = tf.constant([0.1, 0.2, 0.3])
  optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.001)
  optimizer.apply_gradients(zip([grad], [x]))
  ```

  @end_compatibility
  çÍÌÌÌÌÌì?ç        ç»½×Ùß|Û=FÚRMSPropc                    sL   t t| ƒ ||¡ || _|| _|| _|| _|| _d| _d| _	d| _
d| _dS )a:  Construct a new RMSProp optimizer.

    Note that in the dense implementation of this algorithm, variables and their
    corresponding accumulators (momentum, gradient moving average, square
    gradient moving average) will be updated even if the gradient is zero
    (i.e. accumulators will decay, momentum will be applied). The sparse
    implementation (used when the gradient is an `IndexedSlices` object,
    typically because of `tf.gather` or an embedding lookup in the forward pass)
    will not update variable slices or their accumulators unless those slices
    were used in the forward pass (nor is there an "eventual" correction to
    account for these omitted updates). This leads to more efficient updates for
    large embedding lookup tables (where most of the slices are not accessed in
    a particular graph execution), but differs from the published algorithm.

    Args:
      learning_rate: A Tensor or a floating point value.  The learning rate.
      decay: Discounting factor for the history/coming gradient
      momentum: A scalar tensor.
      epsilon: Small value to avoid zero denominator.
      use_locking: If True use locks for update operation.
      centered: If True, gradients are normalized by the estimated variance of
        the gradient; if False, by the uncentered second moment. Setting this to
        True may help with training, but is slightly more expensive in terms of
        computation and memory. Defaults to False.
      name: Optional name prefix for the operations created when applying
        gradients. Defaults to "RMSProp".

    N)Úsuperr
   Ú__init__Ú_learning_rateÚ_decayÚ	_momentumÚ_epsilonÚ	_centeredÚ_learning_rate_tensorÚ_decay_tensorÚ_momentum_tensorÚ_epsilon_tensor)ÚselfÚlearning_rateÚdecayÚmomentumÚepsilonÚuse_lockingÚcenteredÚname©Ú	__class__© ú^/var/www/html/django/DPS/env/lib/python3.9/site-packages/tensorflow/python/training/rmsprop.pyr   ˆ   s    $zRMSPropOptimizer.__init__c              	   C   sz   |D ]p}|  ¡  ¡ r&tj|jjd}n
t |¡}|  |||  ¡ |jjd| j	¡ | j
rd|  |d| j	¡ |  |d| j	¡ qd S )N)ÚdtypeÚrmsÚmgr   )Ú	get_shapeÚis_fully_definedr   Úones_initializerr&   Ú
base_dtyper   Ú	ones_likeÚ"_get_or_make_slot_with_initializerÚ_namer   Ú_zeros_slot)r   Úvar_listÚvZinit_rmsr$   r$   r%   Ú_create_slots¹   s    
þzRMSPropOptimizer._create_slotsc                 C   st   |   | j¡}|   | j¡}|   | j¡}|   | j¡}tj|dd| _tj|dd| _tj|dd| _	tj|dd| _
d S )Nr   )r!   r   r   r   )Ú_call_if_callabler   r   r   r   r   Úconvert_to_tensorr   r   r   r   )r   Úlrr   r   r   r$   r$   r%   Ú_prepareÆ   s    zRMSPropOptimizer._preparec                 C   sà   |   |d¡}|   |d¡}| jr„|   |d¡}tj||||t | j|jj¡t | j	|jj¡t | j
|jj¡t | j|jj¡|| jd
jS tj|||t | j|jj¡t | j	|jj¡t | j
|jj¡t | j|jj¡|| jd	jS d S ©Nr'   r   r(   )r   )Úget_slotr   r   Úapply_centered_rms_propr   Úcastr   r&   r,   r   r   r   Ú_use_lockingÚopÚapply_rms_prop©r   ÚgradÚvarr'   Úmomr(   r$   r$   r%   Ú_apply_denseÑ   s6    ö÷zRMSPropOptimizer._apply_densec                 C   sê   |   |d¡}|   |d¡}| jrŠ|   |d¡}tj|j|j|j|jt | j|jj	¡t | j
|jj	¡t | j|jj	¡t | j|jj	¡|| jd
S tj|j|j|jt | j|jj	¡t | j
|jj	¡t | j|jj	¡t | j|jj	¡|| jd	S d S r8   )r9   r   r   Ú resource_apply_centered_rms_propÚhandler   r;   r   r&   r,   r   r   r   r<   Úresource_apply_rms_propr?   r$   r$   r%   Ú_resource_apply_denseí   s6    ö÷z&RMSPropOptimizer._resource_apply_densec                 C   sè   |   |d¡}|   |d¡}| jrˆ|   |d¡}tj||||t | j|jj¡t | j	|jj¡t | j
|jj¡t | j|jj¡|j|j| jdS tj|||t | j|jj¡t | j	|jj¡t | j
|jj¡t | j|jj¡|j|j| jd
S d S r8   )r9   r   r   Úsparse_apply_centered_rms_propr   r;   r   r&   r,   r   r   r   ÚvaluesÚindicesr<   Úsparse_apply_rms_propr?   r$   r$   r%   Ú_apply_sparse	  s:    õözRMSPropOptimizer._apply_sparsec                 C   sÞ   |   |d¡}|   |d¡}| jr„|   |d¡}tj|j|j|j|jt | j|j¡t | j	|j¡t | j
|j¡t | j|j¡||| jdS tj|j|j|jt | j|j¡t | j	|j¡t | j
|j¡t | j|j¡||| jd
S d S r8   )r9   r   r   Ú'resource_sparse_apply_centered_rms_proprE   r   r;   r   r&   r   r   r   r<   Úresource_sparse_apply_rms_prop)r   r@   rA   rJ   r'   rB   r(   r$   r$   r%   Ú_resource_apply_sparse'  s:    õöz'RMSPropOptimizer._resource_apply_sparse)r   r   r   FFr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r3   r7   rC   rG   rL   rO   Ú__classcell__r$   r$   r"   r%   r
   1   s   W      ù1r
   N)rS   Útensorflow.python.frameworkr   Útensorflow.python.opsr   r   r   Útensorflow.python.trainingr   r   Ú tensorflow.python.util.tf_exportr   Ú	Optimizerr
   r$   r$   r$   r%   Ú<module>   s   
