Load a single layer weights from a pretrained model
I want to specifically add the pretrained model parameters of some layers to my new network .
For Linear Layer i just did :

model_enc.linear_3d.weight = model_trained.linear_3d.weight
model_enc.linear_3d.bias = model_trained.linear_3d.bias


Will this suffice or are there any other parameters that I need to load or is there a easy way than this . My module is already trained and I just want to load params for few layers .

Thank you

Your solution should work and seems easy enough to me.

From the source code on https://pytorch.org/docs/master/_modules/torch/nn/modules/linear.html#Linear you can see that the nn.Linear module has  the attributes in_features, out_features, weight1 and bias:

def __init__(self, in_features, out_features, bias=True):
    super(Linear, self).__init__()
    self.in_features = in_features
    self.out_features = out_features
    self.weight = Parameter(torch.Tensor(out_features, in_features))
    if bias:
        self.bias = Parameter(torch.Tensor(out_features))
    else:
        self.register_parameter('bias', None)
    self.reset_parameters()


Therefore, as long as your in_features and out_features are identical you can just replace the weights and bias as you did.

Alternatively, you can replace the entire Linear module in in one network with the module of the other if you stored it as an attribute.
