/
pytorch_mnist_convnet.py
176 lines (130 loc) · 4.95 KB
/
pytorch_mnist_convnet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import argparse
import torch
import torch.nn as nn
from common import (get_train_loader, get_extended_train_loader,
get_test_loader, train_network, test_network)
OUTPUT_SIZE = 10
class ConvNetSimple(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=20, kernel_size=5)
self.fc1 = nn.Linear(12*12*20, 100)
self.out = nn.Linear(100, OUTPUT_SIZE)
def forward(self, x):
x = self.conv1(x)
x = torch.sigmoid(x)
x = torch.max_pool2d(x, kernel_size=2, stride=2)
x = x.view(-1, 12*12*20)
x = self.fc1(x)
x = torch.sigmoid(x)
x = self.out(x)
return x
class ConvNetTwoConvLayers(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=20, kernel_size=5)
self.conv2 = nn.Conv2d(in_channels=20, out_channels=40, kernel_size=5)
self.fc1 = nn.Linear(4*4*40, 100)
self.out = nn.Linear(100, OUTPUT_SIZE)
def forward(self, x):
x = self.conv1(x)
x = torch.sigmoid(x)
x = torch.max_pool2d(x, kernel_size=2, stride=2)
x = self.conv2(x)
x = torch.sigmoid(x)
x = torch.max_pool2d(x, kernel_size=2, stride=2)
x = x.view(-1, 4*4*40)
x = self.fc1(x)
x = torch.sigmoid(x)
x = self.out(x)
return x
class ConvNetTwoConvLayersReLU(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=20, kernel_size=5)
self.conv2 = nn.Conv2d(in_channels=20, out_channels=40, kernel_size=5)
self.fc1 = nn.Linear(4*4*40, 100)
self.out = nn.Linear(100, OUTPUT_SIZE)
def forward(self, x):
x = self.conv1(x)
x = torch.relu(x)
x = torch.max_pool2d(x, kernel_size=2, stride=2)
x = self.conv2(x)
x = torch.relu(x)
x = torch.max_pool2d(x, kernel_size=2, stride=2)
x = x.view(-1, 4*4*40)
x = self.fc1(x)
x = torch.relu(x)
x = self.out(x)
return x
class ConvNetTwoConvTwoDenseLayersWithDropout(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=20, kernel_size=5)
self.conv2 = nn.Conv2d(in_channels=20, out_channels=40, kernel_size=5)
self.dropout1 = nn.Dropout(p=0.5)
self.fc1 = nn.Linear(4*4*40, 1000)
self.dropout2 = nn.Dropout(p=0.5)
self.fc2 = nn.Linear(1000, 1000)
self.dropout3 = nn.Dropout(p=0.5)
self.out = nn.Linear(1000, OUTPUT_SIZE)
def forward(self, x):
x = self.conv1(x)
x = torch.relu(x)
x = torch.max_pool2d(x, kernel_size=2, stride=2)
x = self.conv2(x)
x = torch.relu(x)
x = torch.max_pool2d(x, kernel_size=2, stride=2)
x = x.view(-1, 4*4*40)
x = self.dropout1(x)
x = self.fc1(x)
x = torch.relu(x)
x = self.dropout2(x)
x = self.fc2(x)
x = torch.relu(x)
x = self.dropout3(x)
x = self.out(x)
return x
def train_and_test_network(net, num_epochs=60, lr=0.1, wd=0,
loss_function=nn.CrossEntropyLoss(),
train_loader=get_train_loader(),
test_loader=get_test_loader()):
sgd = torch.optim.SGD(net.parameters(), lr=lr, weight_decay=wd)
train_network(net, train_loader, num_epochs, loss_function, sgd)
print("")
test_network(net, test_loader)
def choose_network(args):
if args.net == "simple":
return ConvNetSimple()
if args.net == "2conv":
return ConvNetTwoConvLayers()
if args.net == "relu":
return ConvNetTwoConvLayersReLU()
if args.net == "final":
return ConvNetTwoConvTwoDenseLayersWithDropout()
def choose_train_loader(args):
if args.extend_data:
return get_extended_train_loader()
return get_train_loader()
def parse_command_line_args():
parser = argparse.ArgumentParser()
parser.add_argument("--net", help="which network to run",
choices=["simple", "2conv", "relu", "final"],
default="simple")
parser.add_argument("--epochs", help="number of epochs", type=int,
default=60)
parser.add_argument("--lr", help="learning rate", type=float, default=0.1)
parser.add_argument("--wd", help="weight decay", type=float, default=0)
parser.add_argument("--extend_data", help="use extended training data",
action="store_true")
return parser.parse_args()
def main(args):
net = choose_network(args)
num_epochs = args.epochs
lr = args.lr
wd = args.wd
train_loader = choose_train_loader(args)
train_and_test_network(net, num_epochs=num_epochs, lr=lr, wd=wd,
train_loader=train_loader)
if __name__ == "__main__":
main(parse_command_line_args())