Error: RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

My model code:

class Classifier(nn.Module):
    def __init__(self):
        super(Classifier, self).__init__()
        self.resnet34 = models.resnet34(pretrained=True)
        self.fc = nn.Sequential(
            nn.Linear(2000, 1024),
            nn.Linear(1024, 1),
    def forward(self, x1, x2):
        x1 = self.resnet34(x1)
        x2 = self.resnet34(x2)
        output =,x2),1)
        output = self.fc(output)
        return output

Please help me solving this issue.

Your model itself is working fine as seen here:

model = Classifier()
x1 = torch.randn(1, 3, 224, 224)
x2 = torch.randn(1, 3, 224, 224)
out = model(x1, x2)

Could you post a minimal, executable code snippet which would reproduce the issue or check what the difference between my working example and your code could be?

Thanks for the reply @ptrblck i was taking torch.max(,1) before passing the output to calculate the loss. Now it is resolved.