mirror of
https://github.com/yunjey/pytorch-tutorial.git
synced 2025-07-26 19:48:34 +08:00
code for saving the model is added
This commit is contained in:
@ -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')
|
Reference in New Issue
Block a user