Newer
Older
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
import torch
import torch.nn as nn
import torch.nn.functional as F
# This is a basic CNN architecture suitable for smaller images,
# especially used for MNIST in many adversarial training setups.
class SmallCNN(nn.Module):
def __init__(self, num_channels=1, num_classes=10):
super().__init__()
# Four convolutional layers for feature extraction.
# Typically used for grayscale MNIST, so default num_channels=1.
self.conv1 = nn.Conv2d(num_channels, 32, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1)
self.conv3 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
self.conv4 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)
# Then three fully-connected layers for classification.
self.fc1 = nn.Linear(64 * 7 * 7, 200)
self.fc2 = nn.Linear(200, 200)
self.fc3 = nn.Linear(200, num_classes)
def forward(self, x):
# Pass through two conv layers, each with relu,
# then do a 2x2 max pool.
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
# Another pair of conv+relu, then max pool.
x = F.relu(self.conv3(x))
x = F.relu(self.conv4(x))
x = F.max_pool2d(x, 2)
# Flatten the feature maps into a 1D vector.
x = x.view(x.size(0), -1)
# Pass through FC layers with relu, except final layer.
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
# Apply softmax for classification outputs.
x = F.softmax(x, dim=1)
return x
def forward_features(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv3(x))
x = F.relu(self.conv4(x))
x = F.max_pool2d(x, 2)
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
return x