How to copy parameters from one network to another

Hello,

I have two networks (inherited from nn.Block) that have the same structure. What’s the most efficient way to copy parameters from net1 to their counterparts in net2?

Thanks,
Haining

@hyu

The simplest way to do that is to do:

net1.save_parameters('net.params') 
net2.load_parameters('net.params') 

Thank you! I will test it out.

My use case requires me to copy parameters from one network to another regularly (e.g., every N samples). I am a bit afraid that writing to disk (and reading from it) at that frequency will delay the training program. Will try out and let you know

Hi @hyu ,
Iff you are using Gluon (which you seem to be using with nn.Block), you may also try that:

params1 = net1.collect_params()
params2 = net2.collect_params()
for p1, p2 in zip(params1.values(), params2.values()):
    p2.set_data(p1.data())

Intuitively I would say that it will be faster since there is no disk I/O, but if you care about performance, you should definitely benchmark the two methods. :wink:

Thanks. Will try this out too and share findings.

1 Like