-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
45 lines (36 loc) · 1.33 KB
/
model.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
import torch
import torch.nn as nn
class LinearBlock(nn.Module):
def __init__(self, in_nodes, out_nodes):
super(LinearBlock, self).__init__()
self.layer = nn.utils.weight_norm(nn.Linear(in_nodes, out_nodes), dim = 0)
def forward(self, x):
x = self.layer(x)
x = torch.tanh(x)
return x
class PINN(nn.Module):
def __init__(self, layer_list):
super(PINN, self).__init__()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.input_layer = nn.utils.weight_norm(nn.Linear(layer_list[0], layer_list[1]), dim = 0)
self.hidden_layers = self._make_layer(layer_list[1:-1])
self.output_layer = nn.Linear(layer_list[-2], layer_list[-1])
def _make_layer(self, layer_list):
layers = []
for i in range(len(layer_list) - 1):
block = LinearBlock(layer_list[i], layer_list[i + 1])
layers.append(block)
return nn.Sequential(*layers)
def forward(self, x):
x = self.input_layer(x)
x = torch.tanh(x)
x = self.hidden_layers(x)
x = self.output_layer(x)
return x
def weights_init(m):
if isinstance(m, nn.Linear):
torch.nn.init.xavier_normal_(m.weight)
def pinn(layer_list):
model = PINN(layer_list)
model.apply(weights_init)
return model