Why is accuracy droping in the last batch?
I am using PyTorch for a classification task. For some reason, the accuracy drops in the last iteration, I would like to know why? any answer is appreciated.
Hers is the code
class Classifier(nn.Module):                          
    def __init__(self):                                    
        super(Classifier, self).__init__()             
        self.layers = nn.Sequential(nn.Linear(89, 128),   
                                    nn.ReLU(),              
                                    nn.Linear(128, 64),      
                                    nn.ReLU(),              
                                    nn.Linear(64, 2))       
    def forward(self, x):               
        return self.layers(x)

def train(train_dl, model, epochs):  
    loss_function = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=0.1)
    for epoch in range(epochs):
        for (features, target) in train_dl:      
            optimizer.zero_grad() 
            features, target = features.to(device), target.to(device)
            output = model(features.float())
            target = target.view(-1) 
            loss = loss_function(output, target)
            loss.backward()  
            optimizer.step()
            output = torch.argmax(output, dim=1)
            correct = (output == target).float().sum()
            accuracy = correct / 512
            print(accuracy, loss)
        break
        
model = Classifier().to(device)
train(train_dl, model, 10)

and the last part of the output
tensor(0.6465, device='cuda:0') tensor(0.6498, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)
tensor(0.6348, device='cuda:0') tensor(0.6574, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)
tensor(0.6582, device='cuda:0') tensor(0.6423, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)
tensor(0.6660, device='cuda:0') tensor(0.6375, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)
tensor(0.6719, device='cuda:0') tensor(0.6338, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)
tensor(0.6426, device='cuda:0') tensor(0.6523, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)
tensor(0.6699, device='cuda:0') tensor(0.6347, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)
tensor(0.6582, device='cuda:0') tensor(0.6422, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)
tensor(0.6543, device='cuda:0') tensor(0.6449, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)
tensor(0.6465, device='cuda:0') tensor(0.6502, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)
tensor(0.6992, device='cuda:0') tensor(0.6147, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)
tensor(0.6777, device='cuda:0') tensor(0.6289, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)
tensor(0.6836, device='cuda:0') tensor(0.6244, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)
tensor(0.6738, device='cuda:0') tensor(0.6315, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)
tensor(0.1387, device='cuda:0') tensor(0.5749, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;)


Probably because your last batch size is less than 512. It would be better to change this line
accuracy = correct / 512

to:
accuracy = correct / features.shape[0]

Alternatively, if you don't want your last batch to have a different size, you can drop it when you create the DataLoader, by setting drop_last=True, something like this:
train_dl = DataLoader(..., drop_last=True)

