1
1
from __future__ import print_function
2
+ import os
2
3
import torch
3
4
import torch .nn as nn
4
5
import torch .nn .cuda
9
10
def print_header (msg ):
10
11
print ('===>' , msg )
11
12
13
+ if not os .path .exists ('data/processed/training.pt' ):
14
+ import data
15
+
12
16
# Data
13
17
print_header ('Loading data' )
14
18
with open ('data/processed/training.pt' , 'rb' ) as f :
@@ -29,9 +33,9 @@ def print_header(msg):
29
33
class Net (nn .Container ):
30
34
def __init__ (self ):
31
35
super (Net , self ).__init__ (
32
- conv1 = nn .Conv2d (1 , 20 , 5 , 5 ),
36
+ conv1 = nn .Conv2d (1 , 20 , 5 ),
33
37
pool1 = nn .MaxPool2d (2 , 2 ),
34
- conv2 = nn .Conv2d (20 , 50 , 5 , 5 ),
38
+ conv2 = nn .Conv2d (20 , 50 , 5 ),
35
39
pool2 = nn .MaxPool2d (2 , 2 ),
36
40
fc1 = nn .Linear (800 , 500 ),
37
41
fc2 = nn .Linear (500 , 10 ),
@@ -56,35 +60,40 @@ def __call__(self, x):
56
60
TEST_BATCH_SIZE = 1000
57
61
NUM_EPOCHS = 2
58
62
59
- optimizer = optim .SGD (( model , criterion ) , lr = 1e-2 , momentum = 0.9 )
63
+ optimizer = optim .SGD (model , lr = 1e-2 , momentum = 0.9 )
60
64
61
65
def train (epoch ):
62
66
batch_data = Variable (torch .cuda .FloatTensor (BATCH_SIZE , 1 , 28 , 28 ), requires_grad = False )
63
67
batch_targets = Variable (torch .cuda .FloatTensor (BATCH_SIZE ), requires_grad = False )
64
68
for i in range (0 , training_data .size (0 ), BATCH_SIZE ):
65
69
batch_data .data [:] = training_data [i :i + BATCH_SIZE ]
66
70
batch_targets .data [:] = training_labels [i :i + BATCH_SIZE ]
67
- loss = optimizer .step (batch_data , batch_targets )
68
- model .zero_grad_parameters ()
71
+ loss = optimizer .step (lambda : criterion (model (batch_data ), batch_targets ))
69
72
70
73
print ('Epoch: {} [{}/{} ({:.0f}%)]\t Loss: {:.4f}' .format (epoch ,
71
74
i + BATCH_SIZE , training_data .size (0 ),
72
- (i + BATCH_SIZE )/ training_data .size (0 )* 100 , loss ))
75
+ float (i + BATCH_SIZE )/ training_data .size (0 )* 100 , loss ))
73
76
74
77
def test (epoch ):
75
78
test_loss = 0
76
79
batch_data = Variable (torch .cuda .FloatTensor (TEST_BATCH_SIZE , 1 , 28 , 28 ), volatile = True )
77
80
batch_targets = Variable (torch .cuda .FloatTensor (TEST_BATCH_SIZE ), volatile = True )
81
+ correct = 0
78
82
for i in range (0 , test_data .size (0 ), TEST_BATCH_SIZE ):
79
83
print ('Testing model: {}/{}' .format (i , test_data .size (0 )), end = '\r ' )
80
84
batch_data .data [:] = test_data [i :i + TEST_BATCH_SIZE ]
81
85
batch_targets .data [:] = test_labels [i :i + TEST_BATCH_SIZE ]
82
- test_loss += criterion (model (batch_data ), batch_targets )
86
+ output = model (batch_data )
87
+ test_loss += criterion (output , batch_targets )
88
+ pred = output .data .max (1 )[1 ]
89
+ correct += pred .long ().eq (batch_targets .data .long ()).sum ()
83
90
84
91
test_loss = test_loss .data [0 ]
85
92
test_loss /= (test_data .size (0 ) / TEST_BATCH_SIZE ) # criterion averages over batch size
86
93
print ('TEST SET RESULTS:' + ' ' * 20 )
87
- print ('Average loss: {:.4f}' .format (test_loss ))
94
+ print ('Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)' .format (
95
+ test_loss , correct , test_data .size (0 ),
96
+ float (correct )/ test_data .size (0 )* 100 ))
88
97
89
98
for epoch in range (1 , NUM_EPOCHS + 1 ):
90
99
train (epoch )
0 commit comments