RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn (alexnet)

Arnav_Agarwal: <p>Hey everybody,</p>
<p>I’ve just started out and have been trying to debug this but don’t understand what’s wrong.</p>
<p>Would really appreciate the help!</p>
<p>Regards!</p>
<pre><code class="lang-auto">def trained_model(model, criterion, optimizer, epochs):
    for epoch in range(epochs):
        
        print('Epoch:', str(epoch+1) + '/' + str(epochs))
        print('-'*10)
        
        for phase in ['train', 'validation']:
            if phase == 'train':
                model.train() #this trains the model
            else:
                model.eval() #this evaluates the model

            running_loss, running_corrects = 0.0, 0 

            for inputs, labels in dataloaders[phase]:
                inputs = inputs.to(device) #convert inputs to cpu or cuda
                labels = labels.to(device) #convert labels to cpu or cuda

                outputs = model(inputs) #outputs is inputs being fed to the model
                loss = criterion(outputs, labels) #outputs are fed into the model
                # loss.requires_grad = True


                if phase == 'train':
                    optimizer.zero_grad() #sets gradients to zero
                    loss.backward() #computes sum of gradients
                    optimizer.step() #preforms an optimization step

                _, preds = torch.max(outputs, 1) #max elements of outputs with output dimension of one
                running_loss += loss.item() * inputs.size(0) #loss multiplied by the first dimension of inputs
                running_corrects += torch.sum(preds == labels.data) #sum of all the correct predictions

            epoch_loss = running_loss / len(data_images[phase]) #this is the epoch loss
            epoch_accuracy = running_corrects.double() / len(data_images[phase]) #this is the epoch accuracy

            print(phase, ' loss:', epoch_loss, 'epoch_accuracy:', epoch_accuracy)

    return model 
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/1/71a919a351237634235f5648e5ddec4b4e02be73.png" data-download-href="https://discuss.pytorch.org/uploads/default/71a919a351237634235f5648e5ddec4b4e02be73" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/1/71a919a351237634235f5648e5ddec4b4e02be73.png" alt="image" data-base62-sha1="gduftixM2LH7VPcfqAHlYennUFJ" width="690" height="283" data-dominant-color="2F333D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1570×644 39.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>The <code>loss</code> tensor seems to be detached from the model (or rather the <code>output</code>) and you can verify it by checking its <code>.grad_fn</code> which should yield <code>None</code>.<br>
To fix this, check the intermediate tensors in the forward pass (starting with the <code>output</code>) and try to isolate where the graph is detached.<br>
Also, make sure you didn’t globally disable the gradient calculation.</p>