This code creates a simple model with two layers. It freezes the first layer so it does not update during training. The optimizer only updates the second layer. After one training step, we print gradients to see which layers changed.
import torch
import torch.nn as nn
import torch.optim as optim
# Simple model with two linear layers
class SimpleModel(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(5, 3)
self.fc2 = nn.Linear(3, 1)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
model = SimpleModel()
# Freeze first layer
for param in model.fc1.parameters():
param.requires_grad = False
# Optimizer only updates parameters with requires_grad=True
optimizer = optim.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=0.1)
# Dummy data
inputs = torch.randn(4, 5)
targets = torch.randn(4, 1)
criterion = nn.MSELoss()
# Training step
model.train()
outputs = model(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
# Check which parameters were updated
fc1_grad = model.fc1.weight.grad
fc2_grad = model.fc2.weight.grad
print("fc1 weight grad:", fc1_grad)
print("fc2 weight grad:", fc2_grad)