Hi, I am training Fashion-MNIST using a neural network. When loading the dataset, I specify batch size = 64, so “images” is a tensor with size (64, 1, 28, 28). However, after training the network, the image shape turns to be (32, 1, 28, 28). I am a newbie to Pytorch so I do not really understand why this happened? Could you please explain? Thanks in advance!
Below is my code:
import torch
from torchvision import datasets, transforms
import helper
# Define a transform to normalize the data
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])
# Download and load the training data
trainset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
# Download and load the test data
testset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=True)
class Classifier(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(784, 256)
self.fc2 = nn.Linear(256, 128)
self.fc3 = nn.Linear(128, 64)
self.fc4 = nn.Linear(64, 10)
def forward(self, x):
# make sure input tensor is flattened
x = x.view(x.shape[0], -1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = F.log_softmax(self.fc4(x), dim=1)
return x
model = Classifier()
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.parameters(), lr=0.003)
epochs = 5
for e in range(epochs):
running_loss = 0
for images, labels in trainloader:
log_ps = model(images)
loss = criterion(log_ps, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
running_loss += loss.item()
else:
print(f"Training loss: {running_loss/len(trainloader)}")
print(images.shape)
Then I got the final results as follows:
Training loss: 0.3105689758049654
torch.Size([32, 1, 28, 28])
Training loss: 0.300544722255931
torch.Size([32, 1, 28, 28])
Training loss: 0.28751245076690657
torch.Size([32, 1, 28, 28])
Training loss: 0.2766977702217824
torch.Size([32, 1, 28, 28])
Training loss: 0.2701437816992879
torch.Size([32, 1, 28, 28])