-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
48 lines (37 loc) · 1.21 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
import torch
import torch.nn as nn
def init_weights(m):
if type(m) == nn.Linear:
torch.nn.init.xavier_uniform_(m.weight)
m.bias.data.fill_(0.00)
class Encoder(nn.Module):
def __init__(self, n_inp, n_hidden, n_out):
super(Encoder, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(n_inp, n_hidden),
nn.ReLU(),
)
self.mu_head = nn.Linear(n_hidden, n_out)
self.logvar_head = nn.Linear(n_hidden, n_out)
self.apply(init_weights)
def forward(self, x):
x = self.encoder(x)
mu, log_var = self.mu_head(x), self.logvar_head(x)
return mu, log_var
class Decoder(nn.Module):
def __init__(self, n_inp, n_hidden, n_out):
super(Decoder, self).__init__()
self.decoder = nn.Sequential(
nn.Linear(n_inp, n_hidden),
nn.ReLU(),
nn.Linear(n_hidden, n_out),
)
self.apply(init_weights)
def forward(self, x):
return self.decoder(x)
class Classifier(nn.Module):
def __init__(self, n_inp, n_out):
super(Classifier, self).__init__()
self.fc1 = nn.Linear(n_inp, n_out)
def forward(self, x):
return self.fc1(x)