-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSPNO_1d.py
210 lines (180 loc) · 7.37 KB
/
SPNO_1d.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.parameter import Parameter
import matplotlib.pyplot as plt
import operator
from functools import reduce
from functools import partial
from timeit import default_timer
from utilities3 import *
from Adam import Adam
torch.manual_seed(0)
np.random.seed(0)
################################################################
# 1d fourier layer
################################################################
class SpectralConv1d(nn.Module):
def __init__(self, in_channels, out_channels, modes1):
super(SpectralConv1d, self).__init__()
"""
1D Fourier layer. It does FFT, linear transform, and Inverse FFT.
"""
self.in_channels = in_channels
self.out_channels = out_channels
self.modes1 = modes1 #Number of Fourier modes to multiply, at most floor(N/2) + 1
self.scale = (1 / (in_channels*out_channels))
self.weights1 = nn.Parameter(self.scale * torch.rand(in_channels, out_channels, self.modes1, dtype=torch.cfloat))
# Complex multiplication
def compl_mul1d(self, input, weights):
# (batch, in_channel, x ), (in_channel, out_channel, x) -> (batch, out_channel, x)
return torch.einsum("bix,iox->box", input, weights)
def forward(self, x):
batchsize = x.shape[0]
#Compute Fourier coeffcients up to factor of e^(- something constant)
x_ft = torch.fft.rfft(x)
# Multiply relevant Fourier modes
out_ft = torch.zeros(batchsize, self.out_channels, x.size(-1)//2 + 1, device=x.device, dtype=torch.cfloat)
out_ft[:, :, :self.modes1] = self.compl_mul1d(x_ft[:, :, :self.modes1], self.weights1)
#Return to physical space
x = torch.fft.irfft(out_ft, n=x.size(-1))
return x
class SPNO1d(nn.Module):
def __init__(self, modes, width, eps, grid_dim, begin=0, end=1):
super(SPNO1d, self).__init__()
self.begin = begin
self.end = end
self.modes1 = modes
self.width = width
self.eps = eps
self.grid_dim = grid_dim
self.padding = 2 # pad the domain if input is non-periodic
# input channel is 2: (a(x), x)
self.fc0 = nn.Linear(2, self.width)
self.conv0 = SpectralConv1d(self.width, self.width, self.modes1)
self.conv1 = SpectralConv1d(self.width, self.width, self.modes1)
self.conv2 = SpectralConv1d(self.width, self.width, self.modes1)
self.conv3 = SpectralConv1d(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.w3 = nn.Conv1d(self.width, self.width, 1)
self.fc1 = nn.Linear(self.width, 128)
self.fc2 = nn.Linear(128, 1)
self.f1 = nn.Linear(self.grid_dim, 64)
self.f2 = nn.Linear(64, 1)
self.f3 = nn.Linear(self.grid_dim, 64)
self.f4 = nn.Linear(64, 1)
self.f5 = nn.Linear(self.grid_dim, 64)
self.f6 = nn.Linear(64, 1)
self.f7 = nn.Linear(self.grid_dim, 64)
self.f8 = nn.Linear(64, 1)
self.f9 = nn.Linear(self.grid_dim, 64)
self.f10 = nn.Linear(64, 1)
self.f11 = nn.Linear(self.grid_dim, 64)
self.f12 = nn.Linear(64, 1)
self.fi0 = nn.Linear(2, self.width)
self.fi1 = nn.Linear(2, self.width)
self.fi2 = nn.Linear(2, self.width)
self.coni0 = SpectralConv1d(self.width, self.width, self.modes1)
self.coni1 = SpectralConv1d(self.width, self.width, self.modes1)
self.coni2 = SpectralConv1d(self.width, self.width, self.modes1)
self.coni3 = SpectralConv1d(self.width, self.width, self.modes1)
self.coni4 = SpectralConv1d(self.width, self.width, self.modes1)
self.coni5 = SpectralConv1d(self.width, self.width, self.modes1)
self.wi0 = nn.Conv1d(self.width, self.width, 1)
self.wi1 = nn.Conv1d(self.width, self.width, 1)
self.wi2 = nn.Conv1d(self.width, self.width, 1)
self.wi3 = nn.Conv1d(self.width, self.width, 1)
self.wi4 = nn.Conv1d(self.width, self.width, 1)
self.wi5 = nn.Conv1d(self.width, self.width, 1)
self.fi3 = nn.Linear(self.width, 128)
self.fi4 = nn.Linear(128, 1)
self.fi5 = nn.Linear(self.width, 128)
self.fi6 = nn.Linear(128, 1)
self.fi7 = nn.Linear(self.width, 128)
self.fi8 = nn.Linear(128, 1)
def forward(self, x):
grid = self.get_grid(x.shape, x.device)
x_n = torch.reshape(x,(x.shape[0],x.shape[1]))
c1 = self.f1(x_n)
c1 = F.gelu(c1)
c1 = self.f2(c1)
c2 = self.f3(x_n)
c2 = F.gelu(c2)
c2 = self.f4(c2)
c3 = self.f5(x_n)
c3 = F.gelu(c3)
c3 = self.f6(c3)
c4 = self.f7(x_n)
c4 = F.gelu(c4)
c4 = self.f8(c4)
c5 = self.f9(x_n)
c5 = F.gelu(c5)
c5 = self.f10(c5)
c6 = self.f11(x_n)
c6 = F.gelu(c6)
c6 = self.f12(c6)
x_1 = torch.cat((x, -(grid-0)/self.eps), dim=-1)
x_1 = self.fi0(x_1)
x_1 = x_1.permute(0, 2, 1)
x_1 = self.coni0(x_1) + self.wi0(x_1)
x_1 = F.gelu(x_1)
x_1 = self.coni1(x_1) + self.wi1(x_1)
x_1 = x_1.permute(0, 2, 1)
x_1 = self.fi3(x_1)
x_1 = F.gelu(x_1)
x_1 = self.fi4(x_1)
x_2 = torch.cat((x, (grid-1)/self.eps), dim=-1)
x_2 = self.fi1(x_2)
x_2 = x_2.permute(0, 2, 1)
x_2 = self.coni2(x_2) + self.wi2(x_2)
x_2 = F.gelu(x_2)
x_2 = self.coni3(x_2) + self.wi3(x_2)
x_2 = x_2.permute(0, 2, 1)
x_2 = self.fi5(x_2)
x_2 = F.gelu(x_2)
x_2 = self.fi6(x_2)
x_3 = torch.cat((x, grid), dim=-1)
x_3 = self.fi2(x_3)
x_3 = x_3.permute(0, 2, 1)
x_3 = self.coni4(x_3) + self.wi4(x_3)
x_3 = F.gelu(x_3)
x_3 = self.coni5(x_3) + self.wi5(x_3)
x_3 = x_3.permute(0, 2, 1)
x_3 = self.fi7(x_3)
x_3 = F.gelu(x_3)
x_3 = self.fi8(x_3)
x = torch.cat((x, grid), dim=-1)
x = self.fc0(x)
x = x.permute(0, 2, 1)
# x = F.pad(x, [0,self.padding]) # pad the domain if input is non-periodic
x1 = self.conv0(x)
x2 = self.w0(x)
x = x1 + x2
x = F.gelu(x)
x1 = self.conv1(x)
x2 = self.w1(x)
x = x1 + x2
x = F.gelu(x)
x1 = self.conv2(x)
x2 = self.w2(x)
x = x1 + x2
x = F.gelu(x)
x1 = self.conv3(x)
x2 = self.w3(x)
x = x1 + x2
# x = x[..., :-self.padding] # pad the domain if input is non-periodic
x = x.permute(0, 2, 1)
x = self.fc1(x)
x = F.gelu(x)
x = self.fc2(x)
return x + self.mul(torch.exp(x_1),c1) + self.mul(torch.exp(x_2),c2) + (self.mul(torch.cos(x_1),c3)+self.mul(torch.sin(x_1),c4)+self.mul(torch.cos(x_2),c5)+self.mul(torch.sin(x_2),c6))*torch.exp(x_3)
def get_grid(self, shape, device):
batchsize, size_x = shape[0], shape[1]
gridx = torch.tensor(np.linspace(self.begin, self.end, size_x), dtype=torch.float)
gridx = gridx.reshape(1, size_x, 1).repeat([batchsize, 1, 1])
return gridx.to(device)
def mul(self, a, b):
return torch.einsum("bmi,bi->bmi", a, b)