How to assign an arbitrary tensor to model's parameter?

You can manually assign the new parameter to the model’s parameter:

lin = nn.Linear(10, 10, bias=False)
print(lin)
> Linear(in_features=10, out_features=10, bias=False)

x = torch.randn(1, 10)
out = lin(x)
print(out.shape)
> torch.Size([1, 10])

with torch.no_grad():
    lin.weight = nn.Parameter(torch.randn(1, 10)) # out_features, in_features

out = lin(x)
print(out.shape)
> torch.Size([1, 1])

print(lin) # wrong information
> Linear(in_features=10, out_features=10, bias=False)

but note that the attributes won’t be automatically changes as well, so you might want to change them also manually.

4 Likes