-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmlp.py
More file actions
31 lines (24 loc) · 974 Bytes
/
Copy pathmlp.py
File metadata and controls
31 lines (24 loc) · 974 Bytes
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
class MultiLayerPerceptron(torch.nn.Module):
def __init__(self,
input_dim:int,
output_dim:int,
middle_dim:[int],
stddev:float,
activation=None, # todo
norm=None): # todo
super().__init__()
input_dims = [input_dim, *middle_dim]
output_dims = [*middle_dim, output_dim]
layers = []
for input_dim, output_dim in zip(input_dims,output_dims):
layers.append(torch.nn.Linear(input_dim,output_dim))
layers.append(torch.nn.ReLU())
self.network = torch.nn.Sequential(*layers)
self.init_weight(stddev)
def init_weight(self,stddev):
for i in self.network:
if isinstance(i,torch.nn.Linear):
torch.nn.init.uniform_(i.weight, -stddev, stddev)
torch.nn.init.zeros_(i.bias)
def forward(self,x):
return self.network(x)