-
Notifications
You must be signed in to change notification settings - Fork 1
/
cnn.py
29 lines (25 loc) · 912 Bytes
/
cnn.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
from torch import nn
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(1, 128, kernel_size=5, stride=1, padding=2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2))
self.layer2 = nn.Sequential(
nn.Conv2d(128, 256, kernel_size=5, stride=1, padding=2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2))
self.dropout = nn.Dropout() # Dropout to prevent overfitting
self.fc1 = nn.Linear(7 * 7 * 256, 512)
self.fc2 = nn.Linear(512, 62)
self.softmax = nn.LogSoftmax(dim=1)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = out.reshape(out.size(0), -1)
out = self.dropout(out)
out = self.fc1(out)
out = self.fc2(out)
out = self.softmax(out)
return out