-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnets.py
More file actions
63 lines (53 loc) · 1.71 KB
/
Copy pathnets.py
File metadata and controls
63 lines (53 loc) · 1.71 KB
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
"""
Authors: Original MEow authors (reformatted by us)
"""
import torch
from torch import nn
class MLP(nn.Module):
'''
A multilayer perceptron with Swish ReLU nonlinearities
'''
def __init__(
self,
layers,
dropout_rate=0.0,
init=False,
layernorm=False
):
super().__init__()
net = nn.ModuleList([])
for k in range(len(layers) - 2):
net.append(nn.Linear(layers[k], layers[k + 1]))
# Set Initial values
if init == 'zero':
nn.init.zeros_(net[-1].weight)
elif init == 'orthogonal':
nn.init.orthogonal_(net[-1].weight)
else:
NotImplementedError('This output function is not implemented.')
if layernorm:
net.append(nn.LayerNorm(layers[k + 1]))
net.append(Swish(dim=layers[k + 1]))
if dropout_rate > 0.0:
net.append(nn.Dropout(p=dropout_rate))
net.append(nn.Linear(layers[-2], layers[-1]))
# Set Initial values
if init == 'zero':
nn.init.zeros_(net[-1].weight)
elif init == 'orthogonal':
nn.init.orthogonal_(net[-1].weight)
else:
NotImplementedError('This output function is not implemented.')
# Construct the model
self.net = nn.Sequential(*net)
def forward(self, x):
return self.net(x)
class Swish(nn.Module):
def __init__(self, dim=-1):
'''
Swish from: https://github.com/wgrathwohl/LSD/blob/master/networks.py#L299
'''
super().__init__()
self.beta = nn.Parameter(torch.ones((dim,)))
def forward(self, x):
return x * torch.sigmoid(self.beta[None, :] * x)