-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathTrainCNO.py
More file actions
227 lines (180 loc) · 9.42 KB
/
TrainCNO.py
File metadata and controls
227 lines (180 loc) · 9.42 KB
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
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.CNOBenchmarks import Darcy, Airfoil, DiscContTranslation, ContTranslation, AllenCahn, SinFrequency, WaveEquation, ShearLayer
if len(sys.argv) == 2:
training_properties = {
"learning_rate": 0.001,
"weight_decay": 1e-6,
"scheduler_step": 10,
"scheduler_gamma": 0.98,
"epochs": 1000,
"batch_size": 16,
"exp": 1, # Do we use L1 or L2 errors? Default: L1
"training_samples": 256 # How many training samples?
}
model_architecture_ = {
#Parameters to be chosen with model selection:
"N_layers": 3, # Number of (D) & (U) blocks
"channel_multiplier": 32, # Parameter d_e (how the number of channels changes)
"N_res": 4, # Number of (R) blocks in the middle networs.
"N_res_neck" : 6, # Number of (R) blocks in the BN
#Other parameters:
"in_size": 64, # Resolution of the computational grid
"retrain": 4, # Random seed
"kernel_size": 3, # Kernel size.
"FourierF": 0, # Number of Fourier Features in the input channels. Default is 0.
"activation": 'cno_lrelu',# cno_lrelu or cno_lrelu_torch or lrelu or
#Filter properties:
"cutoff_den": 2.0001, # Cutoff parameter.
"lrelu_upsampling": 2, # Coefficient N_{\sigma}. Default is 2.
"half_width_mult": 0.8, # Coefficient c_h. Default is 1
"filter_size": 6, # 2xfilter_size is the number of taps N_{tap}. Default is 6.
"radial_filter": 0, # Is the filter radially symmetric? Default is 0 - NO.
}
# "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/"+"CNO_"+which_example+"_1"
else:
# Do we use a script to run the code (for cluster):
folder = sys.argv[1]
training_properties = json.loads(sys.argv[2].replace("\'", "\""))
model_architecture_ = json.loads(sys.argv[3].replace("\'", "\""))
which_example = sys.argv[4]
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
writer = SummaryWriter(log_dir=folder) #usage of TensorBoard
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 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([model_architecture_]).T
df.to_csv(folder + '/net_architecture.txt', header=False, index=True, mode='w')
if which_example == "shear_layer":
example = ShearLayer(model_architecture_, device, batch_size, training_samples, size = 64)
elif which_example == "poisson":
example = SinFrequency(model_architecture_, device, batch_size, training_samples)
elif which_example == "wave_0_5":
example = WaveEquation(model_architecture_, device, batch_size, training_samples)
elif which_example == "allen":
example = AllenCahn(model_architecture_, device, batch_size, training_samples)
elif which_example == "cont_tran":
example = ContTranslation(model_architecture_, device, batch_size, training_samples)
elif which_example == "disc_tran":
example = DiscContTranslation(model_architecture_, device, batch_size, training_samples)
elif which_example == "airfoil":
model_architecture_["in_size"] = 128
example = Airfoil(model_architecture_, device, batch_size, training_samples)
elif which_example == "darcy":
example = Darcy(model_architecture_, device, batch_size, training_samples)
else:
raise ValueError()
#-----------------------------------Train--------------------------------------
model = example.model
n_params = model.print_size()
train_loader = example.train_loader #TRAIN LOADER
val_loader = example.val_loader #VALIDATION 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.L1Loss()
elif p == 2:
loss = torch.nn.MSELoss()
best_model_testing_error = 1000 #Save the model once it has less than 1000% relative L1 error
patience = int(0.2 * epochs) # Early stopping parameter
counter = 0
if str(device) == 'cpu':
print("------------------------------------------")
print("YOU ARE RUNNING THE CODE ON A CPU.")
print("WE SUGGEST YOU TO RUN THE CODE ON A GPU!")
print("------------------------------------------")
print(" ")
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": #Mask the airfoil shape
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(val_loader):
input_batch = input_batch.to(device)
output_batch = output_batch.to(device)
output_pred_batch = model(input_batch)
if which_example == "airfoil": #Mask the airfoil shape
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(val_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": #Mask the airfoil shape
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()
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