<p>You can do something link this</p>
<pre><code>model.linear1.weight.requires_grad = False
model.linear1.bias.requires_grad = False
</code></pre>
<p>for the pytorch model with linear1 defined as:</p>
<pre><code>self.linear1 = nn.Linear(5, 5)
</code></pre>
<p>as in <a href="https://gist.github.com/L0SG/2f6d81e4ad119c4f798ab81fa8d62d3f" rel="nofollow noreferrer">this</a> code snippet</p>

CC BY-SA 4.0
2022-08-19T21:49:33.563Z
2022-08-19T21:49:33.563Z
Robert
<p>I'm constructing a neural network where the weights of my first hidden layer (connected to the input) are all 1 (identity matrix), but the biases are variable.</p>
<p>Is there a way to &quot;freeze&quot; any updates/training to the weights in a specific layer, but continue to allow the biases in that specific layer to be updated?</p>

CC BY-SA 4.0
2022-08-19T20:08:54.317Z
Maybe this will help: https://discuss.pytorch.org/t/how-the-pytorch-freeze-network-in-some-layers-only-the-rest-of-the-training/7088
GKozinski
<neural-networks><training><pytorch><weights><bias>
Is there a way to freeze training for weights, but not biases in PyTorch?
Nova
# Is there a way to freeze training for weights, but not biases in PyTorch?

(Asked by: username_0 on 2022-08-19)

I'm constructing a neural network where the weights of my first hidden layer (connected to the input) are all 1 (identity matrix), but the biases are variable.

Is there a way to "freeze" any updates/training to the weights in a specific layer, but continue to allow the biases in that specific layer to be updated?

**Tags:** neural-networks, training, pytorch, weights, bias

> **Comments:**
> **username_1**: Maybe this will help: https://discuss.pytorch.org/t/how-the-pytorch-freeze-network-in-some-layers-only-the-rest-of-the-training/7088  

---

## 1 answers

### Answer by username_2 on 2022-08-19. Score: 2 | Accepted answer

You can do something link this
model.linear1.weight.requires_grad = False
model.linear1.bias.requires_grad = False

for the pytorch model with linear1 defined as:
self.linear1 = nn.Linear(5, 5)

as in this code snippet


---

