Change of batch size during the MNIST evaluation

YoonhoRoh: <p>Hello, I’m trying to solve MNIST tutorial.<br>
Here is my code</p>
<pre><code class="lang-auto">import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import matplotlib.pyplot as plt
from torchvision import datasets, transforms
dev = 'cuda'
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
no_cuda = False
use_cuda = not no_cuda and torch.cuda.is_available()
device = torch.device ('cuda' if use_cuda else 'cpu')
seed = 1
batch_size = 64
test_batch_size = 64
torch.manual_seed(seed)
train_loader = torch.utils.data.DataLoader (datasets.MNIST('dataset/', train=True, download=True,
                                                           transform= transforms.Compose([transforms.ToTensor(),
                                                            transforms.Normalize ((0.1307,), (0.3081,))])),
                                                            batch_size = batch_size, shuffle=True)

test_loader = torch.utils.data.DataLoader(datasets.MNIST('dataset/', train=False, transform=transforms.Compose
([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])), batch_size = test_batch_size, shuffle=True)

image, label = next(iter(train_loader))
class Net (nn.Module):
    def __init__ (self): 
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5, 1)
        self.conv2 = nn.Conv2d(20, 50, 5, 1)
        self.fc1 = nn.Linear (4*4*50, 500)
        self.fc2 = nn.Linear(500, 10)

    def forward(self, x):
        #feature extraction
        x = F.relu (self.conv1(x))
        x = F.max_pool2d(x, 2, 2)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2, 2)
        # print(x.shape)
        # model = Net()
        # model.forward(image)  
        # Fully connected (Classification)
        x = x.view(-1, 4*4*50)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)

model = Net().to(device)
optimizer = optim.SGD(model.parameters(), lr = 0.001, momentum = 0.5)


epochs = 1
log_interval = 100
for epoch in range (1, 2):
    model.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        data, target = data.to(device), target.to(device)
        optimizer.zero_grad() #optimizer clear
        output = model(data)
        loss = F.nll_loss(output, target)
        loss.backward()
        optimizer.step() # update

        if batch_idx % log_interval == 0: 
            print('Train Epoch: {} [{}/{} ({:.0f}%)] \t Loss: {:.6f}'.format (epoch, batch_idx * len(data), len(train_loader.dataset), 100*batch_idx / len (train_loader), loss.item()))

model.eval ()

test_loss = 0
correct = 0

with torch.no_grad(): 
    for data, target in test_loader:
        data, target = data.to(device), target.to(device)
        output = model(data) # size [64, 10]
        test_loss += F.nll_loss(output, target, reduction ='sum').item()
        pred = output.argmax(dim = 1, keepdim = True) # size [64, 1]
        correct += pred.eq(target.view_as(pred)).sum().item() 

test_loss = test_loss / len(test_loader.dataset)
print(len(data), target.shape, data.shape, output.shape)
print ('\nTest set: Average Loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n' .format(test_loss, correct, len(test_loader.dataset), 100 * correct / len(test_loader.dataset)))
</code></pre>
<p>Even though I designated test_batch_size as 64, batch size of evaluation part is 16.<br>
I’m wondering why the batch size had changed.</p>
<p>I am looking forward to see any help. Thanks in advance.<br>
Kind regards,<br>
Yoon Ho</p>

bsridatta: <p>You have train as <code>    for batch_idx, (data, target) in enumerate(train_loader):</code><br>
and test as <code>for data, target in test_loader:</code>. Fix that and it should work?</p>
<p>Also shuffling test is not a good idea, you want to keep it same for comparision across runs</p>

ptrblck: <p>The test data of <code>MNIST</code> will contain 10000 samples.<br>
If you are using a batch size of 64, you would get 156 full batches (9984 samples) and a last batch of 16 samples (9984+16=10000), so I guess you are only checking the shape of the last batch.<br>
If you don’t want to use this last (smaller) batch, you can use <code>drop_last=True</code> in the <code>DataLoader</code>.</p>

YoonhoRoh: <p>Thanks!! it really helped me a lot</p>