Error: there are no graph nodes that require computing gradients

                # wrap them in Variable
                inputs = Variable(inputs.cuda())
                labels = Variable(labels.cuda())

                # zero the parameter gradients

                # forward
                outputs = model(inputs)

                _, preds = torch.max(outputs, 1)

                preds = preds.float()
                loss = criterion(preds, labels)

I’m trying to solve a classifying problem using code above. But I got the error “RuntimeError: there are no graph nodes that require computing gradients”. How should I solve it? Does it due to the function"torch.max"?Thanks in advance.


The argmax operation is not differentiable, if you check the two outputs of the max operation, you will see that the value has requires_grad=True (this one is differentiable) but the indices has requires_grad=False (this one is not).
You cannot learn this unfortunately, to do classification, you will need a criterion that works directly with the outputs values like nn.CrossEntropyLoss

1 Like