-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
74 lines (42 loc) · 1.88 KB
/
models.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Linear') != -1:
m.bias.data.fill_(0)
nn.init.xavier_uniform_(m.weight,gain=0.5)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
class encoder_template(nn.Module):
def __init__(self,input_dim,latent_size,hidden_size_rule,device):
super(encoder_template,self).__init__()
if len(hidden_size_rule)==2:
self.layer_sizes = [input_dim, hidden_size_rule[0], latent_size]
elif len(hidden_size_rule)==3:
self.layer_sizes = [input_dim, hidden_size_rule[0], hidden_size_rule[1] , latent_size]
modules = []
for i in range(len(self.layer_sizes)-2):
modules.append(nn.Linear(self.layer_sizes[i],self.layer_sizes[i+1]))
modules.append(nn.ReLU())
self.feature_encoder = nn.Sequential(*modules)
self._mu = nn.Linear(in_features=self.layer_sizes[-2], out_features=latent_size)
self._logvar = nn.Linear(in_features=self.layer_sizes[-2], out_features=latent_size)
self.apply(weights_init)
self.to(device)
def forward(self,x):
h = self.feature_encoder(x)
mu = self._mu(h)
logvar = self._logvar(h)
return mu, logvar
class decoder_template(nn.Module):
def __init__(self,input_dim,output_dim,hidden_size_rule,device):
super(decoder_template,self).__init__()
self.layer_sizes = [input_dim, hidden_size_rule[-1] , output_dim]
self.feature_decoder = nn.Sequential(nn.Linear(input_dim,self.layer_sizes[1]),nn.ReLU(),nn.Linear(self.layer_sizes[1],output_dim))
self.apply(weights_init)
self.to(device)
def forward(self,x):
return self.feature_decoder(x)