-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
112 lines (87 loc) · 2.79 KB
/
models.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
111
112
import torch.nn as nn
class twoLNN(nn.Module):
def __init__(self, K):
# K inidicating the number of MNIST images
super(twoLNN, self).__init__()
self.K = K
self.first_section = nn.Sequential(
nn.Linear(28 * 28 * K, 512),
nn.ReLU()
)
self.parity = nn.Linear(512, 1)
self.classification = nn.Linear(512,10)
def forward(self, x):
x = x.view(-1, 28*28*self.K)
x = self.first_section(x)
x = self.parity(x)
return(x)
def classify(self, x):
x = x.view(-1, 28*28*self.K)
x = self.first_section(x)
x = self.classification(x)
return(x)
def extract_representation(self, x):
x = x.view(-1, 28*28*self.K)
x = self.first_section(x)
return(x)
class fiveLNN(nn.Module):
def __init__(self, K):
# K inidicating the number of MNIST images
super(fiveLNN, self).__init__()
self.K = K
self.first_section = nn.Sequential(
nn.Linear(28 * 28 * K, 512),
nn.ReLU(),
nn.Linear(512, 256),
nn.ReLU(),
nn.Linear(256, 128),
nn.ReLU(),
nn.Linear(128, 64),
nn.ReLU()
)
self.parity = nn.Linear(64, 1)
self.classification = nn.Linear(64,10)
def forward(self, x):
x = x.view(-1, 28*28*self.K)
x = self.first_section(x)
x = self.parity(x)
return(x)
def classify(self, x):
x = x.view(-1, 28*28*self.K)
x = self.first_section(x)
x = self.classification(x)
return(x)
def extract_representation(self, x):
x = x.view(-1, 28*28*self.K)
x = self.first_section(x)
return(x)
class LeNet(nn.Module):
def __init__(self, K):
super(LeNet, self).__init__()
self.K = K
self.first_section = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, padding=2),
nn.ReLU(),
nn.AvgPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5, padding=0),
nn.ReLU(),
nn.AvgPool2d(kernel_size=2, stride=2),
nn.Flatten(),
nn.Linear(16*5*(5+7*K-7), 120),
nn.ReLU(),
nn.Linear(120, 84),
nn.ReLU()
)
self.parity = nn.Linear(84,1)
self.classification = nn.Linear(84,10)
def forward(self,x):
x = self.first_section(x)
x = self.parity(x)
return(x)
def classify(self, x):
x = self.first_section(x)
x = self.classification(x)
return(x)
def extract_representation(self, x):
x = self.first_section(x)
return(x)