-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers.py
38 lines (33 loc) · 1.24 KB
/
layers.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions.normal import Normal
'''MLP model'''
class MLP(nn.Module):
def __init__(self, input_dim, output_dim, hidden_size=(1024, 512), activation='relu', discrim=False, dropout=-1):
super(MLP, self).__init__()
dims = []
dims.append(input_dim)
dims.extend(hidden_size)
dims.append(output_dim)
self.layers = nn.ModuleList()
for i in range(len(dims)-1):
self.layers.append(nn.Linear(dims[i], dims[i+1]))
if activation == 'relu':
self.activation = nn.ReLU()
elif activation == 'sigmoid':
self.activation = nn.Sigmoid()
self.sigmoid = nn.Sigmoid() if discrim else None
self.dropout = dropout
def forward(self, x):
for i in range(len(self.layers)):
x = self.layers[i](x)
if i != len(self.layers)-1:
x = self.activation(x)
if self.dropout != -1:
x = nn.Dropout(min(0.1, self.dropout/3) if i == 1 else self.dropout)(x)
elif self.sigmoid:
x = self.sigmoid(x)
return x
if __name__ == "__main__":
print("Running the layers.py file now;")