'How to create a copy of nn.Sequential in torch?
I am trying to create a copy of a nn.Sequential network. For example, the following is the easiest way to do the same-
net = nn.Sequential(
nn.Conv2d(16, 32, 3, stride=2),
nn.ReLU(),
nn.Conv2d(32, 64, 3, stride=2),
nn.ReLU(),
)
net_copy = nn.Sequential(
nn.Conv2d(16, 32, 3, stride=2),
nn.ReLU(),
nn.Conv2d(32, 64, 3, stride=2),
nn.ReLU(),
)
However, it is not so great to define the network again. I tried the following ways but it didn't work-
net_copy = nn.Sequential(net)
: In this approach, it seems thatnet_copy
is just a shared pointer ofnet
net_copy = nn.Sequential(*net.modules())
: In this approach,net_copy
contains many more layers.
Finally, I tired deepcopy
in the following way which worked fine-
net_copy = deepcopy(net)
However, I am wondering if it is the proper way. I assume it is fine because it works.
Solution 1:[1]
Well, I just use torch.load
and torch.save
with io.BytesIO
import io, torch
# write to a buffer
buffer = io.BytesIO()
torch.save(model, buffer) #<--- model is some nn.module
print(buffer.tell()) #<---- no of bytes written
del model
# read from buffer
buffer.seek(0) #<--- must see to origin every time before reading
model = torch.load(buffer)
del buffer
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |