-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvae.py
More file actions
215 lines (189 loc) · 5.63 KB
/
vae.py
File metadata and controls
215 lines (189 loc) · 5.63 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from torch import nn
import torch
class PrintShapeSequential(nn.Sequential):
def forward(self, inp):
for module in self:
inp = module(inp)
print(inp.shape)
return inp
# what happens if you significantly reduce
# expressivity of Encoder block?
# ... stunted works just as well as dnn
class Encoder(nn.Module):
def __init__(self, z_dim, dropout=0.1):
super().__init__()
self.stunted = nn.Sequential(
nn.Dropout1d(p=0.1),
nn.Linear(784,128),
nn.Sigmoid()
)
self.dnn = nn.Sequential(
# 26x26 # target 512
nn.Dropout1d(p=dropout),
nn.Linear(784,512),
nn.ReLU(),
nn.Dropout1d(p=dropout),
nn.Linear(512,512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Dropout1d(p=dropout),
# 24x24 # target 256
nn.Linear(512,256),
nn.ReLU(),
nn.Dropout1d(p=dropout),
nn.Linear(256,256),
nn.BatchNorm1d(256),
nn.ReLU(),
nn.Dropout(p=0.1),
# 12x12 # target 128
nn.Linear(256,128),
nn.ReLU(),
nn.Dropout1d(p=dropout),
nn.Linear(128,128),
nn.BatchNorm1d(128),
nn.Sigmoid(),
)
self.mean = nn.Sequential(
self.stunted,
nn.Linear(128,z_dim)
)
self.variance = nn.Sequential(
self.stunted,
nn.Linear(128,z_dim)
)
self.e = torch.randn((1, z_dim))
def forward(self, x):
u,v = self.mean(x), self.variance(x)
z = v * self.e + u
return z
class Decoder(nn.Module):
def __init__(self,z_dim, dropout=0.1):
super().__init__()
self.decoder = nn.Sequential(
# latent_dim -> 128
nn.Linear(z_dim,128),
nn.BatchNorm1d(128),
nn.ReLU(),
# target 256
nn.Dropout1d(p=dropout),
nn.Linear(128,256),
nn.ReLU(),
nn.Linear(256,256),
nn.BatchNorm1d(256),
nn.ReLU(),
# target 512
nn.Dropout1d(p=dropout),
nn.Linear(256,512),
nn.ReLU(),
nn.Linear(512,512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Linear(512,784),
nn.Sigmoid(),
)
# let z be equal to x
def forward(self, z):
ret = self.decoder(z)
#print(ret.shape)
return ret
class VAE(nn.Module):
def __init__(self, z_dim, dropout=0.1):
super().__init__()
self.encoder = Encoder(z_dim, dropout=dropout)
self.decoder = Decoder(z_dim, dropout=dropout)
self.z_dim = z_dim
def forward(self, x):
z = self.encoder(x)
#print(z.shape)
o = self.decoder(z)
return o
class CEncoder(nn.Module):
def __init__(self, z_dim, dropout=0.1):
super().__init__()
self.stunted = nn.Sequential(
nn.Dropout1d(p=0.1),
nn.Linear(794,128),
nn.Sigmoid()
)
self.dnn = nn.Sequential(
# 26x26 # target 512
nn.Dropout1d(p=dropout),
nn.Linear(794,512),
nn.ReLU(),
nn.Dropout1d(p=dropout),
nn.Linear(512,512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Dropout1d(p=dropout),
# 24x24 # target 256
nn.Linear(512,256),
nn.ReLU(),
nn.Dropout1d(p=dropout),
nn.Linear(256,256),
nn.BatchNorm1d(256),
nn.ReLU(),
nn.Dropout(p=0.1),
# 12x12 # target 128
nn.Linear(256,128),
nn.ReLU(),
nn.Dropout1d(p=dropout),
nn.Linear(128,128),
nn.BatchNorm1d(128),
nn.Sigmoid(),
)
self.mean = nn.Sequential(
self.dnn,
nn.Linear(128,z_dim)
)
self.variance = nn.Sequential(
self.dnn,
nn.Linear(128,z_dim)
)
self.e = torch.randn((1, z_dim))
def forward(self,x,y):
x = torch.cat((x,y),dim=-1)
m,v = self.mean(x), self.variance(x)
z = self.reparameterize(m,v)
return z, m, v
def reparameterize(self, mu, logvar):
std = torch.exp(0.5*logvar)
eps = torch.randn_like(std)
return mu + eps*std
class CDecoder(nn.Module):
def __init__(self,z_dim, dropout=0.1):
super().__init__()
self.decoder = nn.Sequential(
# latent_dim -> 128
nn.Linear(z_dim+10,128),
nn.BatchNorm1d(128),
nn.ReLU(),
# target 256
nn.Dropout1d(p=dropout),
nn.Linear(128,256),
nn.ReLU(),
nn.Linear(256,256),
nn.BatchNorm1d(256),
nn.ReLU(),
# target 512
nn.Dropout1d(p=dropout),
nn.Linear(256,512),
nn.ReLU(),
nn.Linear(512,512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Linear(512,784),
nn.Sigmoid(),
)
# let z be equal to x
def forward(self, z, y):
ret = self.decoder(torch.cat((z,y), dim=-1))
return ret
class CVAE(nn.Module):
def __init__(self, z_dim, dropout=0.1):
super().__init__()
self.encoder = CEncoder(z_dim, dropout=dropout)
self.decoder = CDecoder(z_dim, dropout=dropout)
def forward(self,x,y):
z,m,v = self.encoder(x,y)
recon_x = self.decoder(z,y)
return recon_x,m,v