-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
63 lines (48 loc) · 1.81 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
import random
import torch
import torch.nn as nn
seed = 99
random.seed(seed)
torch.manual_seed(seed)
class Encoder(nn.Module):
def __init__(self, input_dim=5, hidden_dim=300, latent_dim=2):
super(Encoder, self).__init__()
self.hidden_dim = hidden_dim
self.lstm = nn.LSTM(input_size=input_dim, hidden_size=hidden_dim, bidirectional=True)
self.fc_mu = nn.Linear(hidden_dim * 2, latent_dim)
self.fc_var = nn.Linear(hidden_dim * 2, latent_dim)
def forward(self, x):
h, _ = self.lstm(x)
feat = torch.cat([h[:, -1, :self.hidden_dim], h[:, 0, self.hidden_dim:]], dim=-1)
# Sample mu and var
mu = self.fc_mu(feat)
var = self.fc_var(feat)
return mu, var
class Decoder(nn.Module):
def __init__(self, latent_dim=2):
super(Decoder, self).__init__()
self.regressor = nn.Sequential(*[nn.Linear(latent_dim, 200),
nn.Tanh(),
nn.Linear(200, 1)])
def forward(self, z):
out = self.regressor(z)
return out
def reparameterize(mu, logvar):
"""
Reparameterization trick to sample from N(mu, var) from
N(0,1).
:param mu: (Tensor) Mean of the latent Gaussian [B x D]
:param logvar: (Tensor) Standard deviation of the latent Gaussian [B x D]
:return: (Tensor) [B x D]
"""
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return eps * std + mu
if __name__=='__main__':
device = 'cuda:1'
input = torch.ones([128, 30, 5]).to(device)
encoder = Encoder().to(device)
decoder = Decoder().to(device)
mu, var = encoder(input)
z = reparameterize(mu, var)
out = decoder(z)