Repository navigation
Expand file tree
/
Copy pathtrainer.py
More file actions
622 lines (521 loc) · 25.7 KB
/
Copy pathtrainer.py
File metadata and controls
622 lines (521 loc) · 25.7 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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.distributed as dist
from torch.cuda.amp import GradScaler
from torch.utils.data import DataLoader
import mlflow
from typing import Dict, List, Optional, Tuple
import numpy as np
from omegaconf import DictConfig
from tqdm import tqdm
import time
from visualization import ModelPerformance
from weight_visualization import WeightVisualizer
from sklearn.metrics import (
roc_auc_score,
accuracy_score,
precision_score,
recall_score,
f1_score,
)
from loss import DisCoLoss, ConditionalDisCoLoss, MoDeLossWrapper
class Trainer:
def __init__(
self,
model: nn.Module,
optimizer: optim.Optimizer,
criterion: nn.Module,
device: torch.device,
feature_names: List[str],
scheduler: Optional[torch.optim.lr_scheduler._LRScheduler] = None,
grad_clip_val: Optional[float] = None,
use_mixed_precision: bool = False,
is_distributed: bool = False,
is_master: bool = True,
):
"""Initialize trainer with model and training components"""
self.model = model
self.optimizer = optimizer
self.criterion = criterion
self.device = device
self.scheduler = scheduler
self.grad_clip_val = grad_clip_val
self.use_mixed_precision = use_mixed_precision and torch.cuda.is_available()
self.scaler = GradScaler() if self.use_mixed_precision else None
self.is_distributed = is_distributed
self.is_master = is_master
# Initialize visualization tools (only on master process if distributed)
self.performance = ModelPerformance(
model=model, feature_names=feature_names, device=device
)
self.weight_viz = WeightVisualizer(model=model, feature_names=feature_names)
# Initialize gradient and weight tracking
self.grad_stats_history = []
self.weight_stats_history = []
def _compute_parameter_stats(self) -> Tuple[Dict[str, float], Dict[str, float]]:
"""Compute gradient and weight statistics for monitoring"""
grad_stats = {}
weight_stats = {}
# Calculate statistics for each parameter
for name, param in self.model.named_parameters():
# Skip non-parameter tensors
if param.requires_grad is False or param.grad is None:
continue
# Get weights and gradients
weights = param.detach().cpu()
grads = param.grad.detach().cpu()
# Weight stats
weight_abs = weights.abs()
weight_stats[f"{name}_mean"] = weights.mean().item()
weight_stats[f"{name}_abs_mean"] = weight_abs.mean().item()
weight_stats[f"{name}_abs_max"] = weight_abs.max().item()
weight_stats[f"{name}_norm"] = weights.norm().item()
# Gradient stats
grad_abs = grads.abs()
grad_stats[f"{name}_mean"] = grads.mean().item()
grad_stats[f"{name}_abs_mean"] = grad_abs.mean().item()
grad_stats[f"{name}_abs_max"] = grad_abs.max().item()
grad_stats[f"{name}_norm"] = grads.norm().item()
# Calculate aggregated statistics
grad_abs_means = [v for k, v in grad_stats.items() if k.endswith("_abs_mean")]
weight_abs_means = [v for k, v in weight_stats.items() if k.endswith("_abs_mean")]
# Overall statistics
grad_stats["overall_abs_mean"] = np.mean(grad_abs_means) if grad_abs_means else 0
grad_stats["overall_abs_min"] = np.min(grad_abs_means) if grad_abs_means else 0
grad_stats["overall_abs_max"] = np.max(grad_abs_means) if grad_abs_means else 0
weight_stats["overall_abs_mean"] = np.mean(weight_abs_means) if weight_abs_means else 0
weight_stats["overall_abs_min"] = np.min(weight_abs_means) if weight_abs_means else 0
weight_stats["overall_abs_max"] = np.max(weight_abs_means) if weight_abs_means else 0
return grad_stats, weight_stats
def _train_step(
self, X: torch.Tensor, y: torch.Tensor, spectators: Optional[torch.Tensor] = None
) -> Tuple[float, torch.Tensor, Dict[str, float], Dict[str, float]]:
"""Single training step with mixed precision support and DisCo loss"""
X, y = X.to(self.device), y.to(self.device)
if spectators is not None:
spectators = spectators.to(self.device)
# Sanity check to counter spurious outliers; Assert that X values are bounded between 0 and 1
min_vals = X.min(dim=0).values
max_vals = X.max(dim=0).values
# Check for NaN values or values outside the expected range
if torch.isnan(min_vals).any() or torch.isnan(max_vals).any():
print(f"WARNING: Found NaN values in input batch. Skipping this batch.")
return None, None, {}, {}
# if not torch.all(min_vals >= 0):
# print(f"WARNING: Input contains values less than 0: {min_vals}. Skipping this batch.")
# return None, None, {}, {}
# if not torch.all(max_vals <= 1):
# print(f"WARNING: Input contains values greater than 1: {max_vals}. Skipping this batch.")
# return None, None, {}, {}
# Reset gradients
self.optimizer.zero_grad()
# Forward pass with optional mixed precision
with torch.autocast(self.device.type, enabled=self.use_mixed_precision):
outputs = self.model(X)
# Apply appropriate loss function
if spectators is not None and (
isinstance(self.criterion, DisCoLoss) or
isinstance(self.criterion, ConditionalDisCoLoss) or
isinstance(self.criterion, MoDeLossWrapper)
):
loss = self.criterion(outputs, y.unsqueeze(1), spectators)
else:
loss = self.criterion(outputs, y.unsqueeze(1))
# Add L1 regularization if applicable
if hasattr(self.model, "get_l1_loss"):
loss += self.model.get_l1_loss()
# Backward pass with gradient scaling
if self.use_mixed_precision:
self.scaler.scale(loss).backward()
if self.grad_clip_val:
self.scaler.unscale_(self.optimizer)
torch.nn.utils.clip_grad_norm_(
self.model.parameters(), self.grad_clip_val
)
# Compute gradient & weight statistics before optimizer step - unscale first
self.scaler.unscale_(self.optimizer)
grad_stats, weight_stats = self._compute_parameter_stats()
self.scaler.step(self.optimizer)
self.scaler.update()
else:
loss.backward()
if self.grad_clip_val:
torch.nn.utils.clip_grad_norm_(
self.model.parameters(), self.grad_clip_val
)
# Compute gradient & weight statistics before optimizer step
grad_stats, weight_stats = self._compute_parameter_stats()
self.optimizer.step()
return loss.item(), outputs, grad_stats, weight_stats
def train(
self,
train_loader: DataLoader,
val_loader: DataLoader,
num_epochs: int,
metrics_cfg: DictConfig,
early_stopping_patience: Optional[int] = None,
) -> Dict[str, list]:
"""Complete training loop with comprehensive logging"""
history = {
"train_loss": [],
"eval_metrics": [],
"learning_rates": [],
"epoch_times": [],
}
best_val_loss = float("inf")
patience_counter = 0
for epoch in range(num_epochs):
epoch_start_time = time.time()
# Set train loader's sampler epoch for proper shuffling in distributed mode
if (
self.is_distributed
and hasattr(train_loader, "sampler")
and hasattr(train_loader.sampler, "set_epoch")
):
train_loader.sampler.set_epoch(epoch)
# Training phase
self.model.train()
train_losses = []
all_train_outputs = []
all_train_targets = []
epoch_grad_stats = {
"overall_abs_mean": [],
"overall_abs_min": [],
"overall_abs_max": [],
}
epoch_weight_stats = {
"overall_abs_mean": [],
"overall_abs_min": [],
"overall_abs_max": [],
}
# Only show progress bar on master process to avoid output clutter
if not self.is_distributed or self.is_master:
progress_bar = tqdm(train_loader, desc=f"Epoch {epoch+1}/{num_epochs}")
train_iter = progress_bar
else:
train_iter = train_loader
for batch in train_iter:
# Handle batches with or without spectator variables
if len(batch) == 3: # (X, y, spectators)
X, y, spectators = batch
batch_loss, outputs, grad_stats, weight_stats = self._train_step(X, y, spectators)
else: # Standard (X, y) batch
X, y = batch
batch_loss, outputs, grad_stats, weight_stats = self._train_step(X, y)
# Skip this batch if it contained NaN values
if batch_loss is None:
continue
train_losses.append(batch_loss)
# Collect gradient and weight stats
for key in epoch_grad_stats.keys():
if key in grad_stats:
epoch_grad_stats[key].append(grad_stats[key])
for key in epoch_weight_stats.keys():
if key in weight_stats:
epoch_weight_stats[key].append(weight_stats[key])
# Collect outputs and targets even in distributed mode for the master process
# This is used for visualizations later
if not self.is_distributed or self.is_master:
all_train_outputs.extend(
torch.sigmoid(outputs).detach().cpu().numpy()
)
all_train_targets.extend(y.cpu().numpy())
# Update progress bar if using one
if hasattr(locals(), "progress_bar"):
progress_bar.set_postfix({"loss": f"{batch_loss:.4f}"})
# Calculate epoch metrics
train_loss = np.mean(train_losses)
# Average gradient and weight stats across batches
avg_grad_stats = {k: np.mean(v) for k, v in epoch_grad_stats.items() if v}
avg_weight_stats = {k: np.mean(v) for k, v in epoch_weight_stats.items() if v}
# Store for history
self.grad_stats_history.append(avg_grad_stats)
self.weight_stats_history.append(avg_weight_stats)
# Synchronize loss across processes in distributed mode
if self.is_distributed:
train_loss_tensor = torch.tensor(train_loss).to(self.device)
dist.all_reduce(train_loss_tensor, op=dist.ReduceOp.SUM)
train_loss = (train_loss_tensor / dist.get_world_size()).item()
# Evaluate on validation set
eval_metrics = self.evaluate(val_loader, metrics_cfg, epoch)
epoch_time = time.time() - epoch_start_time
# Store metrics and do logging (only on master process to avoid conflicts)
if not self.is_distributed or self.is_master:
history["train_loss"].append(train_loss)
history["eval_metrics"].append(eval_metrics)
history["epoch_times"].append(epoch_time)
if self.scheduler:
history["learning_rates"].append(self.scheduler.get_last_lr()[0])
# Generate gradient and weight magnitude report
self._print_parameter_stats_report(avg_grad_stats, avg_weight_stats, epoch)
# Log metrics and visualizations
self._log_epoch_info(
train_loss=train_loss,
eval_metrics=eval_metrics,
epoch=epoch,
epoch_time=epoch_time,
train_outputs=(
np.array(all_train_outputs) if all_train_outputs else None
),
train_targets=(
np.array(all_train_targets) if all_train_targets else None
),
grad_stats=avg_grad_stats,
weight_stats=avg_weight_stats
)
# Generate visualizations periodically
if epoch % 10 == 0 or epoch == num_epochs - 1:
self._generate_visualizations(history, epoch)
# Learning rate scheduling - must happen on all processes
if self.scheduler:
if isinstance(
self.scheduler, torch.optim.lr_scheduler.ReduceLROnPlateau
):
self.scheduler.step(eval_metrics["loss"])
else:
self.scheduler.step()
# Early stopping check - must be synchronized across processes
if early_stopping_patience:
# Broadcast val_loss from master to all processes to ensure consistency
if self.is_distributed:
val_loss_tensor = torch.tensor(eval_metrics["loss"]).to(self.device)
dist.broadcast(val_loss_tensor, src=0)
current_val_loss = val_loss_tensor.item()
else:
current_val_loss = eval_metrics["loss"]
if current_val_loss < best_val_loss:
best_val_loss = current_val_loss
patience_counter = 0
if not self.is_distributed or self.is_master:
self._save_checkpoint(epoch, eval_metrics)
else:
patience_counter += 1
if patience_counter >= early_stopping_patience:
print(f"Early stopping triggered at epoch {epoch+1}")
break
return history
def _print_parameter_stats_report(self, grad_stats: Dict[str, float], weight_stats: Dict[str, float], epoch: int) -> None:
"""Print a concise report of gradient and weight statistics"""
print(f"\n===== PARAMETER STATS REPORT - EPOCH {epoch+1} =====")
# Gradient report
print("\nGRADIENT STATISTICS:")
print(f" Mean absolute gradient: {grad_stats.get('overall_abs_mean', 0):.8f}")
print(f" Min absolute gradient: {grad_stats.get('overall_abs_min', 0):.8f}")
print(f" Max absolute gradient: {grad_stats.get('overall_abs_max', 0):.8f}")
# Warning for vanishing gradients
if grad_stats.get('overall_abs_mean', 0) < 1e-4:
print(" ⚠️ WARNING: Potential vanishing gradients detected!")
# Weight report
print("\nWEIGHT STATISTICS:")
print(f" Mean absolute weight: {weight_stats.get('overall_abs_mean', 0):.6f}")
print(f" Min absolute weight: {weight_stats.get('overall_abs_min', 0):.6f}")
print(f" Max absolute weight: {weight_stats.get('overall_abs_max', 0):.6f}")
# Weight-to-gradient ratio
if grad_stats.get('overall_abs_mean', 0) > 0:
w2g_ratio = weight_stats.get('overall_abs_mean', 0) / grad_stats.get('overall_abs_mean', 0)
print(f"\nWeight-to-gradient ratio: {w2g_ratio:.2f}")
print("=================================================\n")
def evaluate(
self,
val_loader: DataLoader,
metrics_cfg: DictConfig,
epoch: Optional[int] = None,
) -> dict:
"""Comprehensive model evaluation with distributed support"""
self.model.eval()
local_metrics = {}
all_preds = []
all_targets = []
all_spectators = []
val_losses = []
has_spectators = False
with torch.no_grad():
for batch in val_loader:
# Handle batches with or without spectator variables
if len(batch) == 3: # (X, y, spectators)
X, y, spectators = batch
has_spectators = True
else: # Standard (X, y) batch
X, y = batch
spectators = None
# Move data to device
X, y = X.to(self.device), y.to(self.device)
if spectators is not None:
spectators = spectators.to(self.device)
with torch.autocast(self.device.type, enabled=self.use_mixed_precision):
outputs = self.model(X)
# Apply appropriate loss function
if has_spectators and (
isinstance(self.criterion, DisCoLoss) or
isinstance(self.criterion, ConditionalDisCoLoss) or
isinstance(self.criterion, MoDeLossWrapper)
):
loss = self.criterion(outputs, y.unsqueeze(1), spectators)
else:
loss = self.criterion(outputs, y.unsqueeze(1))
val_losses.append(loss.item())
# Collect predictions and targets
all_preds.extend(torch.sigmoid(outputs).cpu().numpy())
all_targets.extend(y.cpu().numpy())
# Collect spectators if available
if spectators is not None:
all_spectators.extend(spectators.cpu().numpy())
# Calculate metrics
local_metrics["loss"] = np.mean(val_losses)
all_preds = np.array(all_preds)
all_targets = np.array(all_targets)
# Calculate performance metrics
if metrics_cfg.compute.get("roc_auc", False):
local_metrics["roc_auc"] = roc_auc_score(all_targets, all_preds)
binary_preds = (all_preds > metrics_cfg.threshold).astype(int)
for metric_name in ["accuracy", "precision", "recall", "f1"]:
if metrics_cfg.compute.get(metric_name, False):
metric_fn = globals()[f"{metric_name}_score"]
local_metrics[metric_name] = metric_fn(all_targets, binary_preds)
# # NOTE: EXPERIMENTAL - NOT USED
# # For distributed training, synchronize metrics across processes
# if self.is_distributed:
# # Create a tensor to hold all metrics
# metrics_keys = list(local_metrics.keys())
# metrics_tensor = torch.zeros(
# len(metrics_keys), dtype=torch.float32, device=self.device
# )
# # Fill the tensor with local metric values
# for i, key in enumerate(metrics_keys):
# metrics_tensor[i] = local_metrics[key]
# # All-reduce to get the average across all processes
# dist.all_reduce(metrics_tensor, op=dist.ReduceOp.SUM)
# metrics_tensor /= dist.get_world_size()
# # Update metrics with synchronized values
# for i, key in enumerate(metrics_keys):
# local_metrics[key] = metrics_tensor[i].item()
# Calculate decorrelation metrics if spectators are available
if has_spectators and len(all_spectators) > 0:
all_spectators = np.array(all_spectators)
# If multiple spectator variables, calculate for each one
if len(all_spectators.shape) > 1:
for i in range(all_spectators.shape[1]):
# Calculate distance correlation between predictions and spectator
# This is a simplified implementation - you would need to implement
# the actual distance correlation calculation
local_metrics[f"spectator_{i}_dcor"] = np.corrcoef(all_preds.ravel(), all_spectators[:, i])[0, 1]
else:
# Single spectator variable
local_metrics["spectator_dcor"] = np.corrcoef(all_preds.ravel(), all_spectators)[0, 1]
return local_metrics
def _log_epoch_info(
self,
train_loss: float,
eval_metrics: Dict[str, float],
epoch: int,
epoch_time: float,
train_outputs: Optional[np.ndarray],
train_targets: Optional[np.ndarray],
grad_stats: Optional[Dict[str, float]] = None,
weight_stats: Optional[Dict[str, float]] = None,
) -> None:
"""Log comprehensive epoch information to MLflow (master process only)"""
if self.is_distributed and not self.is_master:
return
# Log basic metrics
mlflow.log_metric("train_loss", train_loss, step=epoch)
mlflow.log_metric("epoch_time", epoch_time, step=epoch)
for metric_name, metric_value in eval_metrics.items():
mlflow.log_metric(f"eval_{metric_name}", metric_value, step=epoch)
if self.scheduler:
mlflow.log_metric(
"learning_rate", self.scheduler.get_last_lr()[0], step=epoch
)
# Log training distribution metrics if available
if train_outputs is not None and train_targets is not None:
mlflow.log_metric("train_pred_mean", train_outputs.mean(), step=epoch)
mlflow.log_metric("train_pred_std", train_outputs.std(), step=epoch)
# Log gradient statistics if available
if grad_stats:
for stat_name, stat_value in grad_stats.items():
mlflow.log_metric(f"grad_{stat_name}", stat_value, step=epoch)
# Log a vanishing gradient flag
if grad_stats.get('overall_abs_mean', 0) < 1e-4:
mlflow.log_metric("vanishing_gradient_warning", 1, step=epoch)
else:
mlflow.log_metric("vanishing_gradient_warning", 0, step=epoch)
# Log weight statistics if available
if weight_stats:
for stat_name, stat_value in weight_stats.items():
mlflow.log_metric(f"weight_{stat_name}", stat_value, step=epoch)
# Log weight-to-gradient ratio
if grad_stats and weight_stats:
if grad_stats.get('overall_abs_mean', 0) > 0:
w2g_ratio = weight_stats.get('overall_abs_mean', 0) / grad_stats.get('overall_abs_mean', 0)
mlflow.log_metric("weight_to_gradient_ratio", w2g_ratio, step=epoch)
def _generate_visualizations(
self,
history: Dict[str, list],
epoch: int,
) -> None:
"""Generate and log visualizations using direct access to test dataset (master process only)"""
if self.is_distributed and not self.is_master:
return
# Skip if data module is not available
if not hasattr(self, "data_module"):
print("Data module not available for visualization")
return
# Check if we have direct access to the test dataset tensors
if hasattr(self.data_module, "X_test") and hasattr(self.data_module, "y_test"):
# Direct access to dataset tensors
X_viz = self.data_module.X_test.to(self.device)
y_viz = self.data_module.y_test
# Get channels if available
channels = (
self.data_module.test_channels
if hasattr(self.data_module, "test_channels")
else None
)
try:
processor = (
self.data_module.preprocessor
if hasattr(self.data_module, "preprocessor")
else None
)
# Create performance dashboard
self.performance.create_performance_dashboard(
X_viz.cpu().numpy(),
y_viz.numpy(),
history,
epoch,
channels=channels,
preprocessor=processor, # pass to invert [0,1]-scaling in visualization
)
# Visualize weights if needed - uncomment this for weight visualizations
# self.weight_viz.create_weight_dashboard(X_viz, epoch)
# print(f"Visualizations for epoch {epoch} created successfully")
except Exception as e:
print(f"Error creating visualizations: {str(e)}")
else:
print("Test dataset tensors not available for visualization")
return
def _save_checkpoint(self, epoch: int, metrics: Dict[str, float]) -> None:
"""Save model checkpoint (master process only)"""
if self.is_distributed and not self.is_master:
return
# For distributed training, we need to save the unwrapped model
if self.is_distributed:
if hasattr(self.model, "module"):
model_state_dict = self.model.module.state_dict()
else:
model_state_dict = self.model.state_dict()
else:
model_state_dict = self.model.state_dict()
checkpoint = {
"epoch": epoch,
"model_state_dict": model_state_dict,
"optimizer_state_dict": self.optimizer.state_dict(),
"metrics": metrics,
}
if self.scheduler:
checkpoint["scheduler_state_dict"] = self.scheduler.state_dict()
# Save as PyTorch file instead of JSON
checkpoint_path = f"checkpoint_epoch_{epoch}.pth"
torch.save(checkpoint, checkpoint_path)
mlflow.log_artifact(checkpoint_path)