How to get gradient of loss with respect to predicted output

I’m trying to get the gradient of the final output of a nn with respect to the loss function like so:

x = torch.FloatTensor(...)
x = Variable(x, requires_grad=True)
y = torch.FloatTensor(...)
y = Variable(y)

model = torch.nn.Sequential(
    torch.nn.Linear(D_in, H, False),
    torch.nn.Linear(H, D_out, False),

loss_fn = torch.nn.L1Loss(size_average=False)
y_pred = model(x)
loss = loss_fn(y_pred, y)

But the gradient comes back ‘None’. What am I doing wrong? For reference, I can get the linear layer’s weights just fine with a similar approach. Also, y_pred (created by model(x)) does have ‘requires_grad=True’

Gradients don’t accumulate in non-leaf nodes. You should register a backward hook on the model: to view the gradient of the loss wrt y_pred


Did you figure out how to use it for finding the gradient respect to prediction?

@richard Can you provide an example to how to do this?

1 Like