Default output even after changing the output features

Muhammad4hmed: <p>I’m trying to make a custom vgg16 and the architecture is as follows:</p>
<pre><code class="lang-auto">class CustomVGG16(torch.nn.Module):
    def __init__(self):
        super(CustomVGG16, self).__init__()
        self.vgg = torchvision.models.vgg16_bn(pretrained = True)
        self.vgg.classifier[-1].out_features = 25
        self.softmax = torch.nn.Softmax()
        

    def forward(self, x):
        x = self.vgg(x)
        x = self.softmax(x)
        return x
</code></pre>
<p>As it can be seen that I have changed the output features in last layer to 25 from 1000. as it is verified by the model architecture:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/d/5db370d445c3e1cf2e59c5786407ae93acdb4647.png" data-download-href="https://discuss.pytorch.org/uploads/default/5db370d445c3e1cf2e59c5786407ae93acdb4647" title="Screenshot_2020-12-03 Google Colaboratory"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/d/5db370d445c3e1cf2e59c5786407ae93acdb4647.png" alt="Screenshot_2020-12-03 Google Colaboratory" data-base62-sha1="dmUQZHJgJLIA4XFSBH7uJsYZx0H" width="690" height="239" data-dominant-color="3E3E3E"><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">Screenshot_2020-12-03 Google Colaboratory</span><span class="informations">718×249 6.26 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>
<p>So the model is supposed to give 25 outputs ( n x 25 ) right?<br>
But on the following, <code>outputs = model(inputs)</code><br>
the shape of outputs is (6, 1000). 6 because of batch size.<br>
but as mentioned earlier, I changed this 1000 to 25, so this should be (6, 25).<br>
what could be the reason?</p>
<p>my training loop:</p>
<pre><code class="lang-auto">    running_loss = 0.0
    accuracy = []
    val_accuracy = []

    model = model.to(device)

    scheduler = torch.optim.lr_scheduler.StepLR(optimizer, 20, gamma = 0.1)

    for ep in range(epoch):
        correct = 0
        total = 0
        start_time = time.time()
        for i, data in enumerate(train_loader):
            inputs, labels = data

            inputs, labels = inputs.to(device), labels.to(device)

            optimizer.zero_grad()

            outputs = model(inputs)
            print(inputs.shape)
            
            loss = criterion(labels, outputs)
            loss.backward()
            optimizer.step()

            running_loss += loss.item()
            correct += (outputs.argmax(1) == labels).float().sum()
            total += len(labels)
        accuracy_local = (correct / total)*100
        accuracy_local = accuracy_local.data.cpu().numpy()

        accuracy.append(accuracy_local)
        val_acc = valid(model,criterion,optimizer,val_loader)
        val_accuracy.append(val_acc)
        scheduler.step()

        print('EPOCH: {:} Accuracy: {:.2f}% Val_Accuracy: {:.2f}% Time: {:.2f} seconds'.format(ep, accuracy_local, val_acc, time.time() - start_time))

</code></pre>
<p>criterion is bcewithlogitsloss</p>

ptrblck: <aside class="quote no-group" data-username="Muhammad4hmed" data-post="1" data-topic="105003">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/muhammad4hmed/48/26387_2.png" class="avatar"> Muhammad4hmed:</div>
<blockquote>
<p>So the model is supposed to give 25 outputs ( n x 25 ) right?</p>
</blockquote>
</aside>
<p>Unfortunately that’s not the case, since changing the <code>out_features</code> alone won’t reinitialize the internal parameters. You would have to reassign a new <code>nn.Linear</code> layer to <code>self.vgg.classifier[-1]</code> instead.</p>