-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wildfire.py
273 lines (218 loc) · 8.51 KB
/
Wildfire.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import torch
import torch.nn as nn
import torch.nn.functional as F
from numpy import meshgrid
import torch.optim as optim
from torch.utils.data import TensorDataset, DataLoader, Dataset
from torch.autograd import gradcheck
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import shift
from math import sqrt
from scipy.sparse import diags
from scipy.linalg import cholesky
import os
inpath = "./data/Wildfire/input/"
impath = "./data/Wildfire/mac/"
immpath = "./plots/Wildfire/mac/"
os.makedirs(impath, exist_ok=True)
os.makedirs(immpath, exist_ok=True)
seed = 10
class NuclearNormAutograd(torch.autograd.Function):
@staticmethod
def forward(ctx, input_matrix):
ctx.save_for_backward(input_matrix)
return torch.linalg.matrix_norm(input_matrix, ord="nuc")
@staticmethod
def backward(ctx, grad_output):
input_matrix, = ctx.saved_tensors
u, s, v = torch.svd(input_matrix, some=False)
rank = torch.sum(s > 0).item()
dtype = input_matrix.dtype
eye_approx = torch.diag((s > 0).to(dtype)[:rank])
grad_input = torch.matmul(torch.matmul(u[:, :rank], eye_approx), v[:, :rank].t())
return grad_input * grad_output.unsqueeze(-1).unsqueeze(-1)
class ShapeShiftNet(nn.Module):
def __init__(self):
super(ShapeShiftNet, self).__init__()
self.elu = nn.ELU()
# Subnetwork for f^1 and shift^1
self.f1_fc1 = nn.Linear(2, 5)
self.f1_fc2 = nn.Linear(5, 10)
self.f1_fc3 = nn.Linear(10, 5)
self.f1_fc4 = nn.Linear(5, 1)
self.shift1_fc1 = nn.Linear(1, 5)
self.shift1_fc2 = nn.Linear(5, 5)
self.shift1_fc3 = nn.Linear(5, 1)
# Subnetwork for f^2 and shift^2
self.f2_fc1 = nn.Linear(2, 5)
self.f2_fc2 = nn.Linear(5, 10)
self.f2_fc3 = nn.Linear(10, 5)
self.f2_fc4 = nn.Linear(5, 1)
self.shift2_fc1 = nn.Linear(1, 5)
self.shift2_fc2 = nn.Linear(5, 5)
self.shift2_fc3 = nn.Linear(5, 1)
# Subnetwork for f^3 and shift^3
self.f3_fc1 = nn.Linear(2, 5)
self.f3_fc2 = nn.Linear(5, 10)
self.f3_fc3 = nn.Linear(10, 5)
self.f3_fc4 = nn.Linear(5, 1)
self.shift3_fc1 = nn.Linear(1, 5)
self.shift3_fc2 = nn.Linear(5, 5)
self.shift3_fc3 = nn.Linear(5, 1)
def forward(self, x, t):
# Pathway for f^1 and shift^1
shift1 = self.elu(self.shift1_fc1(t))
shift1 = self.elu(self.shift1_fc2(shift1))
shift1 = self.shift1_fc3(shift1)
x_shifted1 = x + shift1
f1 = self.elu(self.f1_fc1(torch.cat((x_shifted1, t), dim=1)))
f1 = self.elu(self.f1_fc2(f1))
f1 = self.elu(self.f1_fc3(f1))
f1 = self.f1_fc4(f1)
f1_without_shift = self.elu(self.f1_fc1(torch.cat((x, t), dim=1)))
f1_without_shift = self.elu(self.f1_fc2(f1_without_shift))
f1_without_shift = self.elu(self.f1_fc3(f1_without_shift))
f1_without_shift = self.f1_fc4(f1_without_shift)
# Pathway for f^2 and shift^2
shift2 = self.elu(self.shift2_fc1(t))
shift2 = self.elu(self.shift2_fc2(shift2))
shift2 = self.shift2_fc3(shift2)
x_shifted2 = x + shift2
f2 = self.elu(self.f2_fc1(torch.cat((x_shifted2, t), dim=1)))
f2 = self.elu(self.f2_fc2(f2))
f2 = self.elu(self.f2_fc3(f2))
f2 = self.f2_fc4(f2)
f2_without_shift = self.elu(self.f2_fc1(torch.cat((x, t), dim=1)))
f2_without_shift = self.elu(self.f2_fc2(f2_without_shift))
f2_without_shift = self.elu(self.f2_fc3(f2_without_shift))
f2_without_shift = self.f2_fc4(f2_without_shift)
# Pathway for f^3 and shift^3
shift3 = self.elu(self.shift3_fc1(t))
shift3 = self.elu(self.shift3_fc2(shift3))
shift3 = self.shift3_fc3(shift3)
x_shifted3 = x + shift3
f3 = self.elu(self.f3_fc1(torch.cat((x_shifted3, t), dim=1)))
f3 = self.elu(self.f3_fc2(f3))
f3 = self.elu(self.f3_fc3(f3))
f3 = self.f3_fc4(f3)
f3_without_shift = self.elu(self.f3_fc1(torch.cat((x, t), dim=1)))
f3_without_shift = self.elu(self.f3_fc2(f3_without_shift))
f3_without_shift = self.elu(self.f3_fc3(f3_without_shift))
f3_without_shift = self.f3_fc4(f3_without_shift)
return f1, f2, f3, f1_without_shift, f2_without_shift, f3_without_shift, shift1, shift2, shift3
# Load the data
Q_wf = np.load(inpath + 'SnapShotMatrix558.49.npy', allow_pickle=True)
t = np.load(inpath + 'Time.npy', allow_pickle=True)
x_grid = np.load(inpath + '1D_Grid.npy', allow_pickle=True)
x = x_grid[0]
T = Q_wf[:len(x), :]
# Set the seed
np.random.seed(seed)
torch.manual_seed(seed)
Q = T/T.max()
Q_tensor = torch.tensor(Q)
Nx = len(x)
Nt = len(t)
xx, tt = np.meshgrid(x, t)
# Inputs to the network
inputs = np.stack([x.repeat(Nt), np.tile(t, Nx)], axis=1)
inputs_tensor = torch.tensor(inputs, dtype=torch.float32)
# Define the model
model = ShapeShiftNet()
pretrained_load = True
if pretrained_load:
state_dict_original = torch.load("./data/Crossing_waves/seed=54/Crossing_waves.pth")
state_dict_new = model.state_dict()
for name, param in state_dict_original.items():
if name in state_dict_new:
state_dict_new[name].copy_(param)
model.load_state_dict(state_dict_new, strict=False)
optimizer = optim.Adam(model.parameters(), lr=0.0001)
# Training loop
num_epochs = 10
lambda_k = 0.1
for epoch in range(num_epochs + 1):
x_NN, t_NN = inputs_tensor[:, 0:1], inputs_tensor[:, 1:2]
optimizer.zero_grad()
f1_full, f2_full, f3_full, f1_full_nos, f2_full_nos, f3_full_nos, s1, s2, s3 = model(x_NN, t_NN)
frobenius_loss = torch.norm(Q_tensor - f1_full.view(Nx, Nt) - f2_full.view(Nx, Nt) - f3_full.view(Nx, Nt), 'fro') ** 2
nuclear_loss_q1 = NuclearNormAutograd.apply(f1_full_nos.view(Nx, Nt))
nuclear_loss_q2 = NuclearNormAutograd.apply(f2_full_nos.view(Nx, Nt))
nuclear_loss_q3 = NuclearNormAutograd.apply(f3_full_nos.view(Nx, Nt))
nuclear_loss = lambda_k * (nuclear_loss_q1 + nuclear_loss_q2 + nuclear_loss_q3)
total_loss = nuclear_loss + frobenius_loss
total_loss.backward(retain_graph=True)
optimizer.step()
if frobenius_loss < 1.0:
print("Early stopping is triggered")
break
if epoch % 100 == 0:
print(
f'Epoch {epoch}/{num_epochs}, Frob Loss: {frobenius_loss.item()}, Nuclear Loss: {nuclear_loss.item()}, Total loss: {total_loss.item()},')
combined = f1_full + f2_full + f3_full
Q_tilde = combined.view(Nx, Nt).detach().numpy()
# Save the weights
torch.save(model.state_dict(), impath + 'Wildfire.pth')
# Plot the results
fig, axs = plt.subplots(1, 8, figsize=(20, 4))
vmin = np.min(Q)
vmax = np.max(Q)
# Q
axs[0].pcolormesh(xx.T, tt.T, Q, vmin=vmin, vmax=vmax)
axs[0].set_title(r"$\mathbf{Q}$")
axs[0].set_xlabel("t")
axs[0].set_ylabel("x")
axs[0].set_xticks([])
axs[0].set_yticks([])
# Qtilde
axs[1].pcolormesh(xx.T, tt.T, Q_tilde, vmin=vmin, vmax=vmax)
axs[1].set_title(r"$\mathbf{\tilde{Q}}$")
axs[1].set_xlabel("t")
axs[1].set_ylabel("x")
axs[1].set_xticks([])
axs[1].set_yticks([])
# f^1
axs[2].pcolormesh(xx.T, tt.T, f1_full.view(Nx, Nt).detach().numpy(), vmin=vmin, vmax=vmax)
axs[2].set_title(r"$\mathcal{T}^1\mathbf{Q}^1$")
axs[2].set_xlabel("t")
axs[2].set_ylabel("x")
axs[2].set_xticks([])
axs[2].set_yticks([])
# f^3
axs[3].pcolormesh(xx.T, tt.T, f3_full.view(Nx,Nt).detach().numpy(), vmin=vmin, vmax=vmax)
axs[3].set_title(r"$\mathcal{T}^2\mathbf{Q}^2$")
axs[3].set_xlabel("t")
axs[3].set_ylabel("x")
axs[3].set_xticks([])
axs[3].set_yticks([])
# f^2
axs[4].pcolormesh(xx.T, tt.T, f2_full.view(Nx, Nt).detach().numpy(), vmin=vmin, vmax=vmax)
axs[4].set_title(r"$\mathcal{T}^3\mathbf{Q}^3$")
axs[4].set_xlabel("t")
axs[4].set_ylabel("x")
axs[4].set_xticks([])
axs[4].set_yticks([])
# f^1
axs[5].pcolormesh(xx.T, tt.T, f1_full_nos.view(Nx,Nt).detach().numpy(), vmin=vmin, vmax=vmax)
axs[5].set_title(r"$\mathbf{Q}^1$")
axs[5].set_xlabel("t")
axs[5].set_ylabel("x")
axs[5].set_xticks([])
axs[5].set_yticks([])
# f^3
axs[6].pcolormesh(xx.T, tt.T, f3_full_nos.view(Nx,Nt).detach().numpy(), vmin=vmin, vmax=vmax)
axs[6].set_title(r"$\mathbf{Q}^2$")
axs[6].set_xlabel("t")
axs[6].set_ylabel("x")
axs[6].set_xticks([])
axs[6].set_yticks([])
# f^2
cax4 = axs[7].pcolormesh(xx.T, tt.T, f2_full_nos.view(Nx,Nt).detach().numpy(), vmin=vmin, vmax=vmax)
axs[7].set_title(r"$\mathbf{Q}^3$")
axs[7].set_xlabel("t")
axs[7].set_ylabel("x")
axs[7].set_xticks([])
axs[7].set_yticks([])
plt.colorbar(cax4, ax=axs.ravel().tolist(), orientation='vertical')
fig.savefig(immpath + "Wildfire_NN", dpi=300, transparent=True)