-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
141 lines (119 loc) · 4.29 KB
/
model.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import torch
from torch import nn
import torch.nn.functional as F
import torchvision.models as models
from utils import *
class Leader(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(785, 784)
self.fc2 = nn.Linear(784, 784)
self.fc3 = nn.Linear(784, 784)
def forward(self, x):
x = F.leaky_relu(self.fc1(x), negative_slope=0.2)
x = F.leaky_relu(self.fc2(x), negative_slope=0.2)
return self.fc3(x).tanh()
class Leader_cnn(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(785, 784)
self.fc2 = nn.Linear(784, 784)
self.fc3 = nn.Linear(784, 784)
def forward(self, x):
x = x.view(-1,785)
x = F.leaky_relu(self.fc1(x), negative_slope=0.2)
x = F.leaky_relu(self.fc2(x), negative_slope=0.2)
x = self.fc3(x).tanh()
x = x.view(-1,1,28,28)
return x
class Leader_cifar(nn.Module):
def __init__(self):
super(Leader_cifar, self).__init__()
# Input size: [batch, 3, 32, 32]
# Output size: [batch, 3, 32, 32]
self.encoder = nn.Sequential(
nn.Conv2d(3, 12, 4, stride=2, padding=1), # [batch, 12, 16, 16]
nn.ReLU(),
nn.Conv2d(12, 24, 4, stride=2, padding=1), # [batch, 24, 8, 8]
nn.ReLU(),
nn.Conv2d(24, 48, 4, stride=2, padding=1), # [batch, 48, 4, 4]
nn.ReLU(),
# nn.Conv2d(48, 96, 4, stride=2, padding=1), # [batch, 96, 2, 2]
# nn.ReLU(),
)
self.decoder = nn.Sequential(
# nn.ConvTranspose2d(96, 48, 4, stride=2, padding=1), # [batch, 48, 4, 4]
# nn.ReLU(),
nn.ConvTranspose2d(48, 24, 4, stride=2, padding=1), # [batch, 24, 8, 8]
nn.ReLU(),
nn.ConvTranspose2d(24, 12, 4, stride=2, padding=1), # [batch, 12, 16, 16]
nn.ReLU(),
nn.ConvTranspose2d(12, 3, 4, stride=2, padding=1), # [batch, 3, 32, 32]
)
def forward(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
class Follower_lr(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(784, 10)
def forward(self, x):
x = x.view(-1,28*28)
x = self.fc1(x)
return x
class Follower_mlp(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(784, 784)
self.fc2 = nn.Linear(784,784)
self.fc3 = nn.Linear(784,10)
def forward(self,x):
#x = x.view(-1,28*28)
x = F.leaky_relu(self.fc1(x), negative_slope=0.2)
x = F.leaky_relu(self.fc2(x), negative_slope=0.2)
x = self.fc3(x)
return x
class Follower_cnn(nn.Module):
def __init__(self):
super(Follower, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(
in_channels=1,
out_channels=16,
kernel_size=5,
stride=1,
padding=2,
),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
)
self.conv2 = nn.Sequential(
nn.Conv2d(16, 32, 5, 1, 2),
nn.ReLU(),
nn.MaxPool2d(2),
)
self.out = nn.Linear(32 * 7 * 7, 10)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = x.view(x.size(0), -1)
output = self.out(x)
return output
class Follower_cifar(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
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 = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = torch.flatten(x, 1) # flatten all dimensions except batch
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x