-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_real_experiments_GP.py
More file actions
426 lines (340 loc) · 17.8 KB
/
Copy pathrun_real_experiments_GP.py
File metadata and controls
426 lines (340 loc) · 17.8 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# REAL DATA EXPERIMENTS
# RUN WITH python run_real_experiments_GP.py
# _ _ _
# | | | | (_)
# __ _ _ __ | |_ __ _ _ __ ___| |_ _ ___
# / _` | '_ \| __/ _` | '__/ __| __| |/ __|
# | (_| | | | | || (_| | | | (__| |_| | (__
# \__,_|_| |_|\__\__,_|_| \___|\__|_|\___|
#
model_name = "GP"
from gpytorch_models import GP
# import configs to we can access the hypers with getattr
import configs
from configs import PATIENCE, MAX_NUM_EPOCHS, NUM_RUNS, PRINT_FREQUENCY
from configs import TRACK_EMISSIONS_BOOL
# We overwrite this parameter's initialisation with REAL data noise range
from configs import REAL_NOISE_VAR_RANGE, OUTPUTSCALE_VAR_RANGE, TASK_COVAR_FACTOR_RANGE
# Reiterating import for visibility
MAX_NUM_EPOCHS = MAX_NUM_EPOCHS
NUM_RUNS = NUM_RUNS
PATIENCE = PATIENCE
# assign model-specific variable
MODEL_LEARNING_RATE = getattr(configs, f"{model_name}_REAL_LEARNING_RATE")
MODEL_REAL_RESULTS_DIR = getattr(configs, f"{model_name}_REAL_RESULTS_DIR")
import os
os.makedirs(MODEL_REAL_RESULTS_DIR, exist_ok = True)
# basics
import pandas as pd
import torch
import gpytorch
# universals
from metrics import compute_divergence_field, quantile_coverage_error_2d
from utils import set_seed
import gc
import warnings
set_seed(42)
# setting device to GPU if available, else CPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# overwrite if needed: # device = 'cpu'
print('Using device:', device)
print()
### START TIMING ###
import time
start_time = time.time() # Start timing after imports
### START TRACKING EXPERIMENT EMISSIONS ###
if TRACK_EMISSIONS_BOOL:
from codecarbon import EmissionsTracker
tracker = EmissionsTracker(project_name = "GP_real_experiments", output_dir = MODEL_REAL_RESULTS_DIR)
tracker.start()
#############################
### LOOP 1 - over REGIONS ###
#############################
for region_name in ["region_lower_byrd", "region_mid_byrd", "region_upper_byrd"]:
print(f"\nTraining for {region_name.upper()}...")
# Store metrics for the current region (used for *metrics_summary* report and *metrics_per_run*)
region_results = []
##########################################
### x_train & y_train, x_test & x_test ###
##########################################
# define paths based on region_name
path_to_training_tensor = "data/real_data/" + region_name + "_train_tensor.pt"
path_to_test_tensor = "data/real_data/" + region_name + "_test_tensor.pt"
# load and tranpose to have rows as points
train = torch.load(path_to_training_tensor, weights_only = False).T
test = torch.load(path_to_test_tensor, weights_only = False).T
# The train and test tensors have the following columns:
# [:, 0] = x
# [:, 1] = y
# [:, 2] = surface elevation (s) (not used currently)
# [:, 3] = ice flux in x direction (u)
# [:, 4] = ice flux in y direction (v)
# train
x_train = train[:, [0, 1]].to(device)
y_train = train[:, [3, 4]].to(device)
# test
x_test = test[:, [0, 1]].to(device)
y_test = test[:, [3, 4]].to(device)
# NOTE: Here we estimate the noise variance
# Print train details
print(f"=== {region_name.upper()} ===")
print(f"Training inputs shape: {x_train.shape}")
print(f"Training observations shape: {y_train.shape}")
print(f"Training inputs dtype: {x_train.dtype}")
print()
# Print test details
print(f"=== {region_name.upper()} ===")
print(f"Test inputs shape: {x_test.shape}")
print(f"Test observations shape: {y_test.shape}")
print(f"Test inputs dtype: {x_test.dtype}")
print()
##################################
### LOOP 2 - over training run ###
##################################
# NOTE: GPs don't train on batches, use full data
for run in range(NUM_RUNS):
print(f"\n--- Training Run {run + 1}/{NUM_RUNS} ---")
# Initialise the likelihood for the GP model (estimates noise)
# NOTE: we use a multitask likelihood for the GP model but with a global noise term
likelihood = gpytorch.likelihoods.MultitaskGaussianLikelihood(
num_tasks = 2,
has_global_noise = True,
has_task_noise = False, # HACK: This still needs to be manually turned off
).to(device)
model = GP(
x_train,
y_train,
likelihood
).to(device)
# INITIALISATIONS & CONSTRAINTS
# CONSTRAINT: Domain-informed noise variance constraint
model.likelihood.register_constraint(
"raw_noise", gpytorch.constraints.Interval(REAL_NOISE_VAR_RANGE[0], REAL_NOISE_VAR_RANGE[1])
)
# Overwrite default noise variance initialisation with REAL data noise range init
model.likelihood.noise = torch.empty(1, device = device).uniform_(REAL_NOISE_VAR_RANGE[1], REAL_NOISE_VAR_RANGE[1])
# Use other default initialisations from SIM experiments
# GP models do not need weight decay, so we set it to 0
optimizer = torch.optim.AdamW(model.parameters(), lr = MODEL_LEARNING_RATE, weight_decay = 0)
# Use ExactMarginalLogLikelihood
mll = gpytorch.mlls.ExactMarginalLogLikelihood(likelihood, model)
model.train()
likelihood.train()
# _________________
# BEFORE EPOCH LOOP
# Export the convergence just for first run only
if run == 0:
# initialise tensors to store losses over epochs (for convergence plot)
train_losses_NLML_over_epochs = torch.zeros(MAX_NUM_EPOCHS) # objective
train_losses_RMSE_over_epochs = torch.zeros(MAX_NUM_EPOCHS) # by-product
# monitor performance transfer to test (only RMSE easy to calc without covar)
test_losses_RMSE_over_epochs = torch.zeros(MAX_NUM_EPOCHS)
# NOTE: Here, we estimate the noise
l1_over_epochs = torch.zeros(MAX_NUM_EPOCHS)
l2_over_epochs = torch.zeros(MAX_NUM_EPOCHS)
Buu_over_epochs = torch.zeros(MAX_NUM_EPOCHS)
Buv_over_epochs = torch.zeros(MAX_NUM_EPOCHS)
Bvu_over_epochs = torch.zeros(MAX_NUM_EPOCHS)
Bvv_over_epochs = torch.zeros(MAX_NUM_EPOCHS)
noise_var_over_epochs = torch.zeros(MAX_NUM_EPOCHS)
# Early stopping variables
best_loss = float('inf')
# counter starts at 0
epochs_no_improve = 0
############################
### LOOP 3 - over EPOCHS ###
############################
print("\nStart Training")
for epoch in range(MAX_NUM_EPOCHS):
# Set to train
model.train()
likelihood.train()
# Do a step
optimizer.zero_grad()
# model outputs a multivariate normal distribution
train_pred_dist = model(x_train.to(device))
# Train on noisy or targets
# NOTE: We only have observational y_train i.e. noisy data
loss = - mll(train_pred_dist, y_train.to(device)) # negative marginal log likelihood
loss.backward()
optimizer.step()
# For Run 1 we save a bunch of metrics and update, while for the rest we only update
if run == 0:
model.eval()
likelihood.eval()
with warnings.catch_warnings():
warnings.simplefilter("ignore", gpytorch.utils.warnings.GPInputWarning)
train_pred_dist = model(x_train.to(device))
test_pred_dist = model(x_test.to(device))
# Compute RMSE for training and test predictions (given true data, not noisy)
train_RMSE = torch.sqrt(gpytorch.metrics.mean_squared_error(train_pred_dist, y_train.to(device)).mean())
test_RMSE = torch.sqrt(gpytorch.metrics.mean_squared_error(test_pred_dist, y_test.to(device)).mean())
# Save losses for convergence plot
train_losses_NLML_over_epochs[epoch] = loss.item()
train_losses_RMSE_over_epochs[epoch] = train_RMSE.item()
test_losses_RMSE_over_epochs[epoch] = test_RMSE.item()
# Save evolution of hypers for convergence plot
# NOTE: This is different to dfGPs
l1_over_epochs[epoch] = model.covar_module.data_covar_module.lengthscale[0, 0].item()
l2_over_epochs[epoch] = model.covar_module.data_covar_module.lengthscale[0, 1].item()
# Reconstruct B first via FF.T + D where F is the covar_factor and D is the diagonal matrix of task variances var
B = model.covar_module.task_covar_module.covar_factor @ model.covar_module.task_covar_module.covar_factor.T + torch.diag(model.covar_module.task_covar_module.var)
# Extract items
Buu_over_epochs[epoch] = B[0, 0].item()
Buv_over_epochs[epoch] = B[0, 1].item()
Bvu_over_epochs[epoch] = B[1, 0].item()
Bvv_over_epochs[epoch] = B[1, 1].item()
noise_var_over_epochs[epoch] = model.likelihood.noise.item()
# Print a bit more information for the first run
if epoch % PRINT_FREQUENCY == 0:
print(f"{region_name} {model_name} Run {run + 1}/{NUM_RUNS}, Epoch {epoch + 1}/{MAX_NUM_EPOCHS}, Training Loss (NLML): {loss:.4f}, Training RMSE: {train_RMSE:.4f}")
# delete after printing and saving
# NOTE: keep loss for early stopping check
del train_pred_dist, test_pred_dist, train_RMSE, test_RMSE
# Free up memory every PRINT_FREQUENCY epochs
if epoch % PRINT_FREQUENCY == 0:
gc.collect() and torch.cuda.empty_cache()
# For all runs after the first we run a minimal version using only lml_train
else:
if epoch % PRINT_FREQUENCY == 0:
# After run 1 we only print lml, nothing else
print(f"{region_name} {model_name} Run {run + 1}/{NUM_RUNS}, Epoch {epoch + 1}/{MAX_NUM_EPOCHS}, Training Loss (NLML): {loss:.4f}")
# EVERY EPOCH: Early stopping check
if loss < best_loss:
best_loss = loss
# reset counter if loss improves
epochs_no_improve = 0
else:
epochs_no_improve += 1
if epochs_no_improve >= PATIENCE:
print(f"Early stopping triggered after {epoch + 1} epochs.")
# exit epoch loop
break
##############################
### END LOOP 3 over EPOCHS ###
##############################
# for every run...
#######################################################
### EVALUATE after all training for RUN is finished ###
#######################################################
model.eval()
likelihood.eval()
# Need gradients for autograd divergence: We clone and detach
x_test_grad = x_test.to(device).clone().requires_grad_(True)
x_train_grad = x_train.to(device).clone().requires_grad_(True)
# Underlying (latent) distribution and predictive distribution
dist_test = model(x_test_grad)
pred_dist_test = likelihood(dist_test)
with warnings.catch_warnings():
warnings.simplefilter("ignore", gpytorch.utils.warnings.GPInputWarning)
dist_train = model(x_train_grad)
pred_dist_train = likelihood(dist_train)
# Compute divergence field (from latent distribution)
test_div_field = compute_divergence_field(dist_test.mean, x_test_grad)
train_div_field = compute_divergence_field(dist_train.mean, x_train_grad)
# Only save mean_pred, covar_pred and divergence fields for the first run
if run == 0:
# (1) Save predictions from first run so we can visualise them later
torch.save(pred_dist_test.mean, f"{MODEL_REAL_RESULTS_DIR}/{region_name}_{model_name}_test_mean_predictions.pt")
torch.save(pred_dist_test.covariance_matrix, f"{MODEL_REAL_RESULTS_DIR}/{region_name}_{model_name}_test_covar_predictions.pt")
# (2) Save divergence field
torch.save(test_div_field, f"{MODEL_REAL_RESULTS_DIR}/{region_name}_{model_name}_test_prediction_divergence_field.pt")
# (3) Since all epoch training is finished, we can save the losses over epochs
df_losses = pd.DataFrame({
'Epoch': list(range(train_losses_NLML_over_epochs.shape[0])), # pythonic indexing
'Train NLML': train_losses_NLML_over_epochs.tolist(),
'Train RMSE': train_losses_RMSE_over_epochs.tolist(),
'Test RMSE': test_losses_RMSE_over_epochs.tolist(),
# hyperparameters
'l1': l1_over_epochs.tolist(),
'l2': l2_over_epochs.tolist(),
'Buu': Buu_over_epochs.tolist(),
'Buv': Buv_over_epochs.tolist(),
'Bvu': Bvu_over_epochs.tolist(),
'Bvv': Bvv_over_epochs.tolist(),
'noise_var': noise_var_over_epochs.tolist(),
})
df_losses.to_csv(f"{MODEL_REAL_RESULTS_DIR}/{region_name}_{model_name}_losses_over_epochs.csv", index = False, float_format = "%.5f") # reduce to 5 decimals for readability
# Compute TRAIN metrics (convert tensors to float) for every run's tuned model
# NOTE: gpytorch outputs metrics per task
train_RMSE = torch.sqrt(gpytorch.metrics.mean_squared_error(
pred_dist_train, y_train.to(device)).mean()).item()
train_MAE = gpytorch.metrics.mean_absolute_error(
pred_dist_train, y_train.to(device)).mean().item()
train_NLL = gpytorch.metrics.negative_log_predictive_density(
pred_dist_train, y_train.to(device)).item()
train_QCE = quantile_coverage_error_2d(
pred_dist_train, y_train.to(device), quantile = 95.0).item()
## NOTE: It is important to use the absolute value of the divergence field, since both positive and negative deviations are violations and shouldn't cancel each other out
train_MAD = train_div_field.abs().mean().item()
# Compute TEST metrics (convert tensors to float) for every run's tuned model
test_RMSE = torch.sqrt(gpytorch.metrics.mean_squared_error(
pred_dist_test, y_test.to(device)).mean()).item()
test_MAE = gpytorch.metrics.mean_absolute_error(
pred_dist_test, y_test.to(device)).mean().item()
test_NLL = gpytorch.metrics.negative_log_predictive_density(
pred_dist_test, y_test.to(device)).item()
test_QCE = quantile_coverage_error_2d(
pred_dist_test, y_test.to(device), quantile = 95.0).item()
## NOTE: It is important to use the absolute value of the divergence field, since both positive and negative deviations are violations and shouldn't cancel each other out
test_MAD = test_div_field.abs().mean().item()
region_results.append([
run + 1,
train_RMSE, train_MAE, train_NLL, train_QCE, train_MAD,
test_RMSE, test_MAE, test_NLL, test_QCE, test_MAD
])
# clean up
del dist_train, dist_test, pred_dist_train, pred_dist_test, test_div_field, train_div_field
gc.collect()
torch.cuda.empty_cache()
############################
### END LOOP 2 over RUNS ###
############################
# Convert results to a Pandas DataFrame
results_per_run = pd.DataFrame(
region_results,
columns = ["Run",
"Train RMSE", "Train MAE", "Train NLL", "Train QCE", "Train MAD",
"Test RMSE", "Test MAE", "Test NLL", "Test QCE", "Test MAD"])
# Compute mean and standard deviation for each metric
mean_std_df = results_per_run.iloc[:, 1:].agg(["mean", "std"]) # Exclude "Run" column
# Add region_name and model_name as columns in the DataFrame _metrics_summary to be able to copy df
mean_std_df["region name"] = region_name
mean_std_df["model name"] = model_name
# Save "_metrics_per_run.csv" to CSV
path_to_metrics_per_run = os.path.join(MODEL_REAL_RESULTS_DIR, f"{region_name}_{model_name}_metrics_per_run.csv")
results_per_run.to_csv(path_to_metrics_per_run, index = False, float_format = "%.5f") # reduce to 5 decimals
print(f"\nResults per run saved to {path_to_metrics_per_run}")
# Save "_metrics_summary.csv" to CSV
path_to_metrics_summary = os.path.join(MODEL_REAL_RESULTS_DIR, f"{region_name}_{model_name}_metrics_summary.csv")
mean_std_df.to_csv(path_to_metrics_summary, float_format = "%.5f") # reduce to 5 decimals
print(f"\nMean & Std saved to {path_to_metrics_summary}")
###############################
### END LOOP 1 over REGIONS ###
###############################
#############################
### WALL time & GPU model ###
#############################
end_time = time.time()
# compute elapsed time
elapsed_time = end_time - start_time
# convert elapsed time to minutes
elapsed_time_minutes = elapsed_time / 60
# also end emission tracking. Will be saved as emissions.csv
if TRACK_EMISSIONS_BOOL:
tracker.stop()
if device == "cuda":
# get name of GPU model
gpu_name = torch.cuda.get_device_name(0)
else:
gpu_name = "N/A"
print(f"Elapsed wall time: {elapsed_time:.4f} seconds")
# Define full path for the file
wall_time_and_gpu_path = os.path.join(MODEL_REAL_RESULTS_DIR, model_name + "_run_" "wall_time.txt")
# Save to the correct folder with both seconds and minutes
with open(wall_time_and_gpu_path, "w") as f:
f.write(f"Elapsed wall time: {elapsed_time:.4f} seconds\n")
f.write(f"Elapsed wall time: {elapsed_time_minutes:.2f} minutes\n")
f.write(f"Device used: {device}\n")
f.write(f"GPU model: {gpu_name}\n")
print(f"Wall time saved to {wall_time_and_gpu_path}.")