a
    Þñh  ã                   @   s(   d dl Z ddlmZ G dd„ deƒZdS )é    Né   )Ú	Optimizerc                       s@   e Zd ZdZd‡ fdd„	Z‡ fd	d
„Ze ¡ ddd„ƒZ‡  Z	S )ÚRMSpropa8  Implements RMSprop algorithm.

    Proposed by G. Hinton in his
    `course <https://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf>`_.

    The centered version first appears in `Generating Sequences
    With Recurrent Neural Networks <https://arxiv.org/pdf/1308.0850v5.pdf>`_.

    The implementation here takes the square root of the gradient average before
    adding epsilon (note that TensorFlow interchanges these two operations). The effective
    learning rate is thus :math:`\alpha/(\sqrt{v} + \epsilon)` where :math:`\alpha`
    is the scheduled learning rate and :math:`v` is the weighted moving average
    of the squared gradient.

    Arguments:
        params (iterable): iterable of parameters to optimize or dicts defining
            parameter groups
        lr (float, optional): learning rate (default: 1e-2)
        momentum (float, optional): momentum factor (default: 0)
        alpha (float, optional): smoothing constant (default: 0.99)
        eps (float, optional): term added to the denominator to improve
            numerical stability (default: 1e-8)
        centered (bool, optional) : if ``True``, compute the centered RMSProp,
            the gradient is normalized by an estimation of its variance
        weight_decay (float, optional): weight decay (L2 penalty) (default: 0)

    ç{®Gáz„?ç®Gáz®ï?ç:Œ0âŽyE>r   Fc           	         s˜   d|kst d |¡ƒ‚d|ks,t d |¡ƒ‚d|ksBt d |¡ƒ‚d|ksXt d |¡ƒ‚d|ksnt d |¡ƒ‚t||||||d�}tt| ƒ ||¡ d S )Ng        zInvalid learning rate: {}zInvalid epsilon value: {}zInvalid momentum value: {}zInvalid weight_decay value: {}zInvalid alpha value: {})ÚlrÚmomentumÚalphaÚepsÚcenteredÚweight_decay)Ú
ValueErrorÚformatÚdictÚsuperr   Ú__init__)	ÚselfÚparamsr   r
   r   r   r	   r   Údefaults©Ú	__class__© úS/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/optim/rmsprop.pyr   "   s    zRMSprop.__init__c                    s8   t t| ƒ |¡ | jD ]}| dd¡ | dd¡ qd S )Nr	   r   r   F)r   r   Ú__setstate__Úparam_groupsÚ
setdefault)r   ÚstateÚgroupr   r   r   r   1   s    
zRMSprop.__setstate__Nc                 C   sø  d}|dur:t  ¡ � |ƒ }W d  ƒ n1 s00    Y  | jD �]°}|d D �] }|jdu r`qN|j}|jrttdƒ‚| j| }t|ƒdkrâd|d< t j|t j	d�|d< |d dkrÆt j|t j	d�|d	< |d
 rât j|t j	d�|d< |d }|d }|d  d7  < |d dk�r"|j
||d d�}| |¡j||d| d� |d
 �r†|d }	|	 |¡j|d| d� |j|	|	dd� ¡  |d ¡}
n| ¡  |d ¡}
|d dk�rÚ|d	 }| |d ¡ ||
¡ |j||d  d� qN|j||
|d  d� qNq@|S )z¶Performs a single optimization step.

        Arguments:
            closure (callable, optional): A closure that reevaluates the model
                and returns the loss.
        Nr   z)RMSprop does not support sparse gradientsr   Ústep)Zmemory_formatÚ
square_avgr	   Zmomentum_bufferr   Úgrad_avgr
   r   r   )r
   )Úvalueéÿÿÿÿr   r   )ÚtorchZenable_gradr   ÚgradZ	is_sparseÚRuntimeErrorr   ÚlenZ
zeros_likeZpreserve_formatÚaddZmul_Zaddcmul_Zadd_ZaddcmulZsqrt_ÚsqrtZaddcdiv_)r   ÚclosureZlossr   Úpr%   r   r    r
   r!   ÚavgÚbufr   r   r   r   7   sH    
$


 zRMSprop.step)r   r   r   r   r   F)N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r$   Zno_gradr   Ú__classcell__r   r   r   r   r      s
   r   )r$   Z	optimizerr   r   r   r   r   r   Ú<module>   s   