-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.py
More file actions
96 lines (75 loc) · 2.87 KB
/
model.py
File metadata and controls
96 lines (75 loc) · 2.87 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import torch as torch
class NN(torch.nn.Module):
def __init__(self, n_in, n_hidden, hidden_layers):
super().__init__()
self.layers = torch.nn.ModuleList()
for _ in range(hidden_layers):
self.layers.append(torch.nn.Linear(n_in, n_hidden))
self.layers.append(torch.nn.ReLU())
n_in = n_hidden
# delete last ReLU
#self.layers = self.layers[:-1]
self.layers.append(torch.nn.Linear(n_hidden, 1))
def forward(self, x):
for layer in self.layers:
x = layer(x)
return x
class NN_(torch.nn.Module):
def __init__(self, n_in, n_out, n_hidden, hidden_layers, activation=torch.nn.ReLU()):
super().__init__()
self.layers = torch.nn.ModuleList()
for _ in range(hidden_layers):
self.layers.append(torch.nn.Linear(n_in, n_hidden))
self.layers.append(activation)
n_in = n_hidden
# delete last ReLU
#self.layers = self.layers[:-1]
self.layers.append(torch.nn.Linear(n_hidden, n_out))
def forward(self, x):
for layer in self.layers:
x = layer(x)
return x
class NN_norm(torch.nn.Module):
def __init__(self, n_in, n_hidden, hidden_layers, activation=torch.nn.ReLU):
super().__init__()
# Define the normalization layer
self.normalize = torch.nn.BatchNorm1d(n_in)
self.layers = torch.nn.ModuleList()
for _ in range(hidden_layers):
self.layers.append(torch.nn.Linear(n_in, n_hidden))
self.layers.append(activation)
n_in = n_hidden
self.layers.append(torch.nn.Linear(n_hidden, 1))
def forward(self, x):
# Apply input normalization
x = self.normalize(x)
for layer in self.layers:
x = layer(x)
return x
# create class for NN with outputdim = 1
class NN1(torch.nn.Module):
def __init__(self, n_in, n_hidden, hidden_layers):
super().__init__()
self.layers = torch.nn.ModuleList()
for _ in range(hidden_layers):
self.layers.append(torch.nn.Linear(n_in, n_hidden))
self.layers.append(torch.nn.ReLU())
n_in = n_hidden
# delete last ReLU
self.layers = self.layers[:-1]
self.layers.append(torch.nn.Linear(n_hidden, 1))
def forward(self, x):
for layer in self.layers:
x = layer(x)
return x
class derivative_NN(torch.nn.Module):
def __init__(self, n_in, n_hidden, hidden_layers):
super().__init__()
self.layers = torch.nn.ModuleList()
for _ in range(hidden_layers):
self.layers.append(torch.nn.Linear(n_in, n_hidden))
self.layers.append(torch.nn.ReLU())
n_in = n_hidden
# delete last ReLU
self.layers = self.layers[:-1]
self.layers.append(torch.nn.Linear(n_hidden, 1))