I'm learning from Udacity using this video.
I saw this piece of code:
model = nn.Sequential(nn.Linear(784, 128),
nn.ReLU(),
nn.Linear(128, 64),
nn.ReLU(),
# nn.Linear(64, 32),
# nn.ReLU(),
nn.Linear(64, 10),
nn.LogSoftmax(dim=1))
criterion = nn.NLLLoss()
optimizer = optim.SGD(model.parameters(), lr=0.003)
epochs = 5
for e in range(epochs):
running_loss = 0
for images, labels in trainloader:
# Flatten MNIST images into a 784 long vector
images = images.view(images.shape[0], -1)
# TODO: Training pass
optimizer.zero_grad()
output = model(images)
loss = criterion(output, labels)
loss.backward()
optimizer.step()
if e == 1 and running_loss == 0:
print(loss)
running_loss += loss.item()
else:
print(f"Training loss: {running_loss/len(trainloader)}")
Then, I was playing around with layers and neurons.
I have tried:
- Adding 1 more layer (64, 32) between (128, 64) and (64, 10), and changing the (64, 10) to (32, 10), increasing the epochs.
- Replacing the (128, 64) with (128, 32), increasing the epochs
- Adding 1 layer (784, 256), (256, 128), increasing the epochs
Etc.
From what I saw here, they can all achieve good accuracy with enough epochs of training.
Then, my question is how can I find the best number of layers and neurons in each layer (architecture) which can:
- Achieve the highest accuracy
- Still be the simplest model (least number of parameters)