-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModClass.py
84 lines (64 loc) · 2.64 KB
/
ModClass.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
80
81
82
83
84
from ImportFile import *
pi = math.pi
class Swish(nn.Module):
def __init__(self, ):
super().__init__()
def forward(self, x):
return x * torch.sigmoid(x)
class Sin(nn.Module):
def __init__(self, ):
super().__init__()
def forward(self, x):
return torch.sin(x)
def activation(name):
if name in ['tanh', 'Tanh']:
return nn.Tanh()
elif name in ['relu', 'ReLU']:
return nn.ReLU(inplace=True)
elif name in ['lrelu', 'LReLU']:
return nn.LeakyReLU(inplace=True)
elif name in ['sigmoid', 'Sigmoid']:
return nn.Sigmoid()
elif name in ['softplus', 'Softplus']:
return nn.Softplus(beta=4)
elif name in ['celu', 'CeLU']:
return nn.CELU()
elif name in ['swish']:
return Swish()
elif name in ['sin']:
return Sin()
else:
raise ValueError('Unknown activation function')
class Pinns(nn.Module):
def __init__(self, input_dimension, output_dimension, network_properties):
super(Pinns, self).__init__()
self.input_dimension = input_dimension
self.output_dimension = output_dimension
self.n_hidden_layers = int(network_properties["hidden_layers"])
self.neurons = int(network_properties["neurons"])
self.lambda_residual = float(network_properties["residual_parameter"])
self.kernel_regularizer = int(network_properties["kernel_regularizer"])
self.regularization_param = float(network_properties["regularization_parameter"])
self.num_epochs = int(network_properties["epochs"])
self.act_string = str(network_properties["activation"])
self.optimizer = network_properties["optimizer"]
self.input_layer = nn.Linear(self.input_dimension, self.neurons)
self.hidden_layers = nn.ModuleList(
[nn.Linear(self.neurons, self.neurons) for _ in range(self.n_hidden_layers - 1)])
self.output_layer = nn.Linear(self.neurons, self.output_dimension)
self.activation = activation(self.act_string)
def forward(self, x):
x = self.activation(self.input_layer(x))
for k, l in enumerate(self.hidden_layers):
x = self.activation(l(x))
return self.output_layer(x)
def init_xavier(model):
def init_weights(m):
if type(m) == nn.Linear and m.weight.requires_grad and m.bias.requires_grad:
# gain = nn.init.calculate_gain('tanh')
gain = 1
torch.nn.init.xavier_uniform_(m.weight, gain=gain)
torch.nn.init.uniform_(m.bias, 0, 1)
# torch.nn.init.xavier_uniform_(m.bias)
# m.bias.data.fill_(0)
model.apply(init_weights)