-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
709 lines (616 loc) · 22.1 KB
/
util.py
File metadata and controls
709 lines (616 loc) · 22.1 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
import torch
from torch.utils.data import TensorDataset
import torch.nn.functional as F
import polars as pl
import numpy as np
import beaupy
from rich.console import Console
import wandb
import optuna
#from scipy.optimize import curve_fit, least_squares
#from scipy.stats import linregress
#import warnings
from config import RunConfig
import random
import os
import math
def load_data(file_path: str):
"""
Load data from parquet file.
Returns:
TensorDataset with (V, t, q, p, ic) where ic = (q0, p0)
"""
df = pl.read_parquet(file_path)
V = torch.tensor(df["V"].to_numpy().reshape(-1, 100), dtype=torch.float32)
t = torch.tensor(df["t"].to_numpy().reshape(-1, 100), dtype=torch.float32)
q = torch.tensor(df["q"].to_numpy().reshape(-1, 100), dtype=torch.float32)
p = torch.tensor(df["p"].to_numpy().reshape(-1, 100), dtype=torch.float32)
# Extract initial conditions (first time point of each sample)
ic = torch.stack([q[:, 0], p[:, 0]], dim=1) # (N, 2)
return TensorDataset(V, t, q, p, ic)
def set_seed(seed: int):
# random
random.seed(seed)
# numpy
np.random.seed(seed)
# pytorch
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
class EarlyStopping:
def __init__(self, patience=10, mode="min", min_delta=0):
self.patience = patience
self.mode = mode
self.min_delta = min_delta
self.counter = 0
self.best_loss = None
self.early_stop = False
def __call__(self, val_loss):
if self.best_loss is None:
self.best_loss = val_loss
return False
if self.mode == "min":
if val_loss <= self.best_loss * (1 - self.min_delta):
self.best_loss = val_loss
self.counter = 0
else:
self.counter += 1
else: # mode == "max"
if val_loss >= self.best_loss * (1 + self.min_delta):
self.best_loss = val_loss
self.counter = 0
else:
self.counter += 1
if self.counter >= self.patience:
self.early_stop = True
return True
return False
def predict_final_loss(losses, max_epochs):
if len(losses) < 10:
return -np.log10(losses[-1])
try:
# Convert to numpy array
y = np.array(losses)
t = np.arange(len(y))
# Decay fitting
y_transformed = np.log(y)
K, log_A = np.polyfit(t, y_transformed, 1)
A = np.exp(log_A)
# Predict final loss
predicted_loss = -np.log10(A * np.exp(K * max_epochs))
if np.isfinite(predicted_loss):
return predicted_loss
except Exception as e:
print(f"Error in loss prediction: {e}")
return -np.log10(losses[-1])
#def predict_final_loss(losses, max_epochs):
# """
# Predict final loss using multiple curve fitting models.
#
# Args:
# losses: List of validation losses
# max_epochs: Target epoch to predict
#
# Returns:
# Predicted final loss (negative log scale)
# """
# if len(losses) < 5:
# return -np.log10(losses[-1])
#
# # Convert to numpy arrays
# y = np.array(losses)
# t = np.arange(len(y))
#
# # Handle edge cases
# if np.any(~np.isfinite(y)) or np.any(y <= 0):
# return -np.log10(losses[-1])
#
# # Check for plateau - if recent losses are not changing much
# if len(losses) >= 20:
# recent_std = np.std(losses[-10:])
# recent_mean = np.mean(losses[-10:])
# if recent_std / recent_mean < 0.001: # Very small relative change
# return -np.log10(recent_mean)
#
# predictions = []
#
# # Model 1: Exponential decay - y = a * exp(b * t) + c
# try:
# def exp_decay(t, a, b, c):
# return a * np.exp(b * t) + c
#
# # Initial guess based on data
# a0 = y[0] - y[-1]
# b0 = np.log(y[-1] / y[0]) / len(y) if y[0] > 0 and y[-1] > 0 else -0.1
# c0 = min(y) * 0.9
#
# popt, _ = curve_fit(exp_decay, t, y,
# p0=[a0, b0, c0],
# bounds=([0, -np.inf, 0], [np.inf, 0, min(y)]),
# maxfev=5000)
#
# pred = exp_decay(max_epochs, *popt)
# if pred > 0 and pred < y[0]: # Sanity check
# predictions.append(pred)
# except:
# pass
#
# # Model 2: Power law - y = a * t^b + c
# try:
# def power_law(t, a, b, c):
# return a * (t + 1) ** b + c
#
# # Transform to avoid t=0 issues
# popt, _ = curve_fit(power_law, t, y,
# bounds=([0, -5, 0], [np.inf, 0, min(y)]),
# maxfev=5000)
#
# pred = power_law(max_epochs, *popt)
# if pred > 0 and pred < y[0]:
# predictions.append(pred)
# except:
# pass
#
# # Model 3: Logarithmic - y = a * log(t + 1) + b
# try:
# def log_model(t, a, b):
# return a * np.log(t + 1) + b
#
# popt, _ = curve_fit(log_model, t, y, maxfev=5000)
# pred = log_model(max_epochs, *popt)
#
# if pred > 0 and pred < y[0]:
# predictions.append(pred)
# except:
# pass
#
# # Model 4: Inverse - y = a / (t + 1) + b
# try:
# def inverse_model(t, a, b):
# return a / (t + 1) + b
#
# popt, _ = curve_fit(inverse_model, t, y,
# bounds=([0, 0], [np.inf, min(y)]),
# maxfev=5000)
#
# pred = inverse_model(max_epochs, *popt)
# if pred > 0 and pred < y[0]:
# predictions.append(pred)
# except:
# pass
#
# # Model 5: Double exponential for more complex curves
# if len(losses) >= 10:
# try:
# def double_exp(t, a1, b1, a2, b2, c):
# return a1 * np.exp(b1 * t) + a2 * np.exp(b2 * t) + c
#
# # Initial guesses
# mid = len(y) // 2
# a1_0 = (y[0] - y[mid]) * 0.7
# a2_0 = (y[0] - y[mid]) * 0.3
# b1_0 = np.log(0.5) / mid
# b2_0 = np.log(0.1) / mid
# c_0 = min(y) * 0.9
#
# popt, _ = curve_fit(double_exp, t, y,
# p0=[a1_0, b1_0, a2_0, b2_0, c_0],
# bounds=([0, -np.inf, 0, -np.inf, 0],
# [np.inf, 0, np.inf, 0, min(y)]),
# maxfev=5000)
#
# pred = double_exp(max_epochs, *popt)
# if pred > 0 and pred < y[0]:
# predictions.append(pred)
# except:
# pass
#
# # Model 6: Polynomial with constraints (for smooth extrapolation)
# if len(losses) >= 10:
# try:
# # Use lower degree polynomial to avoid overfitting
# degree = min(3, len(losses) // 5)
#
# # Fit polynomial to recent data for better local behavior
# recent_points = min(20, len(losses))
# t_recent = t[-recent_points:]
# y_recent = y[-recent_points:]
#
# # Normalize for numerical stability
# t_norm = (t_recent - t_recent[0]) / (t_recent[-1] - t_recent[0])
# coeffs = np.polyfit(t_norm, y_recent, degree)
#
# # Extrapolate
# t_pred_norm = (max_epochs - t_recent[0]) / (t_recent[-1] - t_recent[0])
# pred = np.polyval(coeffs, t_pred_norm)
#
# # Only accept if decreasing and reasonable
# if pred > 0 and pred < y_recent[0]:
# predictions.append(pred)
# except:
# pass
#
# # If we have predictions, use robust averaging
# if predictions:
# # Remove outliers using IQR
# predictions = np.array(predictions)
# q1, q3 = np.percentile(predictions, [25, 75])
# iqr = q3 - q1
# lower_bound = q1 - 1.5 * iqr
# upper_bound = q3 + 1.5 * iqr
#
# # Filter predictions
# filtered = predictions[(predictions >= lower_bound) &
# (predictions <= upper_bound)]
#
# if len(filtered) > 0:
# # Weighted average favoring lower predictions (more conservative)
# weights = 1.0 / (filtered + 1e-10)
# final_pred = np.average(filtered, weights=weights)
# else:
# final_pred = np.median(predictions)
#
# return -np.log10(final_pred)
#
# # Fallback: linear extrapolation of recent trend
# if len(losses) >= 10:
# recent = losses[-10:]
# t_recent = np.arange(len(recent))
# slope, intercept, _, _, _ = linregress(t_recent, recent)
#
# if slope < 0: # Only if decreasing
# pred = intercept + slope * (max_epochs - len(losses) + 10)
# if pred > 0:
# return -np.log10(pred)
#
# # Final fallback
# return -np.log10(losses[-1])
class Trainer:
def __init__(
self,
model,
optimizer,
scheduler,
criterion,
early_stopping_config=None,
device="cpu",
variational=False,
trial=None,
seed=None,
pruner=None,
):
self.model = model
self.optimizer = optimizer
self.scheduler = scheduler
self.criterion = criterion
self.device = device
self.variational = variational
self.trial = trial
self.seed = seed
self.pruner = pruner
if early_stopping_config and early_stopping_config.enabled:
self.early_stopping = EarlyStopping(
patience=early_stopping_config.patience,
mode=early_stopping_config.mode,
min_delta=early_stopping_config.min_delta,
)
else:
self.early_stopping = None
def step(self, V, t, ic):
return self.model(V, t, ic)
def _obtain_loss(self, V, t, q, p, ic):
q_pred, p_pred = self.step(V, t, ic)
loss_q = self.criterion(q_pred, q)
loss_p = self.criterion(p_pred, p)
loss = 0.5 * (loss_q + loss_p)
return loss
def _obtain_vae_loss(self, V, t, q, p, ic):
q_pred, p_pred, mu, logvar = self.step(V, t, ic)
# Flatten
mu_vec = mu.view((mu.shape[0], -1))
logvar_vec = logvar.view((logvar.shape[0], -1))
# KL Divergence (mean over latent dimensions)
kl_loss = -0.5 * torch.mean(
1 + logvar_vec - mu_vec.pow(2) - logvar_vec.exp(), dim=1
)
beta = self.model.kl_weight
kl_loss = beta * torch.mean(kl_loss)
# Total loss
loss_q = self.criterion(q_pred, q)
loss_p = self.criterion(p_pred, p)
loss = 0.5 * (loss_q + loss_p) + kl_loss
return loss
def train_epoch(self, dl_train):
self.model.train()
# ScheduleFree Optimizer or SPlus
if any(keyword in self.optimizer.__class__.__name__ for keyword in ["ScheduleFree", "SPlus"]):
self.optimizer.train()
train_loss = 0
for V, t, q, p, ic in dl_train:
V = V.to(self.device)
t = t.to(self.device)
q = q.to(self.device)
p = p.to(self.device)
ic = ic.to(self.device)
if not self.variational:
loss = self._obtain_loss(V, t, q, p, ic)
else:
loss = self._obtain_vae_loss(V, t, q, p, ic)
train_loss += loss.item()
self.optimizer.zero_grad(set_to_none=True)
loss.backward()
self.optimizer.step()
train_loss /= len(dl_train)
return train_loss
def val_epoch(self, dl_val):
self.model.eval()
# ScheduleFree Optimizer or SPlus
if any(keyword in self.optimizer.__class__.__name__ for keyword in ["ScheduleFree", "SPlus"]):
self.optimizer.eval()
val_loss = 0
with torch.no_grad():
for V, t, q, p, ic in dl_val:
V = V.to(self.device)
t = t.to(self.device)
q = q.to(self.device)
p = p.to(self.device)
ic = ic.to(self.device)
if not self.variational:
loss = self._obtain_loss(V, t, q, p, ic)
else:
loss = self._obtain_vae_loss(V, t, q, p, ic)
val_loss += loss.item()
val_loss /= len(dl_val)
return val_loss
def train(self, dl_train, dl_val, epochs):
val_loss = 0
train_losses = []
val_losses = []
for epoch in range(epochs):
train_loss = self.train_epoch(dl_train)
val_loss = self.val_epoch(dl_val)
train_losses.append(train_loss)
val_losses.append(val_loss)
# Early stopping if loss becomes NaN
if math.isnan(train_loss) or math.isnan(val_loss):
print("Early stopping due to NaN loss")
train_loss = math.inf
val_loss = math.inf
break
# Early stopping check
if self.early_stopping is not None:
if self.early_stopping(val_loss):
print(f"Early stopping triggered at epoch {epoch}")
break
log_dict = {
"train_loss": train_loss,
"val_loss": val_loss,
"lr": self.optimizer.param_groups[0]["lr"],
}
if epoch >= 10:
log_dict["predicted_final_loss"] = predict_final_loss(
train_losses, epochs
)
# Pruning check
if (
self.pruner is not None
and self.trial is not None
and self.seed is not None
):
self.pruner.report(
trial_id=self.trial.number,
seed=self.seed,
epoch=epoch,
value=val_loss,
)
if self.pruner.should_prune():
raise optuna.TrialPruned()
self.scheduler.step()
wandb.log(log_dict)
if epoch % 10 == 0 or epoch == epochs - 1:
print_str = f"epoch: {epoch}"
for key, value in log_dict.items():
print_str += f", {key}: {value:.4e}"
print(print_str)
return val_loss
def log_cosh_loss(y_pred, y_true, reduction="mean"):
error = y_pred - y_true
loss = torch.log(torch.cosh(error))
if reduction == "mean":
return loss.mean()
elif reduction == "sum":
return loss.sum()
else:
return loss # No reduction
def np_log_cosh_loss(y_pred, y_true, reduction="mean"):
error = y_pred - y_true
loss = np.log(np.cosh(error))
if reduction == "mean":
return np.mean(loss)
elif reduction == "sum":
return np.sum(loss)
else:
return loss # No reduction
def run(
run_config: RunConfig,
dl_train,
dl_val,
group_name=None,
data=None,
trial=None,
pruner=None,
):
project = run_config.project
device = run_config.device
seeds = run_config.seeds
if not group_name:
group_name = run_config.gen_group_name(data)
tags = run_config.gen_tags()
group_path = f"runs/{run_config.project}/{group_name}"
if not os.path.exists(group_path):
os.makedirs(group_path)
run_config.to_yaml(f"{group_path}/config.yaml")
# Register trial at the beginning if pruner exists
if pruner is not None and trial is not None and hasattr(pruner, "register_trial"):
pruner.register_trial(trial.number)
total_loss = 0
complete_seeds = 0
try:
for seed in seeds:
set_seed(seed)
model = run_config.create_model().to(device)
optimizer = run_config.create_optimizer(model)
scheduler = run_config.create_scheduler(optimizer)
run_name = f"{seed}"
wandb.init(
project=project,
name=run_name,
group=group_name,
tags=tags,
config=run_config.gen_config(),
)
# Check if using VaRONet
variational = "VaRONet" in run_config.net
trainer = Trainer(
model,
optimizer,
scheduler,
criterion=log_cosh_loss, # v0.21, v0.24
#criterion=F.mse_loss, # ~v0.20, v0.22, v0.23
early_stopping_config=run_config.early_stopping_config,
device=device,
variational=variational,
trial=trial,
seed=seed,
pruner=pruner,
)
val_loss = trainer.train(dl_train, dl_val, epochs=run_config.epochs)
total_loss += val_loss
complete_seeds += 1
# Save model & configs
run_path = f"{group_path}/{run_name}"
if not os.path.exists(run_path):
os.makedirs(run_path)
torch.save(model.state_dict(), f"{run_path}/model.pt")
wandb.finish()
# Early stopping if loss becomes inf
if math.isinf(val_loss):
break
except optuna.TrialPruned:
wandb.finish()
raise
except Exception as e:
print(f"Runtime error during training: {e}")
wandb.finish()
raise optuna.TrialPruned()
finally:
# Call trial_finished only once after all seeds are done
if (
pruner is not None
and trial is not None
and hasattr(pruner, "complete_trial")
):
pruner.complete_trial(trial.number)
return total_loss / (complete_seeds if complete_seeds > 0 else 1)
# ┌──────────────────────────────────────────────────────────┐
# For Analyze
# └──────────────────────────────────────────────────────────┘
def select_project():
runs_path = "runs/"
projects = [
d for d in os.listdir(runs_path) if os.path.isdir(os.path.join(runs_path, d))
]
# Sort the project names
projects.sort()
if not projects:
raise ValueError(f"No projects found in {runs_path}")
selected_project = beaupy.select(projects)
return selected_project
def select_group(project):
runs_path = f"runs/{project}"
groups = [
d for d in os.listdir(runs_path) if os.path.isdir(os.path.join(runs_path, d))
]
groups.sort()
if not groups:
raise ValueError(f"No run groups found in {runs_path}")
selected_group = beaupy.select(groups)
return selected_group # pyright: ignore
def select_seed(project, group_name):
group_path = f"runs/{project}/{group_name}"
seeds = [
d for d in os.listdir(group_path) if os.path.isdir(os.path.join(group_path, d))
]
seeds.sort()
if not seeds:
raise ValueError(f"No seeds found in {group_path}")
selected_seed = beaupy.select(seeds)
return selected_seed
def select_device():
devices = ["cpu"] + [f"cuda:{i}" for i in range(torch.cuda.device_count())]
selected_device = beaupy.select(devices)
return selected_device
def load_model(project, group_name, seed, weights_only=True):
"""
Load a trained model and its configuration.
Args:
project (str): The name of the project.
group_name (str): The name of the run group.
seed (str): The seed of the specific run.
weights_only (bool, optional): If True, only load the model weights without loading the entire pickle file.
This can be faster and use less memory. Defaults to True.
Returns:
tuple: A tuple containing the loaded model and its configuration.
Raises:
FileNotFoundError: If the config or model file is not found.
Example usage:
# Load full model
model, config = load_model("MyProject", "experiment1", "seed42")
# Load only weights (faster and uses less memory)
model, config = load_model("MyProject", "experiment1", "seed42", weights_only=True)
"""
config_path = f"runs/{project}/{group_name}/config.yaml"
model_path = f"runs/{project}/{group_name}/{seed}/model.pt"
if not os.path.exists(config_path):
raise FileNotFoundError(f"Config file not found for {project}/{group_name}")
if not os.path.exists(model_path):
raise FileNotFoundError(
f"Model file not found for {project}/{group_name}/{seed}"
)
config = RunConfig.from_yaml(config_path)
model = config.create_model()
# Use weights_only option in torch.load
state_dict = torch.load(model_path, map_location="cpu", weights_only=weights_only)
model.load_state_dict(state_dict)
return model, config
def load_study(project, study_name):
"""
Load the best study from an optimization run.
Args:
project (str): The name of the project.
study_name (str): The name of the study.
Returns:
optuna.Study: The loaded study object.
"""
study = optuna.load_study(study_name=study_name, storage=f"sqlite:///{project}.db")
return study
def load_best_model(project, study_name, weights_only=True):
"""
Load the best model and its configuration from an optimization study.
Args:
project (str): The name of the project.
study_name (str): The name of the study.
Returns:
tuple: A tuple containing the loaded model, its configuration, and the best trial number.
"""
study = load_study(project, study_name)
best_trial = study.best_trial
project_name = f"{project}_Opt"
group_name = best_trial.user_attrs["group_name"]
# Select Seed
seed = select_seed(project_name, group_name)
best_model, best_config = load_model(
project_name, group_name, seed, weights_only=weights_only
)
return best_model, best_config