-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
111 lines (89 loc) · 2.91 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from groupsort import GroupSort
class Encoder(nn.Module):
def __init__(self):
super().__init__()
self.enc_1 = torch.nn.Linear(3,3)
self.enc_2 = torch.nn.Linear(3, 3)
self.enc_3 = torch.nn.Linear(3, 3)
self.enc_4 = torch.nn.Linear(3, 2)
self.gp = GroupSort(1, axis=1)
def forward(self, x, gps = 0):
if gps == 1:
x = self.gp(self.enc_1(x))
x = self.gp(self.enc_2(x))
x = self.gp(self.enc_3(x))
x = self.gp(self.enc_4(x))
elif gps == 0:
x = F.leaky_relu(self.enc_1(x))
x = F.leaky_relu(self.enc_2(x))
x = F.leaky_relu(self.enc_3(x))
x = F.leaky_relu(self.enc_4(x))
return x
class Decoder(nn.Module):
def __init__(self):
super().__init__()
self.dec_1 = torch.nn.Linear(2, 3)
self.dec_2 = torch.nn.Linear(3, 3)
self.dec_3 = torch.nn.Linear(3, 3)
self.dec_4 = torch.nn.Linear(3, 3)
self.gp = GroupSort(1, axis=1)
def forward(self, x, gps = 0):
if gps == 1:
x = self.gp(self.dec_1(x))
x = self.gp(self.dec_2(x))
x = self.gp(self.dec_3(x))
x = self.dec_4(x)
elif gps == 0:
x = F.leaky_relu(self.dec_1(x))
x = F.leaky_relu(self.dec_2(x))
x = F.leaky_relu(self.dec_3(x))
x = self.dec_4(x)
return x
class Encoder_Mnist(nn.Module):
def __init__(self):
super().__init__()
self.enc_1 = torch.nn.Linear(1*28*28, 512, bias = False)
self.new_enc3 = torch.nn.Linear(512, 256, bias = False)
self.new_enc4 = torch.nn.Linear(256, 128)
self.enc_2 = torch.nn.Linear(128, 64)
self.gp = GroupSort(1, axis=1)
def forward(self, x, gps = 0):
if gps == 1:
x = x.reshape(x.size(0), 1*28*28)
x = self.gp(self.enc_1(x))
x = self.gp(self.new_enc3(x))
x = self.gp(self.new_enc4(x))
x = self.gp(self.enc_2(x))
elif gps == 0:
x = x.reshape(x.size(0), 1*28*28)
x = F.relu(self.enc_1(x))
x = F.relu(self.new_enc3(x))
x = F.relu(self.new_enc4(x))
x = F.relu(self.enc_2(x))
return x
class Decoder_Mnist(nn.Module):
def __init__(self):
super().__init__()
#hidden layer
self.dec_3 = torch.nn.Linear(64, 128)
self.new_dec1 = torch.nn.Linear(128, 256)
self.new_dec2 = torch.nn.Linear(256, 512, bias = False)
self.dec_4 = torch.nn.Linear(512, 1*28*28, bias = False)
self.gp = GroupSort(1, axis=1)
def forward(self, x, gps = 0):
if gps == 1:
x = self.gp(self.dec_3(x))
x = self.gp(self.new_dec1(x))
x = self.gp(self.new_dec2(x))
x = torch.sigmoid(self.dec_4(x))
x = x.reshape(x.size(0), 1,28,28)
elif gps == 0:
x = F.relu(self.dec_3(x))
x = F.relu(self.new_dec1(x))
x = F.relu(self.new_dec2(x))
x = torch.sigmoid(self.dec_4(x))
x = x.reshape(x.size(0), 1,28,28)
return x