How to copy weights from one model to another model instance wise?

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

I don’t really understand your question.
You copied the weights from one model to another but the print statement doesn’t show the newly assigned weights but the old ones?

Not the old weights but initialized weights. I’ve initialized MobileNet model with pointwise convolution layer slightly modified to groups2(Model) using pruning. I Wanted to transfer these weights to another model where pointwise convolutions were initialised with grouping=2(Net).

I copied the weights of Model into Net like this

layer 0

Net.model[0][0].weight.data.copy_(Model.model[0][0].weight.data)
Net.model[0][1].weight.data.copy_(Model.model[0][1].weight.data)
Net.model[0][1].bias.data.copy_(Model.model[0][1].bias.data)

# layer 1-13
for i in range(1,14):
    Net.model[i][0].weight.data.copy_(Model.model[i][0].weight.data)
    Net.model[i][1].weight.data.copy_(Model.model[i][1].weight.data)
    Net.model[i][1].bias.data.copy_(Model.model[i][1].bias.data)
    Net.model[i][3].weight.data.copy_(Prune_group(Model.model[i][3].weight.data, Net.model[i][3].weight.data, groups=2))
    Net.model[i][4].weight.data.copy_(Model.model[i][4].weight.data)
    Net.model[i][4].bias.data.copy_(Model.model[i][4].bias.data)

# layer 14
Net.fc.weight.data.copy_(Model.fc.weight.data)
Net.fc.bias.data.copy_(Model.fc.bias.data)

@albanD Helped me regarding this.

But I thought copying them instance wise would be better as it is more generic and I can apply that to other larger models. The instance wise code which i written, After the execution of the code, the weights of Net are still taking initialised weights.

Regarding the continuation of the post with dictionary was to show for both dictionaries of params1 and params2 is showing same key for both models even though the pointwise convolution is different in one Model. So I cannot apply instance wise idea over there.

So, My question is how to copy weights from one model to another model instance wise. I hope this is understandable. I’m apologize for badly framing the question