-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
48 lines (42 loc) · 1.57 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
import torch
import torch.nn as nn
class VAE(nn.Module):
def __init__(self, input_dim, hidden_dim, latent_dim, output_dim):
super(VAE, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.GELU(),
nn.Linear(hidden_dim, hidden_dim),
nn.GELU()
)
self.mu = nn.Linear(hidden_dim, latent_dim)
self.log_var = nn.Linear(hidden_dim, latent_dim)
self.decoder = nn.Sequential(
nn.Linear(latent_dim, hidden_dim),
nn.GELU(),
nn.Linear(hidden_dim, hidden_dim),
nn.GELU(),
nn.Linear(hidden_dim, output_dim)
)
def encode(self, x):
h = self.encoder(x)
mu = self.mu(h)
log_var = self.log_var(h)
return mu, log_var
def reparameterize(self, mu, log_var):
std = torch.exp(0.5 * log_var)
eps = torch.randn_like(std)
return mu + eps * std
def decode(self, z):
recon_x = self.decoder(z)
return recon_x
def forward(self, x):
mu, log_var = self.encode(x)
z = self.reparameterize(mu, log_var)
recon_x = self.decode(z)
return recon_x, mu, log_var
def loss_function(recon_x, x, mu, log_var, mask, lambda_weight):
recon_loss = nn.MSELoss(reduction='none')(recon_x, x)
recon_loss = (recon_loss * (1 - mask) * (1 - lambda_weight) + recon_loss * mask * lambda_weight).sum(dim=-1).mean()
kl_loss = -0.5 * torch.sum(1 + log_var - mu.pow(2) - log_var.exp(), dim=-1).mean()
return recon_loss + kl_loss * 0