-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_networks.py
55 lines (41 loc) · 2.09 KB
/
encoder_networks.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
import torch.nn as nn
######################################################
######################################################
################## FNN #####################
######################################################
######################################################
class FNNEncoder(nn.Module):
def __init__(self, input_size, hidden_size, output_size, num_layers, dropout_rate, nonlinear=True):
super(FNNEncoder, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.num_layers = num_layers
self.dropout_rate = dropout_rate
self.nonlinear = nonlinear
print('DAN: input {}, hidden {}, output {}'.format(self.input_size, self.hidden_size, self.output_size))
# first hidden layers
if self.nonlinear:
self.hidden = nn.ModuleList([nn.Linear(in_features=self.input_size, out_features=self.hidden_size),
nn.ReLU(),
nn.Dropout(self.dropout_rate)])
else:
self.hidden = nn.ModuleList([nn.Linear(in_features=self.input_size, out_features=self.hidden_size),
nn.Dropout(self.dropout_rate)])
# optional deep layers
for k in range(1, self.num_layers):
if self.nonlinear:
self.hidden.extend([nn.Linear(in_features=self.hidden_size, out_features=self.hidden_size),
nn.ReLU(),
nn.Dropout(self.dropout_rate)])
else:
self.hidden.extend([nn.Linear(in_features=self.hidden_size, out_features=self.hidden_size),
nn.Dropout(self.dropout_rate)])
# output linear function (readout)
self.final = nn.Linear(in_features=self.hidden_size, out_features=self.output_size)
def forward(self, x):
y = x
for i in range(len(self.hidden)):
y = self.hidden[i](y)
out = self.final(y)
return out