-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathae.py
84 lines (80 loc) · 2.41 KB
/
ae.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
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
import torch.nn as nn
def init_weights(m):
""" initialize weights of fully connected layer
"""
if type(m) == nn.Linear:
nn.init.xavier_uniform_(m.weight)
m.bias.data.fill_(0.01)
# autoencoder with hidden units 20, 2, 20
# Encoder
class Encoder_2(nn.Module):
def __init__(self, num_inputs):
super(Encoder_2, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(num_inputs, 20),
nn.ReLU(),
nn.Linear(20, 2))
self.encoder.apply(init_weights)
def forward(self, x):
x = self.encoder(x)
return x
# Decoder
class Decoder_2(nn.Module):
def __init__(self, num_inputs):
super(Decoder_2, self).__init__()
self.decoder = nn.Sequential(
nn.Linear(2, 20),
nn.ReLU(),
nn.Linear(20, num_inputs),
nn.ReLU())
self.decoder.apply(init_weights)
def forward(self, x):
x = self.decoder(x)
return x
# Autoencoder
class autoencoder_2(nn.Module):
def __init__(self, num_inputs):
super(autoencoder_2, self).__init__()
self.encoder = Encoder_2(num_inputs)
self.decoder = Decoder_2(num_inputs)
def forward(self, x):
code = self.encoder(x)
x = self.decoder(code)
return code, x
# autoencoder with hidden units 200, 20, 200
# Encoder
class Encoder_20(nn.Module):
def __init__(self, num_inputs):
super(Encoder_20, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(num_inputs, 200),
nn.ReLU(),
nn.Linear(200, 20))
self.encoder.apply(init_weights)
def forward(self, x):
x = self.encoder(x)
return x
# Decoder
class Decoder_20(nn.Module):
def __init__(self, num_inputs):
super(Decoder_20, self).__init__()
self.decoder = nn.Sequential(
nn.Linear(20, 200),
nn.ReLU(),
nn.Linear(200, num_inputs),
nn.ReLU())
self.decoder.apply(init_weights)
def forward(self, x):
x = self.decoder(x)
return x
# Autoencoder
class autoencoder_20(nn.Module):
def __init__(self, num_inputs):
super(autoencoder_20, self).__init__()
self.encoder = Encoder_20(num_inputs)
self.decoder = Decoder_20(num_inputs)
def forward(self, x):
code = self.encoder(x)
x = self.decoder(code)
return code, x