Is it possible to copy weights from one model to another model instance wise?
for m, n in zip(Model.modules(), Net.modules()):
if isinstance(m, nn.Conv2d):
# copy the convolutional layer Net model weights and bias
n.weight.data = m.weight.data
#n.bias.data = m.bias.data
elif isinstance(m, nn.BatchNorm2d):
# copy the BatchNorm layer Net model weights and bias
n.weight.data = m.weight.data
n.bias.data = m.bias.data
elif isinstance(m, nn.Linear):
# copy the Linear layer
n.weight_data = m.weight.data
n.bias.data = m.bias.data
#m.weight.data = n.weight.data
I tried as mentioned in this post
params1 = Model.named_parameters()
params2 = Net.named_parameters()
dict_params2 = dict(params2)
for name1, param1 in params1:
if name1 in dict_params2:
print(name1)
The program is not returning the modifying
model.0.0.weight
model.0.1.weight
model.0.1.bias
model.1.0.weight
model.1.1.weight
model.1.1.bias
model.1.3.weight
model.1.4.weight
model.1.4.bias
model.2.0.weight
model.2.1.weight
model.2.1.bias
model.2.3.weight
model.2.4.weight
model.2.4.bias
model.3.0.weight
model.3.1.weight
model.3.1.bias
model.3.3.weight
model.3.4.weight
model.3.4.bias
model.4.0.weight
model.4.1.weight
model.4.1.bias
model.4.3.weight
model.4.4.weight
model.4.4.bias
model.5.0.weight
model.5.1.weight
model.5.1.bias
model.5.3.weight
model.5.4.weight
model.5.4.bias
model.6.0.weight
model.6.1.weight
model.6.1.bias
model.6.3.weight
model.6.4.weight
model.6.4.bias
model.7.0.weight
model.7.1.weight
model.7.1.bias
model.7.3.weight
model.7.4.weight
model.7.4.bias
model.8.0.weight
model.8.1.weight
model.8.1.bias
model.8.3.weight
model.8.4.weight
model.8.4.bias
model.9.0.weight
model.9.1.weight
model.9.1.bias
model.9.3.weight
model.9.4.weight
model.9.4.bias
model.10.0.weight
model.10.1.weight
model.10.1.bias
model.10.3.weight
model.10.4.weight
model.10.4.bias
model.11.0.weight
model.11.1.weight
model.11.1.bias
model.11.3.weight
model.11.4.weight
model.11.4.bias
model.12.0.weight
model.12.1.weight
model.12.1.bias
model.12.3.weight
model.12.4.weight
model.12.4.bias
model.13.0.weight
model.13.1.weight
model.13.1.bias
model.13.3.weight
model.13.4.weight
model.13.4.bias
fc.weight
fc.bias