diff --git a/mnist/main.py b/mnist/main.py index e3b7fc0beb..e971f06a76 100644 --- a/mnist/main.py +++ b/mnist/main.py @@ -6,24 +6,25 @@ import torch.optim as optim from torchvision import datasets, transforms + class Net(nn.Module): def __init__(self): super(Net, self).__init__() - self.conv1 = nn.Conv2d(1, 10, kernel_size=5) - self.conv2 = nn.Conv2d(10, 20, kernel_size=5) - self.conv2_drop = nn.Dropout2d() - self.fc1 = nn.Linear(320, 50) - self.fc2 = nn.Linear(50, 10) + 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): - x = F.relu(F.max_pool2d(self.conv1(x), 2)) - x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) - x = x.view(-1, 320) + 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) + x = x.view(-1, 4*4*50) x = F.relu(self.fc1(x)) - x = F.dropout(x, training=self.training) x = self.fc2(x) return F.log_softmax(x, dim=1) - + def train(args, model, device, train_loader, optimizer, epoch): model.train() for batch_idx, (data, target) in enumerate(train_loader): @@ -51,6 +52,7 @@ def test(args, model, device, test_loader): correct += pred.eq(target.view_as(pred)).sum().item() test_loss /= len(test_loader.dataset) + print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( test_loss, correct, len(test_loader.dataset), 100. * correct / len(test_loader.dataset))) @@ -74,6 +76,9 @@ def main(): help='random seed (default: 1)') parser.add_argument('--log-interval', type=int, default=10, metavar='N', help='how many batches to wait before logging training status') + + parser.add_argument('--save-model', action='store_true', default=False, + help='For Saving the current Model') args = parser.parse_args() use_cuda = not args.no_cuda and torch.cuda.is_available() @@ -104,6 +109,8 @@ def main(): train(args, model, device, train_loader, optimizer, epoch) test(args, model, device, test_loader) - + if (args.save_model): + torch.save(model.state_dict(),"mnist_cnn.pt") + if __name__ == '__main__': - main() \ No newline at end of file + main()