forked from neuraloperator/neuraloperator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lowrank_1d.py
219 lines (165 loc) · 6.37 KB
/
lowrank_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
211
212
213
214
215
216
217
218
219
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.parameter import Parameter
import numpy as np
import h5py
import scipy.io
import matplotlib.pyplot as plt
from timeit import default_timer
import sys
import math
import operator
from functools import reduce
from timeit import default_timer
from utilities3 import *
torch.manual_seed(0)
np.random.seed(0)
################################################################
# lowrank layer
################################################################
class LowRank1d(nn.Module):
def __init__(self, in_channels, out_channels, s, width, rank=1):
super(LowRank1d, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.s = s
self.n = s
self.rank = rank
self.phi = DenseNet([2, 64, 128, 256, width*width*rank], torch.nn.ReLU)
self.psi = DenseNet([2, 64, 128, 256, width*width*rank], torch.nn.ReLU)
def forward(self, v, a):
# a (batch, n, 2)
# v (batch, n, f)
batch_size = v.shape[0]
phi_eval = self.phi(a).reshape(batch_size, self.n, self.out_channels, self.in_channels, self.rank)
psi_eval = self.psi(a).reshape(batch_size, self.n, self.out_channels, self.in_channels, self.rank)
# print(psi_eval.shape, v.shape, phi_eval.shape)
v = torch.einsum('bnoir,bni,bmoir->bmo',psi_eval, v, phi_eval) / self.n
return v
class MyNet(torch.nn.Module):
def __init__(self, s, width=32, rank=4):
super(MyNet, self).__init__()
self.s = s
self.width = width
self.rank = rank
self.fc0 = nn.Linear(2, self.width)
self.net1 = LowRank1d(self.width, self.width, s, width, rank=self.rank)
self.net2 = LowRank1d(self.width, self.width, s, width, rank=self.rank)
self.net3 = LowRank1d(self.width, self.width, s, width, rank=self.rank)
self.net4 = LowRank1d(self.width, self.width, s, width, rank=self.rank)
self.w1 = nn.Linear(self.width, self.width)
self.w2 = nn.Linear(self.width, self.width)
self.w3 = nn.Linear(self.width, self.width)
self.w4 = nn.Linear(self.width, self.width)
self.bn1 = torch.nn.BatchNorm1d(self.width)
self.bn2 = torch.nn.BatchNorm1d(self.width)
self.bn3 = torch.nn.BatchNorm1d(self.width)
self.bn4 = torch.nn.BatchNorm1d(self.width)
self.fc1 = nn.Linear(self.width, 128)
self.fc2 = nn.Linear(128, 1)
def forward(self, v):
batch_size, n = v.shape[0], v.shape[1]
a = v.clone()
v = self.fc0(v)
v1 = self.net1(v, a)
v2 = self.w1(v)
v = v1+v2
v = self.bn1(v.reshape(-1, self.width)).view(batch_size,n,self.width)
v = F.relu(v)
v1 = self.net2(v, a)
v2 = self.w2(v)
v = v1+v2
v = self.bn2(v.reshape(-1, self.width)).view(batch_size,n,self.width)
v = F.relu(v)
v1 = self.net3(v, a)
v2 = self.w3(v)
v = v1+v2
v = self.bn3(v.reshape(-1, self.width)).view(batch_size,n,self.width)
v = F.relu(v)
v1 = self.net4(v, a)
v2 = self.w4(v)
v = v1+v2
v = self.bn4(v.reshape(-1, self.width)).view(batch_size,n,self.width)
v = self.fc1(v)
v = F.relu(v)
v = self.fc2(v)
return v.squeeze()
def count_params(self):
c = 0
for p in self.parameters():
c += reduce(operator.mul, list(p.size()))
return c
################################################################
# configs
################################################################
ntrain = 1000
ntest = 200
sub = 1 #subsampling rate
h = 2**13 // sub
s = h
batch_size = 5
learning_rate = 0.001
################################################################
# reading data and normalization
################################################################
dataloader = MatReader('data/burgers_data_R10.mat')
x_data = dataloader.read_field('a')[:,::sub]
y_data = dataloader.read_field('u')[:,::sub]
x_train = x_data[:ntrain,:]
y_train = y_data[:ntrain,:]
x_test = x_data[-ntest:,:]
y_test = y_data[-ntest:,:]
x_normalizer = UnitGaussianNormalizer(x_train)
x_train = x_normalizer.encode(x_train)
x_test = x_normalizer.encode(x_test)
y_normalizer = UnitGaussianNormalizer(y_train)
y_train = y_normalizer.encode(y_train)
grid = np.linspace(0, 2*np.pi, s).reshape(1, s, 1)
grid = torch.tensor(grid, dtype=torch.float)
x_train = torch.cat([x_train.reshape(ntrain,s,1), grid.repeat(ntrain,1,1)], dim=2)
x_test = torch.cat([x_test.reshape(ntest,s,1), grid.repeat(ntest,1,1)], dim=2)
train_loader = torch.utils.data.DataLoader(torch.utils.data.TensorDataset(x_train, y_train), batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(torch.utils.data.TensorDataset(x_test, y_test), batch_size=batch_size, shuffle=False)
model = MyNet(s).cuda()
print(model.count_params())
################################################################
# training and evaluation
################################################################
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate, weight_decay=1e-4)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.5)
epochs = 500
myloss = LpLoss(size_average=False)
y_normalizer.cuda()
for ep in range(epochs):
model.train()
t1 = default_timer()
train_mse = 0
train_l2 = 0
for x, y in train_loader:
x, y = x.cuda(), y.cuda()
optimizer.zero_grad()
out = model(x).reshape(batch_size, s)
mse = F.mse_loss(out.view(batch_size, -1), y.view(batch_size, -1), reduction='mean')
# mse.backward()
out = y_normalizer.decode(out)
y = y_normalizer.decode(y)
loss = myloss(out.view(batch_size,-1), y.view(batch_size,-1))
loss.backward()
optimizer.step()
train_mse += mse.item()
train_l2 += loss.item()
scheduler.step()
model.eval()
test_l2 = 0.0
with torch.no_grad():
for x, y in test_loader:
x, y = x.cuda(), y.cuda()
out = model(x).reshape(batch_size, s)
out = y_normalizer.decode(out)
test_l2 += myloss(out.view(batch_size, -1), y.view(batch_size, -1)).item()
train_mse /= len(train_loader)
train_l2 /= ntrain
test_l2 /= ntest
t2 = default_timer()
print(ep, t2-t1, train_mse, train_l2, test_l2)