| 3 | import torch |
| 4 | |
| 5 | class VariationalEncoder(nn.Module): |
| 6 | def __init__(self,latent_dims): |
| 7 | super(VariationalEncoder,self).__init__() |
| 8 | self.linear1 = nn.Linear(256,128) |
| 9 | self.linear2 = nn.Linear(128,64) |
| 10 | self.linear3 = nn.Linear(64,latent_dims) |
| 11 | self.linear4 = nn.Linear(64,latent_dims) |
| 12 | |
| 13 | self.N = torch.distributions.Normal(0,1) |
| 14 | self.N.loc = self.N.loc.cuda() |
| 15 | self.N.scale = self.N.scale.cuda() |
| 16 | self.kl = 0 |
| 17 | |
| 18 | def forward(self,x): |
| 19 | x = F.relu(self.linear1(x)) |
| 20 | x = F.relu(self.linear2(x)) |
| 21 | mu = self.linear3(x) |
| 22 | sigma = torch.exp(self.linear4(x)) |
| 23 | z = mu + sigma*self.N.sample(mu.shape) |
| 24 | self.kl = (sigma**2 + mu**2 - torch.log(sigma) - 1/2).sum() |
| 25 | return z |
| 26 | class Decoder(nn.Module): |
| 27 | def __init__(self,latent_dims): |
| 28 | super(Decoder,self).__init__() |