-
Notifications
You must be signed in to change notification settings - Fork 208
/
lenet.py
30 lines (27 loc) · 893 Bytes
/
lenet.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
from abc import ABC
import torch.nn as nn
class LeNet(nn.Module, ABC):
def __init__(self, num_classes=10):
super(LeNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 6, kernel_size=5),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(6, 16, kernel_size=5),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.classifier = nn.Sequential(
nn.Linear(16 * 4 * 4, 120),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(120, 84),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(84, num_classes),
)
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), 16 * 4 * 4)
x = self.classifier(x)
return x