-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathencoders.py
79 lines (62 loc) · 1.95 KB
/
encoders.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
from typing import List
import torch.nn as nn
import torch.nn.functional as F
from siren import SIREN
class TwoLayer256Relu(nn.Module):
def __init__(self, input_dim: int, with_bias=True):
super().__init__()
self._input_dim = input_dim
self.output_dim = 256
self.fc1 = nn.Linear(input_dim, 256, bias=with_bias)
self.fc2 = nn.Linear(256, self.output_dim, bias=with_bias)
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
return x
class MultiLayerRelu(nn.Sequential):
def __init__(
self,
input_dim,
hidden_dims: List[int],
with_batchnorm=False,
with_bias=True,
):
super().__init__()
self._input_dim = input_dim
self._hidden_dims = hidden_dims
self.output_dim = hidden_dims[-1]
prev_dim = input_dim
for layer_idx, dim in enumerate(hidden_dims):
self.add_module(
f"{layer_idx}_linear",
nn.Linear(
prev_dim,
dim,
bias=with_bias,
),
)
if with_batchnorm:
self.add_module(f"{layer_idx}_batch_norm", nn.BatchNorm1d(dim))
self.add_module(f"{layer_idx}_relu", nn.ReLU())
prev_dim = dim
def forward(self, x):
return super().forward(x)
class Siren(nn.Sequential):
def __init__(
self,
input_dim,
hidden_dims: List[int],
with_batchnorm=False,
with_bias=True,
):
super().__init__()
self._input_dim = input_dim
self._hidden_dims = hidden_dims
self.output_dim = hidden_dims[-1]
self.add_module('SIREN', SIREN(
self._hidden_dims[:-1],
self._input_dim,
self.output_dim,
))
def forward(self, x):
return super().forward(x)