Not able to detach and clone the model

I want to clone the model. However, PyTorch fails to detach and clone the model.

temp_enc = model.encoder_net.detach().clone()

Error I get

torch.nn.modules.module.ModuleAttributeError: 'Encoder' object has no attribute 'detach'

When I clone without detaching I receive similar error:

torch.nn.modules.module.ModuleAttributeError: 'Encoder' object has no attribute 'clone'

Hi,

Both clone and detach are constructs on Tensors, not nn.Modules.
If you want to make an independent copy of a model, you can use the regular python deepcopy.

1 Like

Thanks! Stupid mistake. My bad.

Unfortunately, deepcopy throws the following error:

“Only Tensors created explicitly by the user (graph leaves) support the deepcopy protocol at the moment”

UPD: Actually deepcopy on model.state_dict() works fine! Thanks anyway.