-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
116 lines (85 loc) · 4.13 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
from loss import MSEloss_with_Mask
def activation(input, type):
if type.lower()=='selu':
return F.selu(input)
elif type.lower()=='elu':
return F.elu(input)
elif type.lower()=='relu':
return F.relu(input)
elif type.lower()=='relu6':
return F.relu6(input)
elif type.lower()=='lrelu':
return F.leaky_relu(input)
elif type.lower()=='tanh':
return F.tanh(input)
elif type.lower()=='sigmoid':
return F.sigmoid(input)
elif type.lower()=='swish':
return F.sigmoid(input)*input
elif type.lower()=='identity':
return input
else:
raise ValueError("Unknown non-Linearity Type")
class AutoEncoder(nn.Module):
def __init__(self, layer_sizes, nl_type='selu', is_constrained=True, dp_drop_prob=0.0, last_layer_activations=True):
"""
layer_sizes = size of each layer in the autoencoder model
For example: [10000, 1024, 512] will result in:
- encoder 2 layers: 10000x1024 and 1024x512. Representation layer (z) will be 512
- decoder 2 layers: 512x1024 and 1024x10000.
nl_type = non-Linearity type (default: 'selu).
is_constrained = If true then the weights of encoder and decoder are tied.
dp_drop_prob = Dropout probability.
last_layer_activations = Whether to apply activation on last decoder layer.
"""
super(AutoEncoder, self).__init__()
self.layer_sizes = layer_sizes
self.nl_type = nl_type
self.is_constrained = is_constrained
self.dp_drop_prob = dp_drop_prob
self.last_layer_activations = last_layer_activations
if dp_drop_prob>0:
self.drop = nn.Dropout(dp_drop_prob)
self._last = len(layer_sizes) - 2
# Initaialize Weights
self.encoder_weights = nn.ParameterList( [nn.Parameter(torch.rand(layer_sizes[i+1], layer_sizes[i])) for i in range(len(layer_sizes) - 1) ] )
# "Xavier Initialization" ( Understanding the Difficulty in training deep feed forward neural networks - by Glorot, X. & Bengio, Y. )
# ( Values are sampled from uniform distribution )
for weights in self.encoder_weights:
init.xavier_uniform_(weights)
# Encoder Bias
self.encoder_bias = nn.ParameterList( [nn.Parameter(torch.zeros(layer_sizes[i+1])) for i in range(len(layer_sizes) - 1) ] )
reverse_layer_sizes = list(reversed(layer_sizes))
# reversed returns iterator
# Decoder Weights
if is_constrained == False:
self.decoder_weights = nn.ParameterList( [nn.Parameter(torch.rand(reverse_layer_sizes[i+1], reverse_layer_sizes[i])) for i in range(len(reverse_layer_sizes) - 1) ] )
for weights in self.decoder_weights:
init.xavier_uniform_(weights)
self.decoder_bias = nn.ParameterList( [nn.Parameter(torch.zeros(reverse_layer_sizes[i+1])) for i in range(len(reverse_layer_sizes) - 1) ] )
def encode(self,x):
for i,w in enumerate(self.encoder_weights):
x = F.linear(input=x, weight = w, bias = self.encoder_bias[i] )
x = activation(input=x, type=self.nl_type)
# Apply Dropout on the last layer
if self.dp_drop_prob > 0:
x = self.drop(x)
return x
def decode(self,x):
if self.is_constrained == True:
# Weights are tied
for i,w in zip(range(len(self.encoder_weights)),list(reversed(self.encoder_weights))):
x = F.linear(input=x, weight=w.t(), bias = self.decoder_bias[i] )
x = activation(input=x, type=self.nl_type if i != self._last or self.last_layer_activations else 'identity')
else:
for i,w in enumerate(self.decoder_weights):
x = F.linear(input=x, weight = w, bias = self.decoder_weights[i])
x = activation(input=x, type=self.nl_type if i != self._last or self.last_layer_activations else 'identity')
return x
def forward(self,x):
# Forward Pass
return self.decode(self.encode(x))