create a linear model with fixed weights in Pytorch

Viewed 662

I want to create a linear network with a single layer under PyTorch, but I want the weights to be manually initialized and to remain fixed.

For example the values of the weights with the model:

layer = nn.Linear(4, 1, bias=False)
weights = tensor([[ 0.6],
                  [0.25],
                  [ 0.1],
                  [0.05]], dtype=torch.float64)

enter image description here

Is this achievable? If so, how can I do it? Or is there an alternative linear function?

1 Answers

You can freeze your layer by setting the requires_grad to False:

layer.requires_grad_(False)

This way the gradients of the layer's parameters won't get computed.

Or by directly defining so when initializing the parameter:

layer = nn.Linear(4, 1, bias=False)
layer.weight = nn.Parameter(weights, requires_grad=False)

Alternatively, given an input x shaped (n, 4), you can compute the result with a simple matrix multiplication as:

>>> x@weights 
# equivalent to torch.matmul(x, weights)
Related