-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodels.py
135 lines (119 loc) · 5.13 KB
/
models.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import torch
import torch.nn as nn
import param
### Time-dependent models ###
class FNNt(nn.Module):
'''
Feedforward neural network for the coefficient d(t, x)
Input: time and position
Output: coefficient
The input can be an array of (t x), i.e., (dim+1, n_data) tensor.
Then, the output becomes an array of (d), i.e., (dim, n_data) tensor.
'''
def __init__(self):
super(FNNt, self).__init__()
self.n_layer = param.n_layer
tmp = nn.Sequential()
tmp.add_module("fc", nn.Linear(param.dim+1, param.n_hidden))
tmp.add_module("relu", nn.ReLU(inplace=True))
setattr(self, "layer1", tmp)
for i in range(param.n_layer-1):
tmp = nn.Sequential()
tmp.add_module("fc", nn.Linear(param.n_hidden, param.n_hidden))
tmp.add_module("relu", nn.ReLU(inplace=True))
setattr(self, "layer%d" % (i+2), tmp)
self.out = nn.Linear(param.n_hidden, param.dim)
def forward(self, s, correct=1):
'''
argument s = (t x)
correct is for cancelling the constant factor when the short-time TUR is used
'''
for i in range(self.n_layer):
f = getattr(self, "layer%d" % (i+1))
s = f(s)
return self.out(s) / correct
class FNNKt(nn.Module):
'''
Feedforward neural network with kernel function for the coefficient d(t, x)
Input: time and position
Output: coefficient
The input can be an array of (t x), i.e., (dim+1, n_data) tensor.
Then, the output becomes an array of (d), i.e., (dim, n_data) tensor.
'''
def __init__(self):
super(FNNKt, self).__init__()
self.n_layer = param.n_layer
self.n_output = param.n_output
self.dim = param.dim
tmp = nn.Sequential()
tmp.add_module("fc", nn.Linear(param.dim, param.n_hidden))
tmp.add_module("relu", nn.ReLU(inplace=True))
setattr(self, "layer1", tmp)
for i in range(param.n_layer-1):
tmp = nn.Sequential()
tmp.add_module("fc", nn.Linear(param.n_hidden, param.n_hidden))
tmp.add_module("relu", nn.ReLU(inplace=True))
setattr(self, "layer%d" % (i+2), tmp)
self.out = nn.Linear(param.n_hidden, param.dim * param.n_output)
self.out_func_center = nn.Parameter(torch.linspace(param.t_init, param.t_fin, param.n_output).to(param.device))
self.out_func_width = nn.Parameter((torch.ones(param.n_output) * (param.t_fin - param.t_init)/param.n_output).to(param.device))
def forward(self, s, correct=1):
'''
argument s = (t x)
correct is for cancelling the constant factor when the short-time TUR is used
'''
if s.ndim == 3:
t = s[:, 0, 0]
x = s[:, :, 1:]
for i in range(self.n_layer):
f = getattr(self, "layer%d" % (i+1))
x = f(x)
return torch.einsum('ijkl,il->ijk', self.out(x).reshape(len(s), -1, self.dim, self.n_output),
torch.exp(-torch.pow((t.reshape(-1,1)-self.out_func_center.reshape(1,-1))/self.out_func_width.reshape(1,-1), 2))) / correct
elif s.ndim == 2:
t = s[0, 0]
x = s[:, 1:]
for i in range(self.n_layer):
f = getattr(self, "layer%d" % (i+1))
x = f(x)
return torch.matmul(self.out(x).reshape(-1, self.dim, self.n_output),
torch.exp(-torch.pow((t-self.out_func_center)/self.out_func_width, 2))) / correct
elif s.ndim == 1:
t = s[0]
x = s[1:]
for i in range(self.n_layer):
f = getattr(self, "layer%d" % (i+1))
x = f(x)
return torch.matmul(self.out(x).reshape(self.dim, self.n_output),
torch.exp(-torch.pow((t-self.out_func_center)/self.out_func_width, 2))) / correct
### Time-independent models ###
class FNN(nn.Module):
'''
Feedforward neural network for the coefficient d(x)
Input: position
Output: coefficient
The input can be an array of (x), i.e., (dim, n_data) tensor.
Then, the output becomes an array of (d), i.e., (dim, n_data) tensor.
'''
def __init__(self):
super(FNN, self).__init__()
self.n_layer = param.n_layer
tmp = nn.Sequential()
tmp.add_module("fc", nn.Linear(param.dim, param.n_hidden))
tmp.add_module("relu", nn.ReLU(inplace=True))
setattr(self, "layer1", tmp)
for i in range(param.n_layer-1):
tmp = nn.Sequential()
tmp.add_module("fc", nn.Linear(param.n_hidden, param.n_hidden))
tmp.add_module("relu", nn.ReLU(inplace=True))
setattr(self, "layer%d" % (i+2), tmp)
self.out = nn.Linear(param.n_hidden, param.dim)
def forward(self, s, correct=1):
'''
argument s = (x)
correct is for cancelling the constant factor when the short-time TUR is used
'''
for i in range(self.n_layer):
f = getattr(self, "layer%d" % (i+1))
s = f(s)
return self.out(s) / correct