pytorch系列文档之How to use an optimizer(用optimizer更新模型参数)

1·简介

torch.optim是实现各种优化算法的包。已经支持最常用的方法,并且接口足够通用,因此将来也可以轻松集成更复杂的方法。

要使用torch.optim,必须构造一个优化器对象Optimizer,该对象将保持当前状态并根据计算出的梯度来更新参数。

要构造一个Optimizer,必须为其提供一个包含参数的可迭代项(所有参数都应为Variables)以进行优化。然后,可以指定优化器特定的选项,例如学习率,权重衰减等。

注意:如果要用.cuda()将model移动到gpu上,必须在构建optimizers之前进行移动,这因为移动后与移动前是不一样的对象。
一定要注意,在构造和使用optimizer时,应确保优化的参数位于同一位置。

例如:

optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
optimizer = optim.Adam([var1, var2], lr=0.0001)

2·Per-parameter options

model的不同部分可以设定不一样的参数。

optim.SGD([
                {'params': model.base.parameters()},
                {'params': model.classifier.parameters(), 'lr': 1e-3}
            ], lr=1e-2, momentum=0.9)

这样,model.base中的参数使用默认学习率1e-2,model.classifier中的参数使用单独设定的学习率1e-3,所有参数的momentum设置为0.9。

3·Taking an optimization step

使用optimizer.step()更新参数
这个函数在梯度被计算了之后就可以调用了,也就是执行了loss.backward()之后
示例:

for input, target in dataset:
    optimizer.zero_grad()
    output = model(input)
    loss = loss_fn(output, target)
    loss.backward()
    optimizer.step()

一些优化算法(例如共轭梯度和LBFGS)需要多次重新评估函数,因此您必须传递闭包以允许它们重新计算模型。闭包应清除梯度,计算损失,然后将其返回。

示例:

for input, target in dataset:
    def closure():
        optimizer.zero_grad()
        output = model(input)
        loss = loss_fn(output, target)
        loss.backward()
        return loss
    optimizer.step(closure)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To use utils.save_checkpoint, you first need to import the necessary libraries in your Python script. Then, you can create a function to save a checkpoint of your model during training or after training is complete. The function would involve specifying the file path and name of the checkpoint, as well as the model and any other important information you want to include in the checkpoint. Here is an example of how to use utils.save_checkpoint in PyTorch: ```python import torch import os def save_checkpoint(state, checkpoint_dir, filename='checkpoint.pth.tar'): if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) filepath = os.path.join(checkpoint_dir, filename) torch.save(state, filepath) print('Checkpoint saved to {}'.format(filepath)) # Call the function to save a checkpoint checkpoint = { 'epoch': 10, 'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict(), 'loss': loss } save_checkpoint(checkpoint, 'checkpoints') ``` In this example, the save_checkpoint function takes in a dictionary called "state" which contains the epoch, model state_dict, optimizer state_dict, and loss. It also takes in the directory where you want to save the checkpoint, and the filename you want to give to the checkpoint file. When you call the function, you pass in the dictionary containing the relevant information and the directory where you want to save the checkpoint file. The function then creates the directory if it doesn't exist, combines the directory and filename to create the full file path, and saves the checkpoint using torch.save. You can then load this checkpoint later using the utils.load_checkpoint function, which can be useful for resuming training or making predictions.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值