ASGD¶
- class paddle.optimizer. ASGD ( learning_rate=0.001, batch_num=1, parameters=None, weight_decay=None, grad_clip=None, multi_precision=False, name=None ) [source]
-
Optimizer of the ASGD algorithm.Please refer to this for details: Minimizing Finite Sums with the Stochastic Average Gradient.
\[\begin{split}\begin{aligned} &\hspace{0mm} d=0,\ y_i=0\ \textbf{for}\ i=1,2,...,n \\ &\hspace{0mm} \textbf{for}\ \: m=0,1,...\ \textbf{do} \: \\ &\hspace{5mm} i=m\ \%\ n \\ &\hspace{5mm} d=d-y_i+f_i{}'(x) \\ &\hspace{5mm} y_i=f_i{}'(x) \\ &\hspace{5mm} x=x-learning\_rate(\frac{d}{\mathrm{min}(m+1,\ n)}+\lambda x) \\ &\hspace{0mm} \textbf{end for} \\ \end{aligned}\end{split}\]- Parameters
-
learning_rate (float|Tensor|LearningRateDecay, optional) – The learning rate used to update
Parameter
. It can be a float value, aTensor
with a float type or a LearningRateDecay. The default value is 0.001.batch_num (int, optional) – The number of batches needed to complete one epoch. Assuming the total number of samples is
all
, it is recommended to setbatch_num
toall
/batch_size
. In situations where the graphics memory is tight, it is possible to reduce the batch_num appropriately. The default value is 1.parameters (list|tuple, optional) – List/Tuple of
Tensor
to update to minimizeloss
. 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 L1Decay, L2Decay. If a parameter has set regularizer using 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 ( ClipGradByGlobalNorm , ClipGradByNorm , ClipGradByValue ). Default None, meaning there is no gradient clipping.multi_precision (bool, optional) – In mixed precision training scenarios based on GPU, this parameter is mainly used to ensure the numerical stability of gradient updates. When it is set to True, the optimizer will save a backup of FP32 type parameters with an equal value for FP16 type parameters. When updating gradients, first increase the gradient type to FP32, and then assign it to the FP32 type parameter backup. Finally, the updated FP32 type value will be converted to FP16 type first, and then assigned to the actual FP16 type parameters participating in the calculation. The default value is False.
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 Name .
Examples
>>> 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) >>> asgd = paddle.optimizer.ASGD(learning_rate=0.001, batch_num=10, parameters=linear.parameters(), weight_decay=0.01) >>> out.backward() >>> asgd.step() >>> asgd.clear_grad()
-
append_regularization_ops
(
parameters_and_grads,
regularization=None
)
append_regularization_ops¶
-
Create and add backward regularization Operators
Creates and adds backward regularization operators in the BlockDesc. This will add gradients of the regularizer function to the gradients of the parameters and return these modified gradients. This is the same as implementing weight decay in optimizers for regularization.
- Parameters
-
parameters_and_grads – A list of (parameters, gradients) pairs that need to be regularized.
regularization – A global regularizer. If the parameter is not set. It will be applied with regularizer.
- Returns
-
list of (parameters, gradients) pair with the regularized gradient
- Return type
-
list[(Variable, Variable)]
- Raises
-
Exception – Unknown regularization type
-
clear_grad
(
set_to_zero=True
)
clear_grad¶
-
Clear the gradients of all optimized parameters for model.
If not, new gradient will accumulat on previous gradient.
There are two method to clear grad: set_to_zero or delete grad.
- Parameters
-
set_to_zero (bool, optional) – If set grads to zero or not, default is True.
- Returns
-
None
Examples
>>> import paddle >>> a = paddle.arange(26, dtype="float32").reshape([2, 13]) >>> linear = paddle.nn.Linear(13, 5) >>> # This can be any optimizer supported by dygraph. >>> adam = paddle.optimizer.Adam(learning_rate = 0.01, ... parameters = linear.parameters()) >>> out = linear(a) >>> out.backward() >>> adam.step() >>> adam.clear_grad()
-
get_lr
(
)
get_lr¶
-
Get current learning rate of optimizer. If ‘LRScheduler’ is not used, the return value is all the same. If ‘LRScheduler’ is used, the return value is the current scheduled learing rete.
- Returns
-
The current learning rate of optimizer.
- Return type
-
float
Examples
>>> # train on default dynamic graph mode >>> import paddle >>> import numpy as np >>> emb = paddle.nn.Embedding(10, 3) >>> ## example1: LRScheduler is not used, return the same value is all the same >>> adam = paddle.optimizer.Adam(0.01, parameters = emb.parameters()) >>> for batch in range(10): ... input = paddle.randint(low=0, high=5, shape=[5]) ... out = emb(input) ... out.backward() ... print("Learning rate of step{}: {}".format(batch, adam.get_lr())) # 0.01 ... adam.step() Learning rate of step0: 0.01 Learning rate of step1: 0.01 Learning rate of step2: 0.01 Learning rate of step3: 0.01 Learning rate of step4: 0.01 Learning rate of step5: 0.01 Learning rate of step6: 0.01 Learning rate of step7: 0.01 Learning rate of step8: 0.01 Learning rate of step9: 0.01 >>> ## example2: StepDecay is used, return the scheduled learning rate >>> scheduler = paddle.optimizer.lr.StepDecay(learning_rate=0.5, step_size=2, gamma=0.1) >>> adam = paddle.optimizer.Adam(scheduler, parameters = emb.parameters()) >>> for batch in range(10): ... input = paddle.randint(low=0, high=5, shape=[5]) ... out = emb(input) ... out.backward() ... print("Learning rate of step{}: {}".format(batch, adam.get_lr())) # 0.5->0.05... ... adam.step() ... scheduler.step() Learning rate of step0: 0.5 Learning rate of step1: 0.5 Learning rate of step2: 0.05 Learning rate of step3: 0.05 Learning rate of step4: 0.005000000000000001 Learning rate of step5: 0.005000000000000001 Learning rate of step6: 0.0005000000000000001 Learning rate of step7: 0.0005000000000000001 Learning rate of step8: 5.000000000000001e-05 Learning rate of step9: 5.000000000000001e-05 >>> # train on static graph mode >>> paddle.enable_static() >>> main_prog = paddle.static.Program() >>> start_prog = paddle.static.Program() >>> with paddle.static.program_guard(main_prog, start_prog): ... x = paddle.static.data(name='x', shape=[None, 10]) ... z = paddle.static.nn.fc(x, 100) ... loss = paddle.mean(z) ... scheduler = paddle.optimizer.lr.StepDecay(learning_rate=0.5, step_size=2, gamma=0.1) ... adam = paddle.optimizer.Adam(learning_rate=scheduler) ... adam.minimize(loss) >>> exe = paddle.static.Executor() >>> exe.run(start_prog) >>> for batch in range(10): ... print("Learning rate of step{}: {}".format(batch, adam.get_lr())) # 0.5->0.05->0.005... ... out = exe.run(main_prog, feed={'x': np.random.randn(3, 10).astype('float32')}) ... scheduler.step() Learning rate of step0: 0.5 Learning rate of step1: 0.5 Learning rate of step2: 0.05 Learning rate of step3: 0.05 Learning rate of step4: 0.005000000000000001 Learning rate of step5: 0.005000000000000001 Learning rate of step6: 0.0005000000000000001 Learning rate of step7: 0.0005000000000000001 Learning rate of step8: 5.000000000000001e-05 Learning rate of step9: 5.000000000000001e-05
-
minimize
(
loss,
startup_program=None,
parameters=None,
no_grad_set=None
)
minimize¶
-
Add operations to minimize
loss
by updatingparameters
.- Parameters
-
loss (Tensor) – A
Tensor
containing the value to minimize.startup_program (Program, optional) – Program for initializing parameters in
parameters
. The default value is None, at this time default_startup_program will be used.parameters (list, optional) – List of
Tensor
orTensor.name
to update to minimizeloss
. The default value is None, at this time all parameters will be updated.no_grad_set (set, optional) – Set of
Tensor
orTensor.name
that don’t need to be updated. The default value is None.
- Returns
-
tuple (optimize_ops, params_grads), A list of operators appended by minimize and a list of (param, grad) tensor pairs, param is
Parameter
, grad is the gradient value corresponding to the parameter. In static graph mode, the returned tuple can be passed tofetch_list
inExecutor.run()
to indicate program pruning. If so, the program will be pruned byfeed
andfetch_list
before run, see details inExecutor
. - Return type
-
tuple
Examples
>>> import paddle >>> linear = paddle.nn.Linear(10, 10) >>> input = paddle.uniform(shape=[10, 10], min=-0.1, max=0.1) >>> out = linear(input) >>> loss = paddle.mean(out) >>> beta1 = paddle.to_tensor([0.9], dtype="float32") >>> beta2 = paddle.to_tensor([0.99], dtype="float32") >>> adam = paddle.optimizer.Adam(learning_rate=0.1, ... parameters=linear.parameters(), ... weight_decay=0.01) >>> loss.backward() >>> adam.minimize(loss) >>> adam.clear_grad()
-
set_lr
(
value
)
set_lr¶
-
- Api_attr
-
imperative
Set the value of the learning rate manually in the optimizer. If the optimizer use LRScheduler, this API cannot be invoked, because it will lead to conflict.
- Parameters
-
value (float) – the value of learning rate
- Returns
-
None
Examples
>>> import paddle >>> linear = paddle.nn.Linear(10, 10) >>> adam = paddle.optimizer.Adam(0.1, parameters=linear.parameters()) >>> # set learning rate manually by python float value >>> lr_list = [0.2, 0.3, 0.4, 0.5, 0.6] >>> for i in range(5): ... adam.set_lr(lr_list[i]) ... lr = adam.get_lr() ... print("current lr is {}".format(lr)) current lr is 0.2 current lr is 0.3 current lr is 0.4 current lr is 0.5 current lr is 0.6
-
set_lr_scheduler
(
scheduler
)
set_lr_scheduler¶
-
- Api_attr
-
imperative
Set the LRScheduler of the learning rate manually in the optimizer. If the optimizer already used LRScheduler previously, this API will set it be the new one.
- Parameters
-
scheduler (LRScheduler) – the LRScheduler of learning rate
- Returns
-
None
Examples
>>> import paddle >>> linear = paddle.nn.Linear(10, 10) >>> adam = paddle.optimizer.Adam(0.1, parameters=linear.parameters()) >>> # set learning rate manually by class LRScheduler >>> scheduler = paddle.optimizer.lr.MultiStepDecay(learning_rate=0.5, milestones=[2,4,6], gamma=0.8) >>> adam.set_lr_scheduler(scheduler) >>> lr = adam.get_lr() >>> print("current lr is {}".format(lr)) current lr is 0.5 >>> # set learning rate manually by another LRScheduler >>> scheduler = paddle.optimizer.lr.StepDecay(learning_rate=0.1, step_size=5, gamma=0.6) >>> adam.set_lr_scheduler(scheduler) >>> lr = adam.get_lr() >>> print("current lr is {}".format(lr)) current lr is 0.1
-
set_state_dict
(
state_dict
)
set_state_dict¶
-
Load optimizer state dict. For Adam optimizer, contains beta1, beta2, momentum etc. If LRScheduler have been used, global_step will be changed.
- Parameters
-
state_dict (dict) – Dict contains all the Tensor needed by optimizer
- Returns
-
None
Examples
>>> import paddle >>> emb = paddle.nn.Embedding(10, 10) >>> layer_state_dict = emb.state_dict() >>> paddle.save(layer_state_dict, "emb.pdparams") >>> scheduler = paddle.optimizer.lr.NoamDecay( ... d_model=0.01, warmup_steps=100, verbose=True) >>> adam = paddle.optimizer.Adam( ... learning_rate=scheduler, ... parameters=emb.parameters()) >>> opt_state_dict = adam.state_dict() >>> paddle.save(opt_state_dict, "adam.pdopt") >>> opti_state_dict = paddle.load("adam.pdopt") >>> adam.set_state_dict(opti_state_dict)
-
state_dict
(
)
state_dict¶
-
Get state dict information from optimizer. It contain all the tensor used by optimizer. For Adam optimizer, contains beta1, beta2, momentum etc. If LRScheduler have been used, global_step will be include in state dict. If the optimizer never be called(minimize function), the state_dict is empty.
- Parameters
-
None –
- Returns
-
dict contains all the Tensor used by optimizer
- Return type
-
state_dict(dict)
Examples
>>> import paddle >>> emb = paddle.nn.Embedding(10, 10) >>> adam = paddle.optimizer.Adam(0.001, parameters=emb.parameters()) >>> state_dict = adam.state_dict()
-
step
(
)
step¶
-
Execute the optimizer and update parameters once.
- Returns
-
None
Examples
>>> import paddle >>> a = paddle.arange(26, dtype="float32").reshape([2, 13]) >>> linear = paddle.nn.Linear(13, 5) >>> # This can be any optimizer supported by dygraph. >>> adam = paddle.optimizer.Adam(learning_rate = 0.01, ... parameters = linear.parameters()) >>> out = linear(a) >>> out.backward() >>> adam.step() >>> adam.clear_grad()