-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
188 lines (140 loc) · 6.43 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class Approximation_block(nn.Module):
def __init__ (self, in_channels, out_channels, modes):
super(Approximation_block, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.modes1 = modes
self.scale = (1 / (in_channels*out_channels))
self.weights1 = nn.Parameter(self.scale * torch.rand(in_channels, out_channels, self.modes1, dtype=torch.float))
def forward(self, x, LBO_MATRIX, LBO_INVERSE, label = 'True'):
################################################################
# Encode
################################################################
x = x = x.permute(0, 2, 1)
x = LBO_INVERSE @ x
x = x.permute(0, 2, 1)
################################################################
# Approximator
################################################################
x = torch.einsum("bix,iox->box", x[:, :], self.weights1)
################################################################
# Decode
################################################################
x = x @ LBO_MATRIX.T
return x
class NORM_net(nn.Module):
def __init__(self, modes, width, MATRIX_Output, INVERSE_Output, MATRIX_Input, INVERSE_Input):
super(NORM_net, self).__init__()
self.modes1 = modes
self.width = width
self.padding = 2 # pad the domain if input is non-periodic
self.fc0 = nn.Linear(2, self.width)
self.fc01 = nn.Linear(self.width, self.width)
self.LBO_Matri_input = MATRIX_Input
self.LBO_Inver_input = INVERSE_Input
self.LBO_Matri_output = MATRIX_Output
self.LBO_Inver_output = INVERSE_Output
self.conv_encode = Approximation_block(self.width, self.width, self.modes1)
self.conv1 = Approximation_block(self.width, self.width, self.modes1)
self.conv2 = Approximation_block(self.width, self.width, self.modes1)
self.conv3 = Approximation_block(self.width, self.width, self.modes1)
self.w0 = nn.Conv1d(self.width, self.width, 1)
self.w1 = nn.Conv1d(self.width, self.width, 1)
self.w2 = nn.Conv1d(self.width, self.width, 1)
self.fc1 = nn.Linear(self.width, 128)
self.fc2 = nn.Linear(128, 1)
def forward(self, x):
grid = self.get_grid(x.shape, x.device)
x = torch.cat((x, grid), dim=-1)
x = self.fc0(x)
x = F.gelu(x)
x = self.fc01(x)
x = x.permute(0, 2, 1)
x1 = self.conv_encode(x, self.LBO_Matri_input, self.LBO_Inver_input)
x2 = self.w0(x)
x = x1 + x2
x = F.gelu(x)
'''
from the Input manifold to the Output manifold
'''
x = self.conv1(x, self.LBO_Matri_output, self.LBO_Inver_input)
x = F.gelu(x)
x1 = self.conv2(x, self.LBO_Matri_output, self.LBO_Inver_output)
x2 = self.w1(x)
x = x1 + x2
x1 = self.conv3(x, self.LBO_Matri_output, self.LBO_Inver_output)
x2 = self.w2(x)
x = x1 + x2
x = x.permute(0, 2, 1)
x = self.fc1(x)
x = F.gelu(x)
x = self.fc2(x)
return x
def get_grid(self, shape, device):
batchsize, size_x = shape[0], shape[1]
gridx = torch.tensor(np.linspace(0, 1, size_x), dtype=torch.float)
gridx = gridx.reshape(1, size_x, 1).repeat([batchsize, 1, 1])
return gridx.to(device)
class NORM_net_rag(nn.Module):
def __init__(self, modes, width, MATRIX_Output, INVERSE_Output, MATRIX_Input, INVERSE_Input):
super(NORM_net_rag, self).__init__()
self.modes1 = modes
self.width = width
self.padding = 2 # pad the domain if input is non-periodic
self.fc0 = nn.Linear(2 + 2, self.width)
# self.fc0 = nn.Linear(2 + 1, self.width)
self.fc01 = nn.Linear(self.width, self.width)
self.LBO_Matri_input = MATRIX_Input
self.LBO_Inver_input = INVERSE_Input
self.LBO_Matri_output = MATRIX_Output
self.LBO_Inver_output = INVERSE_Output
self.conv_encode = Approximation_block(self.width, self.width, self.modes1)
self.conv1 = Approximation_block(self.width, self.width, self.modes1)
self.conv2 = Approximation_block(self.width, self.width, self.modes1)
self.conv3 = Approximation_block(self.width, self.width, self.modes1)
self.w0 = nn.Conv1d(self.width, self.width, 1)
self.w1 = nn.Conv1d(self.width, self.width, 1)
self.w2 = nn.Conv1d(self.width, self.width, 1)
self.fc1 = nn.Linear(self.width, 128)
self.fc2 = nn.Linear(128, 1)
def forward(self, x):
x, ref_x, ref_y, ref_score = x['x'], x['ref_x'], x['ref_y'], x['ref_score']
# Ref_score
ref_score = ref_score.reshape( -1, 1, 1 ) * torch.ones_like( x ) # B, N, 1
grid = self.get_grid(x.shape, x.device) # B, N, 1
x = torch.cat((x, ref_score, ref_x , grid), dim=-1) # B, N, 4
# x = torch.cat((x, ref_x , grid), dim=-1) # B, N, 3
x = self.fc0(x)
x = F.gelu(x)
x = self.fc01(x)
x = x.permute(0, 2, 1)
x1 = self.conv_encode(x, self.LBO_Matri_input, self.LBO_Inver_input)
x2 = self.w0(x)
x = x1 + x2
x = F.gelu(x)
'''
from the Input manifold to the Output manifold
'''
x = self.conv1(x, self.LBO_Matri_output, self.LBO_Inver_input)
x = F.gelu(x)
x1 = self.conv2(x, self.LBO_Matri_output, self.LBO_Inver_output)
x2 = self.w1(x)
x = x1 + x2
x1 = self.conv3(x, self.LBO_Matri_output, self.LBO_Inver_output)
x2 = self.w2(x)
x = x1 + x2
x = x.permute(0, 2, 1)
x = self.fc1(x)
x = F.gelu(x)
x = self.fc2(x)
return x + ref_y.reshape(x.shape)
def get_grid(self, shape, device):
batchsize, size_x = shape[0], shape[1]
gridx = torch.tensor(np.linspace(0, 1, size_x), dtype=torch.float)
gridx = gridx.reshape(1, size_x, 1).repeat([batchsize, 1, 1])
return gridx.to(device)