Can I train only one row of a PyTorch tensor

I was wandering that is it possible to autograd only one row of a model weights so that other rows are fixed?

You can zero the gradients on all other rows, so that you you don’t use those gradients when updating the weights:

weights = Variable(torch.randn(3, 3, 3) , requires_grad=True)[1:].zero_()

Thanks for the quick reply!

I’m still thinking where to insert this line?[1:].zero_()

Should it be inserted after
data, target = Variable(data), Variable(target)
output = model(data)

or after

After you compute the gradients with something.backward() and before you step with the optimizer (optimizer.step()).

1 Like

Thanks Richard, this works as I need!