How to avoid this "UserWarning: positional arguments and argument "destination" are deprecated. nn.Module.state_dict will not accept them in the future."?

Hi, I am trying to save checkpoints during training using the following code:

info_dict = {
                    'epoch' : epoch,
                    'numpy_random_state' : np.random.get_state(),
                    'torch_random_state' : torch.random.get_rng_state(),
                    'net_state' : net.module.state_dict(),
                    'optimizer_state' : optimizer.state_dict(),
                    'train_hist' : history_plot['train'],
                    'val_hist' : history_plot['val'],
                    
                }
torch.save(info_dict, chkpnts)

And loading them, using:

net.load_state_dict(chkpnt['net_state'])
optimizer.load_state_dict(chkpnt['optimizer_state'])

The code is working fine, however, throws up the UserWarning mentioned above. I am new to PyTorch any help is appreciated.

I don’t see the issue in your code, but the warning is raised if positional instead of keyword arguments are used as seen here:

model = nn.Linear(1, 1)

# no warning
state_dict = model.state_dict()

# still no warning if keyword argument is used
d = dict()
model.state_dict(destination=d)

# warning is positional argument is used
d = dict()
model.state_dict(d)
# UserWarning: Positional args are being deprecated, use kwargs instead. Refer to https://pytorch.org/docs/master/generated/torch.nn.Module.html#torch.nn.Module.state_dict for details.
1 Like

Hi @ptrblck,

Thank you for the response. Based on your reply, I checked the error again. I am running 1.12 and in torch.nn.modules.module positional arguments were used: module.state_dict(destination, prefix + name + '.', keep_vars=keep_vars). I believe this was the reason warnings were thrown up.

1 Like