Skip to content

Commit

Permalink
Fix formatting to comply with PEP8 (pytorch#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
fshabashev authored and soumith committed Nov 3, 2019
1 parent 4e00723 commit 60108ed
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions mnist/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def forward(self, x):
x = F.relu(self.fc1(x))
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):
Expand All @@ -39,6 +40,7 @@ def train(args, model, device, train_loader, optimizer, epoch):
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))


def test(args, model, device, test_loader):
model.eval()
test_loss = 0
Expand All @@ -47,8 +49,8 @@ def test(args, model, device, test_loader):
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss
pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability
test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss
pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability
correct += pred.eq(target.view_as(pred)).sum().item()

test_loss /= len(test_loader.dataset)
Expand All @@ -57,6 +59,7 @@ def test(args, model, device, test_loader):
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))


def main():
# Training settings
parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
Expand All @@ -76,7 +79,7 @@ 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()
Expand All @@ -101,16 +104,16 @@ def main():
])),
batch_size=args.test_batch_size, shuffle=True, **kwargs)


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

for epoch in range(1, args.epochs + 1):
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 args.save_model:
torch.save(model.state_dict(), "mnist_cnn.pt")


if __name__ == '__main__':
main()

0 comments on commit 60108ed

Please sign in to comment.