同步操作将从 PaddlePaddle/Paddle 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.__all__ = ['L1Decay', 'L2Decay']import paddle.fluid as fluidclass L1Decay(fluid.regularizer.L1Decay):r"""Implement the L1 Weight Decay Regularization, which encourages the weights to be sparse.It can be set in :ref:`api_paddle_ParamAttr` or ``optimizer`` (such as :ref:`api_paddle_optimizer_Momentum` ).When set in ``ParamAttr`` , it only takes effect for trainable parameters in this layer. When set in``optimizer`` , it takes effect for all trainable parameters. When set together, ``ParamAttr`` hashigher priority than ``optimizer`` , which means that for a trainable parameter, if regularizer is definedin its ParamAttr, then the regularizer in Optimizer will be ignored. Otherwise the regularizerin Optimizer will be used.In the implementation, the loss function of L1 Weight Decay Regularization is as follows:.. math::loss = coeff * reduce\_sum(abs(x))Args:coeff(float, optional): regularization coeff. Default:0.0.Examples:.. code-block:: python# Example1: set Regularizer in optimizerimport paddlefrom paddle.regularizer import L1Decayimport numpy as nplinear = paddle.nn.Linear(10, 10)inp = paddle.rand(shape=[10, 10], dtype="float32")out = linear(inp)loss = paddle.mean(out)beta1 = paddle.to_tensor([0.9], dtype="float32")beta2 = paddle.to_tensor([0.99], dtype="float32")momentum = paddle.optimizer.Momentum(learning_rate=0.1,parameters=linear.parameters(),weight_decay=L1Decay(0.0001))back = out.backward()momentum.step()momentum.clear_grad()# Example2: set Regularizer in parameters# Set L1 regularization in parameters.# Global regularizer does not take effect on my_conv2d for this case.from paddle.nn import Conv2Dfrom paddle import ParamAttrfrom paddle.regularizer import L2Decaymy_conv2d = Conv2D(in_channels=10,out_channels=10,kernel_size=1,stride=1,padding=0,weight_attr=ParamAttr(regularizer=L2Decay(coeff=0.01)),bias_attr=False)"""def __init__(self, coeff=0.0):super(L1Decay, self).__init__(coeff)class L2Decay(fluid.regularizer.L2Decay):r"""Implement the L2 Weight Decay Regularization, which helps to prevent the model over-fitting.It can be set in :ref:`api_paddle_ParamAttr` or ``optimizer`` (such as :ref:`api_paddle_optimizer_Momentum` ).When set in ``ParamAttr`` , it only takes effect for trainable parameters in this layer. When set in``optimizer`` , it takes effect for all trainable parameters. When set together, ``ParamAttr`` hashigher priority than ``optimizer`` , which means that for a trainable parameter, if regularizer is definedin its ParamAttr, then the regularizer in Optimizer will be ignored. Otherwise the regularizerin Optimizer will be used.In the implementation, the loss function of L2 Weight Decay Regularization is as follows:.. math::loss = 0.5 * coeff * reduce\_sum(square(x))Args:regularization_coeff(float, optional): regularization coeff. Default:0.0Examples:.. code-block:: python# Example1: set Regularizer in optimizerimport paddlefrom paddle.regularizer import L2Decayimport numpy as nplinear = paddle.nn.Linear(10, 10)inp = paddle.rand(shape=[10, 10], dtype="float32")out = linear(inp)loss = paddle.mean(out)beta1 = paddle.to_tensor([0.9], dtype="float32")beta2 = paddle.to_tensor([0.99], dtype="float32")momentum = paddle.optimizer.Momentum(learning_rate=0.1,parameters=linear.parameters(),weight_decay=L2Decay(0.0001))back = out.backward()momentum.step()momentum.clear_grad()# Example2: set Regularizer in parameters# Set L2 regularization in parameters.# Global regularizer does not take effect on my_conv2d for this case.from paddle.nn import Conv2Dfrom paddle import ParamAttrfrom paddle.regularizer import L2Decaymy_conv2d = Conv2D(in_channels=10,out_channels=10,kernel_size=1,stride=1,padding=0,weight_attr=ParamAttr(regularizer=L2Decay(coeff=0.01)),bias_attr=False)"""def __init__(self, coeff=0.0):super(L2Decay, self).__init__(coeff)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。