I am trying to make a common train model for both MNIST and CIFAR10, the optimizer is SGD.
However, I just find the below setup only works for CIFAR10:
optimizer(self.model.parameters(), lr=cmd_lr, momentum=0.9, weight_decay=1e-4)
if using MNIST, looks like I can not set the momentum (remove it will be OK), otherwise, I will get errors like:
why does that happen? I can not figure out why the SGD for MNIST does not support momentum setup?
TypeError: __init__() got an unexpected keyword argument 'momentum'
The MNIST model is as below, which is a quite common model to be used.
class Net(nn.Module): def __init__(self) -> None: super(Net, self).__init__() self.conv1 = nn.Conv2d(1, 32, 3, 1) self.conv2 = nn.Conv2d(32, 64, 3, 1) self.dropout1 = nn.Dropout(0.25) self.dropout2 = nn.Dropout(0.5) self.fc1 = nn.Linear(9216, 128) self.fc2 = nn.Linear(128, 10) def forward(self, x) -> torch.Tensor: x = self.conv1(x) x = F.relu(x) x = self.conv2(x) x = F.relu(x) x = F.max_pool2d(x, 2) x = self.dropout1(x) x = torch.flatten(x, 1) x = self.fc1(x) x = F.relu(x) x = self.dropout2(x) x = self.fc2(x) output = F.log_softmax(x, dim=1) return output