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

username: gss
cooked: <p>I am training a VGG16 with images of size 224x224 with 4 classes.</p>
<pre><code class="lang-auto">device = torch.device("cuda" if torch.cuda.is_available() 
                                  else "cpu")
print(device)
model = models.vgg16(pretrained=True)

for param in model.parameters():
    param.requires_grad = False
    
model.fc = nn.Sequential(nn.Linear(2048, 512),
                                 nn.ReLU(),
                                 nn.Linear(512, 4),
                                 nn.LogSoftmax(dim=1))
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.fc.parameters(), lr=0.0001)
model.to(device)
</code></pre>
<p>And I am training it for 50 epochs:</p>
<pre><code class="lang-auto">epochs = 50
steps = 0
running_loss = 0
print_every = 105
train_losses, test_losses, accuracy = [], [], []

for epoch in range(epochs):
    for inputs, labels in trainloader:
        #print('working')
        steps += 1
        inputs, labels = inputs.to(device), labels.to(device)
        optimizer.zero_grad()
        logps = model.forward(inputs)
        loss = criterion(logps, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()
        
        if steps % print_every == 0:
            test_loss = 0
            accuracy = 0
            model.eval()
            with torch.no_grad():
                for inputs, labels in testloader:
                    #print('validating')
                    inputs, labels = inputs.to(device), labels.to(device)
                    logps = model.forward(inputs)
                    batch_loss = criterion(logps, labels)
                    test_loss += batch_loss.item()
                    
                    ps = torch.exp(logps)
                    top_p, top_class = ps.topk(1, dim=1)
                    equals = top_class == labels.view(*top_class.shape)
                    accuracy += torch.mean(equals.type(torch.FloatTensor)).item()

            train_losses.append(running_loss/len(trainloader))
            test_losses.append(test_loss/len(testloader))                    
            print(f"Epoch {epoch+1}/{epochs}.. "
                  f"Train loss: {running_loss/print_every:.3f}.. "
                  f"Test loss: {test_loss/len(testloader):.3f}.. "
                  f"Test accuracy: {accuracy/len(testloader):.3f}")
            running_loss = 0
            model.train()
</code></pre>
<p>However, this error message pops up:<br>
<code>RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn</code></p>
<p>What am I doing wrong? Thanks!</p>

username: ptrblck
cooked: <p><code>vgg16</code> does not have a <code>model.fc</code> attribute, but uses <code>model.classifier</code> instead.<br>
Because of that you are currently freezing the complete model and assign the <code>nn.Sequential</code> container to a new and unused attribute called <code>fc</code>.</p>

username: gss
cooked: <p>So should it be like this instead?</p>
<pre><code class="lang-auto">for param in model.parameters():
    param.requires_grad = False
    
model.classifier = nn.Sequential(nn.Linear(2048, 512),
                                 nn.ReLU(),
                                 #nn.Dropout(0.2),
                                 nn.Linear(512, 4),
                                 nn.LogSoftmax(dim=1))
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.classifier.parameters(), lr=0.0001)
model.to(device)
</code></pre>

username: ptrblck
cooked: <p>The number of input features for <code>vgg16</code> is <code>25088</code>, but besides that the code looks alright.</p>
<p>EDIT:<br>
Sorry, missed an issue.<br>
Since you are using <code>nn.LogSoftmax</code> as the last activation function, you should use <code>nn.NLLLoss</code> as your criterion.</p>
<p>Alternatively you could remove the <code>nn.LogSoftmax</code> and use <code>nn.CrossEntropyLoss</code>, which will internally use <code>F.log_softmax</code> and <code>nn.NLLLoss</code>.</p>

username: gss
cooked: <p>Does this make any sense at all given the size of my images 224x224 and 4 number of classes?</p>
<pre><code class="lang-auto">for param in model.parameters():
    param.requires_grad = True
    
model.classifier = nn.Sequential(nn.Linear(25088, 64),
                                 nn.ReLU(),
                                 #nn.Dropout(0.2),
                                 nn.Linear(64, 4),
                                 nn.LogSoftmax(dim=1))
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.classifier.parameters(), lr=0.0001)
model.to(device)
</code></pre>
<p>I am trying to replicate last layer of this ResNet50:</p>
<pre><code class="lang-auto">for param in model.parameters():
    param.requires_grad = False
    
model.fc = nn.Sequential(nn.Linear(2048, 512),
                                 nn.ReLU(),
                                 nn.Linear(512, 4),
                                 nn.LogSoftmax(dim=1))
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.fc.parameters(), lr=0.0001)
model.to(device)
</code></pre>
<p>I am kind of lost here…</p>

username: ptrblck
cooked: <p>The number of input features of the first linear layer in a “standard” CNN is defined by the flattened activation from the feature extractor (conv-pool layers).<br>
Each image contains <code>3*224*224=150528</code> pixels at the beginning. The conv and pooling layers then change the activation shape (spatial and channel-wise) so that the output will have the shape <code>[batch_size, 512, 7, 7]</code>. After flattening this activation, you’ll end up with a tensor of <code>[batch_size, 25088]</code>.</p>
<p>Let me know, if you need more information or if I misunderstood your question.</p>