PyTorch: load weights from another model without saving

Viewed 1105

Assume that I have two models in PyTorch, how can I load the weights of model 1 by weights of model 2 without saving the weights?

Like this:

model1.weights = model2.weights

In TensorFlow I can do this:

variables1 = model1.trainable_variables
variables2 = model2.trainable_variables
for v1, v2 in zip(variables1, variables2):
    v1.assign(v2.numpy())
2 Answers

Here's two ways to do that.

# Use load state dict
model_source = Model()
model_dest = Model()
model_dest.load_state_dict(model_source.state_dict())

# Use deep copy
model_source = Model()
model_dest = copy.deepcopy(model_source )
Related