比较与torch.optim.SparseAdam的差异
torch.optim.SparseAdam
class torch.optim.SparseAdam(
params,
lr=0.001,
betas=(0.9, 0.999),
eps=1e-08
)
更多内容详见torch.optim.SparseAdam。
mindspore.nn.LazyAdam
class mindspore.nn.LazyAdam(
params,
learning_rate=1e-3,
beta1=0.9,
beta2=0.999,
eps=1e-8,
use_locking=False,
use_nesterov=False,
weight_decay=0.0,
loss_scale=1.0
)
更多内容详见mindspore.nn.LazyAdam。
差异对比
torch.optim.SparseAdam
为PyTorch中专门用于稀疏场景的Adam算法;
mindspore.nn.LazyAdam
既可以用于常规场景,也可以用于稀疏场景:
当输入梯度为稀疏Tensor时,默认参数下
mindspore.nn.LazyAdam
与torch.optim.SparseAdam
一致,但mindspore.nn.LazyAdam
当前仅支持CPU后端;当输入梯度为非稀疏时,
mindspore.nn.LazyAdam
自动执行mindspore.nn.Adam
算法,且支持CPU/GPU/Ascend后端。
分类 |
子类 |
PyTorch |
MindSpore |
差异 |
---|---|---|---|---|
参数 |
参数1 |
params |
params |
功能一致 |
参数2 |
lr |
learning_rate |
功能一致,参数名不同 |
|
参数3 |
betas |
beta1, beta2 |
功能一致,参数名不同 |
|
参数4 |
eps |
eps |
功能一致 |
|
参数5 |
- |
use_locking |
MindSpore的 |
|
参数6 |
- |
use_nesterov |
MindSpore的 |
|
参数7 |
- |
weight_decay |
PyTorch无此参数 |
|
参数8 |
- |
loss_scale |
MindSpore的 |
代码示例
# MindSpore.
import mindspore
from mindspore import nn
net = nn.Dense(2, 3)
optimizer = nn.LazyAdam(net.trainable_params())
criterion = nn.MAELoss(reduction="mean")
def forward_fn(data, label):
logits = net(data)
loss = criterion(logits, label)
return loss, logits
grad_fn = mindspore.value_and_grad(forward_fn, None, optimizer.parameters, has_aux=True)
def train_step(data, label):
(loss, _), grads = grad_fn(data, label)
optimizer(grads)
return loss
# PyTorch
import torch
model = torch.nn.Linear(2, 3)
criterion = torch.nn.L1Loss(reduction='mean')
optimizer = torch.optim.SparseAdam(model.parameters())
def train_step(data, label):
optimizer.zero_grad()
output = model(data)
loss = criterion(output, label)
loss.backward()
optimizer.step()