-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder_decoder_vae.py
149 lines (127 loc) · 5.01 KB
/
encoder_decoder_vae.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
142
143
144
145
146
147
# Classes
import torch
import pyro
class MLP_x_to_p_dz(torch.nn.Module):
""" x -> p,dz """
def __init__(self, params):
super().__init__()
self.K = params['K']
self.ch = params['CHANNELS']
self.width = params['WIDTH']
self.height = params['HEIGHT']
self.z_dim = params['Z_DIM']
self.x_dim = self.ch*self.width*self.height
self.comp_p = torch.nn.Linear(self.x_dim,self.K, bias=True)
self.comp_dz_mu = torch.nn.Linear(self.x_dim,self.z_dim, bias=True)
self.comp_dz_std = torch.nn.Linear(self.x_dim,self.z_dim, bias=True)
self.softmax = torch.nn.Softmax(dim=-1)
self.tanh = torch.nn.Tanh()
def forward(self,x):
batch_size = x.shape[0]
x1 = x.view(batch_size,-1)
p = self.softmax(self.comp_p(x1))
dz_mu= self.tanh(self.comp_dz_mu(x1))
dz_std= torch.exp(self.comp_dz_std(x1))
return p,dz_mu,dz_std
class MLP_x_to_z(torch.nn.Module):
""" x -> z """
def __init__(self, params):
super().__init__()
self.ch = params['CHANNELS']
self.width = params['WIDTH']
self.height = params['HEIGHT']
self.z_dim = params['Z_DIM']
self.x_dim = self.ch*self.width*self.height
self.layer1 = torch.nn.Linear(self.x_dim,100, bias=True)
self.comp_z_mu = torch.nn.Linear(100,self.z_dim, bias=True)
self.comp_z_std = torch.nn.Linear(100,self.z_dim, bias=True)
self.relu = torch.nn.ReLU()
def forward(self,x):
batch_size = x.shape[0]
x1 = x.view(batch_size,-1)
x2 = self.relu(self.layer1(x1))
z_mu = self.comp_z_mu(x2)
z_std = torch.exp(self.comp_z_std(x2))
return z_mu,z_std
class MLP_z_to_x(torch.nn.Module):
""" z -> x """
def __init__(self, params):
super().__init__()
self.ch = params['CHANNELS']
self.width = params['WIDTH']
self.height = params['HEIGHT']
self.z_dim = params['Z_DIM']
self.x_dim = self.ch*self.width*self.height
self.comp_x_mu = torch.nn.Linear(self.z_dim,self.x_dim, bias=True)
def forward(self,z):
batch_size = z.shape[0]
x_mu = torch.sigmoid(self.comp_x_mu(z)).view(batch_size,self.ch,self.height,self.width)
return x_mu
class VAE_no_latent_structure(torch.nn.Module):
def __init__(self,params,encoder,decoder):
super().__init__()
# Parameters
self.use_cuda = params['use_cuda']
self.ch = params['CHANNELS']
self.width = params['WIDTH']
self.height = params['HEIGHT']
self.z_dim = params['Z_DIM']
self.x_dim = self.ch*self.width*self.height
# Instantiate the encoder and decoder
self.decoder = decoder
self.encoder = encoder
if(self.use_cuda):
self.cuda()
def guide(self, imgs=None):
""" 1. run the inference
2. sample latent variables
"""
#-----------------------#
#-------- Trick -------#
#-----------------------#
if(imgs is None):
observed = False
imgs = torch.zeros(8,self.ch,self.height,self.width)
if(self.use_cuda):
imgs=imgs.cuda()
else:
observed = True
#-----------------------#
#----- Enf of Trick ----#
#-----------------------#
batch_size,ch,width,height = imgs.shape
pyro.module("encoder", self.encoder)
z_mu,z_std = self.encoder(imgs)
with pyro.plate('batch_size', batch_size, dim=-1):
z = pyro.sample('z_latent', dist.Normal(z_mu,z_std).to_event(1))
return z_mu,z_std
def model(self, imgs=None):
""" 1. sample the latent from the prior:
2. runs the generative model
3. score the generative model against actual data
"""
#-----------------------#
#-------- Trick -------#
#-----------------------#
if(imgs is None):
observed = False
imgs = torch.zeros(8,self.ch,self.height,self.width)
if(self.use_cuda):
imgs=imgs.cuda()
else:
observed = True
#-----------------------#
#----- Enf of Trick ----#
#-----------------------#
sigma = pyro.param("sigma", 0.01*imgs.new_ones(1))
batch_size,ch,width,height = imgs.shape
pyro.module("decoder", self.decoder)
with pyro.plate('batch_size', batch_size, dim=-1):
z = pyro.sample('z_latent', dist.Normal(imgs.new_zeros(self.z_dim),10*imgs.new_ones(self.z_dim)).to_event(1))
x_mu = self.decoder(z) #x_mu is between 0 and 1
pyro.sample('obs', dist.Normal(x_mu.view(batch_size,-1),sigma).to_event(1), obs=imgs.view(batch_size,-1))
return x_mu
def reconstruct(self,imgs):
z_mu,z_std = self.encoder(imgs)
x = self.decoder(z_mu)
return x