Skip to content

torch.save does not work with _LRSchedulers #7255

@PetrochukM

Description

@PetrochukM

Issue description

torch.save does not work with torch.optim.lr_scheduler._LRScheduler

Code example

Offending code:

from torch import nn
from torch.optim import Adam
from torch.optim.lr_scheduler import MultiStepLR

import torch

net = nn.LSTM(10, 10)
optimizer = Adam(params=filter(lambda p: p.requires_grad, net.parameters()))
scheduler = MultiStepLR(optimizer, milestones=[30, 80], gamma=0.1)

scheduler.step()  # This is Fine

# Check save and load into the directory work
torch.save(scheduler, 'scheduler.pt')
scheduler = torch.load('scheduler.pt')

scheduler.step()  # AttributeError: 'MultiStepLR' object has no attribute 'optimizer'

Stack trace:

Traceback (most recent call last):
  File "play.py", line 17, in <module>
    scheduler.step()  # AttributeError: 'MultiStepLR' object has no attribute 'optimizer'
  File "/usr/local/lib/python3.6/dist-packages/torch/optim/lr_scheduler.py", line 55, in step
    for param_group, lr in zip(self.optimizer.param_groups, self.get_lr()):
AttributeError: 'MultiStepLR' object has no attribute 'optimizer'

System Info

  • PyTorch or Caffe2: PyTorch
  • How you installed PyTorch (conda, pip, source): pip
  • PyTorch version: 0.4.0
  • Python version: 3.6.4

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions