Python 如何加载学习率计划程序状态dict?

Python 如何加载学习率计划程序状态dict?,python,pytorch,Python,Pytorch,我有一个模型和一个学习率调度器。我使用所示的statedict方法保存模型和优化器 代码在没有加载调度器状态dict的情况下运行良好,因此我不确定我做错了什么。我正试图加载上面提到的state dict,但出现以下错误: TypeError Traceback (most recent call last) <ipython-input-7-e3217d6dd870> in <module> 42

我有一个模型和一个学习率调度器。我使用所示的statedict方法保存模型和优化器

代码在没有加载调度器状态dict的情况下运行良好,因此我不确定我做错了什么。我正试图加载上面提到的state dict,但出现以下错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-7-e3217d6dd870> in <module>
     42 nx.load_state_dict(checkpoint['net_x_state_dict'])
     43 optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
---> 44 scheduler.load_state_dict(checkpoint['scheduler'])

~/anaconda3/lib/python3.7/site-packages/torch/optim/lr_scheduler.py in load_state_dict(self, state_dict)
     92                 from a call to :meth:`state_dict`.
     93         """
---> 94         self.__dict__.update(state_dict)
     95 
     96     def get_last_lr(self):

TypeError: 'CyclicLR' object is not iterable
TypeError回溯(最近一次调用)
在里面
42 nx.加载状态记录(检查点['net\u x\u state\u dict'])
43 optimizer.load_state_dict(检查点['optimizer_state_dict'))
--->44调度程序。加载状态命令(检查点['scheduler'])
~/anaconda3/lib/python3.7/site-packages/torch/optim/lr\u scheduler.py处于加载状态(self,state\u dict)
92从呼叫:meth:‘state_dict’。
93         """
--->94自我记录更新(状态记录)
95
96 def获取最后一个lr(自我):
TypeError:“CyclicLR”对象不可编辑

因为在保存之前,我们必须从
调度程序中提取
state\u dict()
值,即在
torch.save()方法中
下面的代码将起作用

import torch
import torch.nn as nn
import torch.optim as optim

class net_x(nn.Module): 
        def __init__(self):
            super(net_x, self).__init__()
            self.fc1=nn.Linear(2, 20) 
            self.fc2=nn.Linear(20, 20)
            self.out=nn.Linear(20, 4) 

        def forward(self, x):
            x=self.fc1(x)
            x=self.fc2(x)
            x=self.out(x)
            return x

nx = net_x()


r = torch.tensor([1.0,2.0])
optimizer = optim.Adam(nx.parameters(), lr = 0.1)
scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer, base_lr=1e-2, max_lr=0.1, step_size_up=1, mode="triangular2", cycle_momentum=False)

path = 'opt.pt'
for epoch in range(10):
    optimizer.zero_grad()
    net_predictions = nx(r)
    loss = torch.sum(torch.randint(0,10,(4,)) - net_predictions)
    loss.backward()
    optimizer.step()
    scheduler.step()
    print('loss:' , loss)
    torch.save({    'epoch': epoch,
                    'net_x_state_dict': nx.state_dict(),
                    'optimizer_state_dict': optimizer.state_dict(),
                    'scheduler': scheduler.state_dict(),    # HERE IS THE CHANGE
                    }, path)

PATH = control_path
checkpoint = torch.load(path)        
nx.load_state_dict(checkpoint['net_x_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
scheduler.load_state_dict(checkpoint['scheduler'])
import torch
import torch.nn as nn
import torch.optim as optim

class net_x(nn.Module): 
        def __init__(self):
            super(net_x, self).__init__()
            self.fc1=nn.Linear(2, 20) 
            self.fc2=nn.Linear(20, 20)
            self.out=nn.Linear(20, 4) 

        def forward(self, x):
            x=self.fc1(x)
            x=self.fc2(x)
            x=self.out(x)
            return x

nx = net_x()


r = torch.tensor([1.0,2.0])
optimizer = optim.Adam(nx.parameters(), lr = 0.1)
scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer, base_lr=1e-2, max_lr=0.1, step_size_up=1, mode="triangular2", cycle_momentum=False)

path = 'opt.pt'
for epoch in range(10):
    optimizer.zero_grad()
    net_predictions = nx(r)
    loss = torch.sum(torch.randint(0,10,(4,)) - net_predictions)
    loss.backward()
    optimizer.step()
    scheduler.step()
    print('loss:' , loss)
    torch.save({    'epoch': epoch,
                    'net_x_state_dict': nx.state_dict(),
                    'optimizer_state_dict': optimizer.state_dict(),
                    'scheduler': scheduler.state_dict(),    # HERE IS THE CHANGE
                    }, path)

PATH = control_path
checkpoint = torch.load(path)        
nx.load_state_dict(checkpoint['net_x_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
scheduler.load_state_dict(checkpoint['scheduler'])