1414 transforms .ToTensor ()])
1515
1616# CIFAR-10 Dataset
17- train_dataset = dsets .CIFAR10 (root = './data/' ,
17+ train_dataset = dsets .CIFAR10 (root = '.. /data/' ,
1818 train = True ,
1919 transform = transform ,
2020 download = True )
2121
22- test_dataset = dsets .CIFAR10 (root = './data/' ,
22+ test_dataset = dsets .CIFAR10 (root = '.. /data/' ,
2323 train = False ,
2424 transform = transforms .ToTensor ())
2525
@@ -109,7 +109,7 @@ def forward(self, x):
109109optimizer = torch .optim .Adam (resnet .parameters (), lr = lr )
110110
111111# Training
112- for epoch in range (40 ):
112+ for epoch in range (80 ):
113113 for i , (images , labels ) in enumerate (train_loader ):
114114 images = Variable (images .cuda ())
115115 labels = Variable (labels .cuda ())
@@ -122,14 +122,15 @@ def forward(self, x):
122122 optimizer .step ()
123123
124124 if (i + 1 ) % 100 == 0 :
125- print ("Epoch [%d/%d], Iter [%d/%d] Loss: %.4f" % (epoch + 1 , 40 , i + 1 , 500 , loss .data [0 ]))
125+ print ("Epoch [%d/%d], Iter [%d/%d] Loss: %.4f" % (epoch + 1 , 80 , i + 1 , 500 , loss .data [0 ]))
126126
127127 # Decaying Learning Rate
128128 if (epoch + 1 ) % 20 == 0 :
129129 lr /= 3
130130 optimizer = torch .optim .Adam (resnet .parameters (), lr = lr )
131131
132132# Test
133+ resnet .eval ()
133134correct = 0
134135total = 0
135136for images , labels in test_loader :
@@ -139,4 +140,7 @@ def forward(self, x):
139140 total += labels .size (0 )
140141 correct += (predicted .cpu () == labels ).sum ()
141142
142- print ('Accuracy of the model on the test images: %d %%' % (100 * correct / total ))
143+ print ('Accuracy of the model on the test images: %d %%' % (100 * correct / total ))
144+
145+ # Save the Model
146+ torch .save (resnet , 'resnet.pkl' )
0 commit comments