Deepcopy of nn.module changes type of its parameters to tensors?

I noticed deepcopying a module causes its parameters() to be tensors rather than nn.Parameters.

import torch.nn
import copy

l = torch.nn.Linear(3,1)
c = copy.deepcopy(l)
print([type(p) for p in l.parameters()])
print([type(p) for p in c.parameters()])
[<class 'torch.nn.parameter.Parameter'>, <class 'torch.nn.parameter.Parameter'>]
[<class 'torch.Tensor'>, <class 'torch.Tensor'>]

Why does this happen and can this cause any problems when working with the copy of the module later on?

Thanks for reporting it!
It might be this old bug again.

@hdkgr what PyTorch version are you on? print(torch.__version__)

I could reproduce this issue in 1.0.0.dev20181007 and 1.0.0a0+dfad8b6.

I don’t have the system at hand, but I’m quite sure it’s the 0.4.1 stable build.

This looks like a regression in 0.4.0 / 0.4.1, We reopened the issue and an engineer is working on issuing a fix.

1 Like