Ë
    •\;jâ  ã                   ób   — 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 Z G d	„ d
e
«      Zy)é    N)Ú_C_opsé   )Ú	framework)Úno_grad)Úin_dynamic_or_pir_modeé   )Ú	Optimizerc                   óL   ‡ — e Zd ZdZ	 	 	 	 	 	 dˆ fd„	Zd„ Zed„ «       Zd„ Zˆ xZ	S )ÚSGDaM	  
    Optimizer of the stochastic gradient descent algorithm.

    .. math::

        param\_out = param - learning\_rate * grad

    Parameters:
        learning_rate (float|Tensor|LearningRateDecay, optional): The learning rate used to update ``Parameter``.
            It can be a float value, a ``Tensor`` with a float type or a LearningRateDecay. The default value is 0.001.
        parameters (list|tuple, optional): List/Tuple of ``Tensor`` to update to minimize ``loss``. \
            This parameter is required in dygraph mode. \
            The default value is None in static graph mode, at this time all parameters will be updated.
        weight_decay (float|WeightDecayRegularizer, optional): The strategy of regularization. \
            It can be a float value as coeff of L2 regularization or \
            :ref:`api_paddle_regularizer_L1Decay`, :ref:`api_paddle_regularizer_L2Decay`.
            If a parameter has set regularizer using :ref:`api_paddle_ParamAttr` already, \
            the regularization setting here in optimizer will be ignored for this parameter. \
            Otherwise, the regularization setting here in optimizer will take effect. \
            Default None, meaning there is no regularization.
        grad_clip (GradientClipBase, optional): Gradient clipping strategy, it's an instance of
            some derived class of ``GradientClipBase`` . There are three clipping strategies
            ( :ref:`api_paddle_nn_ClipGradByGlobalNorm` , :ref:`api_paddle_nn_ClipGradByNorm` ,
            :ref:`api_paddle_nn_ClipGradByValue` ). Default None, meaning there is no gradient clipping.
        name (str, optional): The default value is None. Normally there is no need for user
                to set this property. For more information, please refer to
                :ref:`api_guide_Name` .

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> inp = paddle.uniform(min=-0.1, max=0.1, shape=[10, 10], dtype='float32')
            >>> linear = paddle.nn.Linear(10, 10)
            >>> inp = paddle.to_tensor(inp)
            >>> out = linear(inp)
            >>> loss = paddle.mean(out)
            >>> sgd = paddle.optimizer.SGD(learning_rate=0.1, parameters=linear.parameters(), weight_decay=0.01)
            >>> out.backward()
            >>> sgd.step()
            >>> sgd.clear_grad()

    c                 ór   •— |€t        d«      ‚t        ‰| �	  |||||¬«       d| _        || _        i | _        y )Nzlearning_rate is not set)Úlearning_rateÚ
parametersÚweight_decayÚ	grad_clipÚnameÚsgd)Ú
ValueErrorÚsuperÚ__init__ÚtypeÚ_multi_precisionÚ_master_weights)Úselfr   r   r   r   Úmulti_precisionr   Ú	__class__s          €ú]G:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/optimizer/sgd.pyr   zSGD.__init__I   sQ   ø€ ð Ð ÜÐ7Ó8Ð8Ü‰ÑØ'Ø!Ø%ØØð 	ô 	
ð ˆŒ	Ø /ˆÔØ!ˆÕó    c                 óö  — t        |t        j                  «      sJ ‚t        |t        «      r| j	                  |«      }|D ]·  }|j
                  | j                  v rŒ| j                  rR| j                  |j                  «      r7| j                  |«      }| j                  j                  |j
                  «       Œz| j                  |j                  «      sŒ–| j                  rŒ£t        j                  d«       Œ¹ y )Nz˜Accumulating with FP16/BF16 in optimizer can lead to poor accuracy or slow convergence.Consider using multi_precision=True option of the Adam optimizer.)Ú
isinstancer   ÚBlockÚdictÚ_update_param_groupr   Ú_already_create_accumulaterr   Ú_is_dtype_fp16_or_bf16ÚdtypeÚ_create_master_weightÚaddÚwarningsÚwarn)r   Úblockr   ÚpÚmaster_ps        r   Ú_create_accumulatorszSGD._create_accumulators_   sÃ   € Ü˜%¤§¡Ô1Ð1Ð1Ü�j¤$Ô'Ø×1Ñ1°*Ó=ˆJó ˆAØ�v‰v˜×9Ñ9Ñ9ØØ×$Ò$¨×)DÑ)DÀQÇWÁWÔ)MØ×5Ñ5°aÓ8�Ø×0Ñ0×4Ñ4°Q·V±VÔ<Øà×+Ñ+¨A¯G©GÕ4Ø×-Ó-ä—‘ðXõñ r   c                 ó  — t        |t        «      r| j                  |«      }| j                  xr | j	                  |d   j
                  «      }|r| j                  |d   j                     nd }| j                  |«      }t        «       r t        j                  |d   ||d   ||«       y t        |t        j                  «      sJ ‚|d   |d   |dœ}d|d   i}d|i}|r
||d<   ||d<   |j                  | j                  |||d¬	«      }	|	S )
Nr   r   )ÚParamÚGradÚLearningRateÚParamOutr   ÚMasterParamÚMasterParamOutT)r   ÚinputsÚoutputsÚattrsÚstop_gradient)r   r!   r"   r   r$   r%   r   r   Ú_create_param_lrr   r   Úsgd_r   r    Ú	append_opr   )
r   r*   Úparam_and_gradÚfind_masterÚmaster_weightÚlrr5   r6   r7   Úsgd_ops
             r   Ú_append_optimize_opzSGD._append_optimize_opu   sA  € ä�n¤dÔ+Ø!×5Ñ5°nÓEˆNà×+Ñ+ò 
°×0KÑ0KØ˜1Ñ×#Ñ#ó1
ˆñ
 ð × Ñ  °Ñ!2×!7Ñ!7Ò8àð 	ð ×"Ñ" >Ó2ˆÜ!Ô#Ü�K‰KØ˜qÑ!ØØ˜qÑ!ØØôð ä˜e¤Y§_¡_Ô5Ð5Ð5ð (¨Ñ*Ø& qÑ)Ø "ñˆFð " >°!Ñ#4Ð5ˆGà&¨Ð4ˆEáØ(5��}Ñ%Ø,9�Ð(Ñ)à—_‘_Ø—Y‘YØØØØ"ð %ó ˆFð ˆMr   c                 ó(   — |j                  d«      }|S )NÚparams)Úget)r   r   s     r   r"   zSGD._update_param_group¨   s   € Ø—^‘^ HÓ-ˆ
ØÐr   )gü©ñÒMbP?NNNFN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r-   r   rA   r"   Ú__classcell__)r   s   @r   r   r      sA   ø„ ñ+ð^ ØØØØØõ"ò,ð, ñ0ó ð0ödr   r   )r(   Úpaddler   Úbaser   Úbase.dygraphr   Úbase.frameworkr   Ú	optimizerr	   Ú__all__r   © r   r   Ú<module>rQ      s,   ðó å å Ý "Ý 3Ý  à
€ôOˆ)õ Or   