-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathTrainFNO.py
201 lines (165 loc) · 7.87 KB
/
TrainFNO.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
import copy
import json
import os
import sys
import pandas as pd
import torch
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
from Problems.FNOBenchmarks import Darcy, Airfoil, DiscContTranslation, ContTranslation, AllenCahn, SinFrequency, WaveEquation, ShearLayer
if len(sys.argv) == 2:
training_properties = {
"learning_rate": 0.001,
"weight_decay": 1e-8,
"scheduler_step": 10,
"scheduler_gamma": 0.97,
"epochs": 1000,
"batch_size": 16,
"exp": 1,
"training_samples": 256,
}
fno_architecture_ = {
"width": 64,
"modes": 16,
"FourierF" : 0, #Number of Fourier Features in the input channels. Default is 0.
"n_layers": 4, #Number of Fourier layers
"padding": 0,
"include_grid":1,
"retrain": 4, #Random seed
}
# "which_example" can be
# poisson : Poisson equation
# wave_0_5 : Wave equation
# cont_tran : Smooth Transport
# disc_tran : Discontinuous Transport
# allen : Allen-Cahn equation
# shear_layer : Navier-Stokes equations
# airfoil : Compressible Euler equations
# darcy : Darcy Flow
which_example = sys.argv[1]
#which_example = "shear_layer"
# Save the models here:
folder = "TrainedModels/"+"FNO_"+which_example+"_tmp1"
else:
folder = sys.argv[1]
training_properties = json.loads(sys.argv[2].replace("\'", "\""))
fno_architecture_ = json.loads(sys.argv[3].replace("\'", "\""))
which_example = sys.argv[4]
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#device = "cpu"
writer = SummaryWriter(log_dir=folder)
learning_rate = training_properties["learning_rate"]
epochs = training_properties["epochs"]
batch_size = training_properties["batch_size"]
weight_decay = training_properties["weight_decay"]
scheduler_step = training_properties["scheduler_step"]
scheduler_gamma = training_properties["scheduler_gamma"]
training_samples = training_properties["training_samples"]
p = training_properties["exp"]
if which_example == "shear_layer":
example = ShearLayer(fno_architecture_, device, batch_size, training_samples)
elif which_example == "poisson":
example = SinFrequency(fno_architecture_, device, batch_size,training_samples)
elif which_example == "wave_0_5":
example = WaveEquation(fno_architecture_, device, batch_size,training_samples)
elif which_example == "allen":
example = AllenCahn(fno_architecture_, device, batch_size,training_samples)
elif which_example == "cont_tran":
example = ContTranslation(fno_architecture_, device, batch_size,training_samples)
elif which_example == "disc_tran":
example = DiscContTranslation(fno_architecture_, device, batch_size,training_samples)
elif which_example == "airfoil":
example = Airfoil(fno_architecture_, device, batch_size, training_samples)
elif which_example == "shear_layer_smooth":
example = ShearLayerSmooth(fno_architecture_, device, batch_size, training_samples)
elif which_example == "darcy":
example = Darcy(fno_architecture_, device, batch_size,training_samples)
else:
raise ValueError("the variable which_example has to be one between darcy")
if not os.path.isdir(folder):
print("Generated new folder")
os.mkdir(folder)
df = pd.DataFrame.from_dict([training_properties]).T
df.to_csv(folder + '/training_properties.txt', header=False, index=True, mode='w')
df = pd.DataFrame.from_dict([fno_architecture_]).T
df.to_csv(folder + '/net_architecture.txt', header=False, index=True, mode='w')
model = example.model
n_params = model.print_size()
train_loader = example.train_loader
test_loader = example.val_loader
optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate, weight_decay=weight_decay)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=scheduler_step, gamma=scheduler_gamma)
freq_print = 1
if p == 1:
loss = torch.nn.SmoothL1Loss()
elif p == 2:
loss = torch.nn.MSELoss()
best_model_testing_error = 300
patience = int(0.25 * epochs)
counter = 0
for epoch in range(epochs):
with tqdm(unit="batch", disable=False) as tepoch:
model.train()
tepoch.set_description(f"Epoch {epoch}")
train_mse = 0.0
running_relative_train_mse = 0.0
for step, (input_batch, output_batch) in enumerate(train_loader):
optimizer.zero_grad()
input_batch = input_batch.to(device)
output_batch = output_batch.to(device)
output_pred_batch = model(input_batch)
if which_example == "airfoil":
output_pred_batch[input_batch==1] = 1
output_batch[input_batch==1] = 1
loss_f = loss(output_pred_batch, output_batch) / loss(torch.zeros_like(output_batch).to(device), output_batch)
loss_f.backward()
optimizer.step()
train_mse = train_mse * step / (step + 1) + loss_f.item() / (step + 1)
tepoch.set_postfix({'Batch': step + 1, 'Train loss (in progress)': train_mse})
writer.add_scalar("train_loss/train_loss", train_mse, epoch)
with torch.no_grad():
model.eval()
test_relative_l2 = 0.0
train_relative_l2 = 0.0
for step, (input_batch, output_batch) in enumerate(test_loader):
input_batch = input_batch.to(device)
output_batch = output_batch.to(device)
output_pred_batch = model(input_batch)
if which_example == "airfoil":
output_pred_batch[input_batch==1] = 1
output_batch[input_batch==1] = 1
loss_f = torch.mean(torch.sum(abs(output_pred_batch - output_batch), dim=(1,2)) / torch.sum(abs(output_batch), dim=(1,2))) * 100
test_relative_l2 += loss_f.item()
test_relative_l2 /= len(test_loader)
for step, (input_batch, output_batch) in enumerate(train_loader):
input_batch = input_batch.to(device)
output_batch = output_batch.to(device)
output_pred_batch = model(input_batch)
if which_example == "airfoil":
output_pred_batch[input_batch==1] = 1
output_batch[input_batch==1] = 1
loss_f = torch.mean(torch.sum(abs(output_pred_batch - output_batch), dim=(1,2)) / torch.sum(abs(output_batch), dim=(1,2))) * 100
train_relative_l2 += loss_f.item()
train_relative_l2 /= len(train_loader)
writer.add_scalar("train_loss/train_loss_rel", train_relative_l2, epoch)
writer.add_scalar("val_loss/val_loss", test_relative_l2, epoch)
if test_relative_l2 < best_model_testing_error:
best_model_testing_error = test_relative_l2
best_model = copy.deepcopy(model)
torch.save(best_model, folder + "/model.pkl")
writer.add_scalar("val_loss/Best Relative Testing Error", best_model_testing_error, epoch)
counter = 0
else:
counter +=1
tepoch.set_postfix({'Train loss': train_mse, "Relative Train": train_relative_l2, "Relative Val loss": test_relative_l2})
tepoch.close()
#print(epoch, "val_loss/val_loss", test_relative_l2, epoch)
with open(folder + '/errors.txt', 'w') as file:
file.write("Training Error: " + str(train_mse) + "\n")
file.write("Best Testing Error: " + str(best_model_testing_error) + "\n")
file.write("Current Epoch: " + str(epoch) + "\n")
file.write("Params: " + str(n_params) + "\n")
scheduler.step()
if counter>patience:
print("Early Stopping")
break