-
Notifications
You must be signed in to change notification settings - Fork 0
/
lenet.py
92 lines (68 loc) · 2.38 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
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
from __future__ import print_function
import torch.nn as nn
import torch.nn.functional as F
class LetNet_Decomposition(nn.Module):
def __init__(self):
super(LetNet_Decomposition, self).__init__()
self.conv1_sigma = nn.Conv2d(1, 6, 5, stride=1, padding=2)
self.conv2_sigma = nn.Conv2d(6, 16, 5)
self.fc1_sigma = nn.Linear(400, 120)
self.fc2_sigma = nn.Linear(120, 84)
self.fc3_sigma = nn.Linear(84, 10)
self.conv1_gamma = nn.Conv2d(1, 6, 5, stride=1, padding=2)
self.conv2_gamma = nn.Conv2d(6, 16, 5)
self.fc1_gamma = nn.Linear(400, 120)
self.fc2_gamma = nn.Linear(120, 84)
self.fc3_gamma = nn.Linear(84, 10)
def forward(self, x):
x_sigma = self.conv1_sigma(x)
x_gamma = self.conv1_gamma(x)
x = x_sigma + x_gamma
x = F.max_pool2d(F.relu(x), 2)
x_sigma = self.conv2_sigma(x)
x_gamma = self.conv2_gamma(x)
x = x_sigma + x_gamma
x = F.max_pool2d(F.relu(x), 2)
x = x.view(x.size(0), -1)
x_sigma = self.fc1_sigma(x)
x_gamma = self.fc1_gamma(x)
x = x_sigma + x_gamma
x = F.relu(x)
x_sigma = self.fc2_sigma(x)
x_gamma = self.fc2_gamma(x)
x = x_sigma + x_gamma
x = F.relu(x)
x_sigma = self.fc3_sigma(x)
x_gamma = self.fc3_gamma(x)
x = x_sigma + x_gamma
return x
def predict(self, x):
x = self.conv1_sigma(x)
x = F.max_pool2d(F.relu(x), 2)
x = self.conv2_sigma(x)
x = F.max_pool2d(F.relu(x), 2)
x = x.view(x.size(0), -1)
x = self.fc1_sigma(x)
x = F.relu(x)
x = self.fc2_sigma(x)
x = F.relu(x)
x = self.fc3_sigma(x)
return x
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5, stride=1, padding=2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(400, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
out = F.relu(self.conv1(x))
out = F.max_pool2d(out, 2)
out = F.relu(self.conv2(out))
out = F.max_pool2d(out, 2)
out = out.view(out.size(0), -1)
out = F.relu(self.fc1(out))
out = F.relu(self.fc2(out))
out = self.fc3(out)
return out