-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlenet.py
34 lines (30 loc) · 1.15 KB
/
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
31
32
33
34
import torch
import torch.nn as nn
class LeNet(torch.nn.Module):
def __init__(self, num_classes=10):
super(LeNet, self).__init__()
self.layer1 = torch.nn.Sequential(
torch.nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
torch.nn.BatchNorm2d(16),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=2, stride=2))
self.layer2 = torch.nn.Sequential(
torch.nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
torch.nn.BatchNorm2d(32),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=2, stride=2))
self.layer3 = torch.nn.Sequential(
torch.nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
torch.nn.BatchNorm2d(64),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=2, stride=2)
)
self.fc = torch.nn.Linear(3 * 3 * 64, num_classes)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
if self.layer3 is not None:
out = self.layer3(out)
out = out.reshape(out.size(0), -1)
out = self.fc(out)
return out