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'],
                }, chkpnts)

And loading them, using:


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()

# warning is positional argument is used
d = dict()
# UserWarning: Positional args are being deprecated, use kwargs instead. Refer to 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