leonardltk1
leonardltk1

Reputation: 377

torch model.load_state_dict *** AttributeError: 'ModelName' object has no attribute 'copy'

i previously saved a model like this:

trainedmodelpath = "model.th"
torch.save({'model': model,
            'scaler': scaler,
            'encoder': label_encoder,
            'config': config_parameters},
            trainedmodelpath)

But when i try to load it like this:

PreviousModelPath = "model.th"
TorchLoadedState = torch.load(PreviousModelPath)
TorchLoadedState_Model = TorchLoadedState['model']
model.load_state_dict(TorchLoadedState_Model)

i received this error

*** AttributeError: 'ModelName' object has no attribute 'copy'

Upvotes: 1

Views: 1377

Answers (1)

joe32140
joe32140

Reputation: 1392

Save model weigths with model.state_dict() instead:

torch.save({'model': model.state_dict(),
            'scaler': scaler,
            'encoder': label_encoder,
            'config': config_parameters},
            trainedmodelpath)

Upvotes: 2

Related Questions