'collections.OrderedDict' object has no attribute 'to'

I have trained my model on GPU but I was getting this error while training
AttributeError:‘collections.OrderedDict’ object has no attribute ‘to’ ,
the code comes after this
my_resnet18 =torch.load(’…/resnet18model/best_model.pth’)
my_resnet18.fc =torch.nn.Sequential(nn.Linear(512, 16),nn.Tanh())
my_resnet18 = my_resnet18.to(device)
Is there anything wrong with this?
my pytorch version is 1.0.0

The problem has been solved thanku

1 Like

How did it work out?

can you tell me how to solve it?

Most likely torch.load() returned a state_dict, which would create the issue, if you are trying to call to() on the OrderedDict.
The proper way of restoring the model is to initialize the model and load the state_dict afterwards:

model = models.resnet18()
state_dict = torch.load(...)
model.load_state_dict(state_dict)
model.to(device)

CC @dong-mvp

1 Like