code for saving the model is added

This commit is contained in:
yunjey
2017-03-11 14:54:46 +09:00
parent 278c13513f
commit 86a0872430
17 changed files with 630 additions and 37 deletions

View File

@ -14,12 +14,12 @@ transform = transforms.Compose([
transforms.ToTensor()])
# CIFAR-10 Dataset
train_dataset = dsets.CIFAR10(root='./data/',
train_dataset = dsets.CIFAR10(root='../data/',
train=True,
transform=transform,
download=True)
test_dataset = dsets.CIFAR10(root='./data/',
test_dataset = dsets.CIFAR10(root='../data/',
train=False,
transform=transforms.ToTensor())
@ -109,7 +109,7 @@ lr = 0.001
optimizer = torch.optim.Adam(resnet.parameters(), lr=lr)
# Training
for epoch in range(40):
for epoch in range(80):
for i, (images, labels) in enumerate(train_loader):
images = Variable(images.cuda())
labels = Variable(labels.cuda())
@ -122,7 +122,7 @@ for epoch in range(40):
optimizer.step()
if (i+1) % 100 == 0:
print ("Epoch [%d/%d], Iter [%d/%d] Loss: %.4f" %(epoch+1, 40, i+1, 500, loss.data[0]))
print ("Epoch [%d/%d], Iter [%d/%d] Loss: %.4f" %(epoch+1, 80, i+1, 500, loss.data[0]))
# Decaying Learning Rate
if (epoch+1) % 20 == 0:
@ -130,6 +130,7 @@ for epoch in range(40):
optimizer = torch.optim.Adam(resnet.parameters(), lr=lr)
# Test
resnet.eval()
correct = 0
total = 0
for images, labels in test_loader:
@ -139,4 +140,7 @@ for images, labels in test_loader:
total += labels.size(0)
correct += (predicted.cpu() == labels).sum()
print('Accuracy of the model on the test images: %d %%' % (100 * correct / total))
print('Accuracy of the model on the test images: %d %%' % (100 * correct / total))
# Save the Model
torch.save(resnet, 'resnet.pkl')