-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.py
110 lines (94 loc) · 3.22 KB
/
net.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from functools import reduce
from torch import nn
from torch.nn import functional as F
class SimpleCNN(nn.Module):
def __init__(self):
super(SimpleCNN, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1, reduce(lambda x, y: x * y, x.size()[1:]))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.bn1 = nn.BatchNorm2d(1)
self.conv1 = nn.Conv2d(1, 6, 5)
self.bn2 = nn.BatchNorm2d(6)
self.conv2 = nn.Conv2d(6, 16, 5)
self.bn3 = nn.BatchNorm2d(16)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.bn4 = nn.BatchNorm1d(120)
self.fc2 = nn.Linear(120, 84)
self.bn5 = nn.BatchNorm1d(84)
self.drop = nn.Dropout()
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.bn1(x)
x = F.max_pool2d(F.relu(self.conv1(x)), 2)
x = self.bn2(x)
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = self.bn3(x)
x = x.view(-1, reduce(lambda x, y: x * y, x.size()[1:]))
x = F.relu(self.fc1(x))
x = self.bn4(x)
x = F.relu(self.fc2(x))
x = self.bn5(x)
x = self.drop(x)
x = self.fc3(x)
return x
class RichCNN(nn.Module):
def __init__(self):
super(RichCNN, self).__init__()
self.bn1 = nn.BatchNorm2d(1)
self.conv1 = nn.Conv2d(1, 32, 5)
self.bn2 = nn.BatchNorm2d(32)
self.conv2 = nn.Conv2d(32, 64, 5)
self.bn3 = nn.BatchNorm2d(64)
self.fc1 = nn.Linear(64 * 5 * 5, 1024)
self.bn4 = nn.BatchNorm1d(1024)
self.drop = nn.Dropout()
self.fc2 = nn.Linear(1024, 10)
def forward(self, x):
x = self.bn1(x)
x = F.max_pool2d(F.relu(self.conv1(x)), 2)
x = self.bn2(x)
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = self.bn3(x)
x = x.view(-1, reduce(lambda x, y: x * y, x.size()[1:]))
x = F.relu(self.fc1(x))
x = self.bn4(x)
x = self.drop(x)
x = self.fc2(x)
return x
class PracticalCNN(nn.Module):
def __init__(self):
super(PracticalCNN, self).__init__()
self.conv1 = nn.Conv2d(1, 5, 5)
self.conv2 = nn.Conv2d(5, 50, 5)
self.fc1 = nn.Linear(50 * 5 * 5, 100)
self.fc2 = nn.Linear(100, 10)
def forward(self, x):
x = F.max_pool2d(F.relu(self.conv1(x)), 2)
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1, reduce(lambda x, y: x * y, x.size()[1:]))
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
class FullyConnected(nn.Module):
def __init__(self):
super(FullyConnected, self).__init__()
self.fc1 = nn.Linear(784, 800)
self.fc2 = nn.Linear(800, 10)
def forward(self, x):
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x