Hello. I’am trying to freeze front layers during training.
Before starting optimization, the optimizer is constructed by
optimizer = torch.optim.SGD(net.parameters(), lr, ...)

Then, during training, i changed the front layers’ requires_grad=False.
Specifically,
for epoch in range(total_epoch):
  if epoch == freeze_epoch:
    net.conv1.weight.requires_grad = False
  train_one_epoch() #update network in an epoch


However, I found that the weights of the front layers are still updated.
I also double-checked that the front layer’s requires_grad=False after changing it.

Why the gradients of front layers are still computed and updated?
How can i implement the same effect in other way (without wrapping each layer as module -> because layers to freeze are not pre-defined ) ??

p.s.
def train_one_epoch():
  for data, target in train_loader:
    ...
    optimizer.zero_grad()
    loss = ...
    loss.backward()
    optimizer.step()

Thank you.