File size: 9,996 Bytes
6aa4c8c cf47595 6aa4c8c 93db32e 6aa4c8c cf47595 6aa4c8c 93db32e 6aa4c8c 7a2f94c 5677fec 6aa4c8c 7a2f94c 93db32e 7a2f94c 6aa4c8c cf47595 6aa4c8c 93db32e 6aa4c8c 5677fec 6aa4c8c cf47595 d64b5de bc0f47a d64b5de 6aa4c8c bc0f47a cf47595 bc0f47a 5677fec cf47595 6aa4c8c 93db32e 6aa4c8c 93db32e 6aa4c8c 93db32e cf47595 bc0f47a 93db32e d64b5de 6aa4c8c cf47595 6aa4c8c 93db32e 6aa4c8c cf47595 d64b5de cf47595 d64b5de cf47595 6aa4c8c 93db32e 6aa4c8c 93db32e 6aa4c8c 93db32e 6aa4c8c cf47595 93db32e 6aa4c8c cf47595 6aa4c8c 93db32e cf47595 6aa4c8c 93db32e 6aa4c8c d64b5de cf47595 5677fec d64b5de 93db32e d64b5de cf47595 93db32e cf47595 93db32e d64b5de 6aa4c8c cf47595 d64b5de cf47595 d64b5de 6aa4c8c 93db32e 6aa4c8c cf47595 6aa4c8c 93db32e 6aa4c8c cf47595 6aa4c8c cf47595 6aa4c8c 93db32e 6aa4c8c cf47595 93db32e 6aa4c8c d64b5de cf47595 d64b5de 6aa4c8c cf47595 d64b5de 6aa4c8c 93db32e 6aa4c8c 93db32e 6aa4c8c 93db32e 6aa4c8c 93db32e cf47595 6aa4c8c 93db32e 6aa4c8c 93db32e 6aa4c8c 93db32e 6aa4c8c cf47595 6aa4c8c 93db32e 6aa4c8c 93db32e 6aa4c8c 93db32e 6aa4c8c 93db32e 6aa4c8c 93db32e cf47595 |
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 |
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim.lr_scheduler import ReduceLROnPlateau
import numpy as np
import time
import logging
from utils.metrics import GraphMetrics
logger = logging.getLogger(__name__)
class GraphMambaTrainer:
"""Anti-overfitting trainer with heavy regularization"""
def __init__(self, model, config, device):
self.model = model
self.config = config
self.device = device
# Optimized learning parameters
self.lr = config['training']['learning_rate']
self.epochs = config['training']['epochs']
self.patience = config['training'].get('patience', 20)
self.min_lr = config['training'].get('min_lr', 1e-6)
self.max_gap = config['training'].get('max_gap', 0.4)
# Heavily regularized optimizer
self.optimizer = optim.AdamW(
model.parameters(),
lr=self.lr,
weight_decay=config['training']['weight_decay'],
betas=(0.9, 0.999),
eps=1e-8
)
# Proper loss function with label smoothing
self.criterion = nn.CrossEntropyLoss(
label_smoothing=config['training'].get('label_smoothing', 0.1)
)
# Balanced scheduler
self.scheduler = ReduceLROnPlateau(
self.optimizer,
mode='max',
factor=0.5,
patience=5,
min_lr=self.min_lr
)
# Training state
self.best_val_acc = 0.0
self.best_val_loss = float('inf')
self.patience_counter = 0
self.training_history = {
'train_loss': [], 'train_acc': [],
'val_loss': [], 'val_acc': [], 'lr': []
}
# Track overfitting
self.best_gap = float('inf')
self.overfitting_threshold = 0.25 # Balanced threshold
def train_node_classification(self, data, verbose=True):
"""Anti-overfitting training with gap monitoring"""
if verbose:
total_params = sum(p.numel() for p in self.model.parameters())
train_samples = data.train_mask.sum().item()
params_per_sample = total_params / train_samples
print(f"ποΈ Training GraphMamba for {self.epochs} epochs")
print(f"π Dataset: {data.num_nodes} nodes, {data.num_edges} edges")
print(f"π― Classes: {len(torch.unique(data.y))}")
print(f"πΎ Device: {self.device}")
print(f"βοΈ Parameters: {total_params:,}")
print(f"π Training samples: {train_samples}")
print(f"β οΈ Params per sample: {params_per_sample:.1f}")
print(f"π¨ Max allowed gap: {self.max_gap:.3f}")
if params_per_sample > 500:
print(f"π¨ WARNING: High params per sample ratio - overfitting risk!")
# Initialize classifier
num_classes = len(torch.unique(data.y))
self.model._init_classifier(num_classes, self.device)
self.model.train()
start_time = time.time()
for epoch in range(self.epochs):
# Training step
train_metrics = self._train_epoch(data, epoch)
# Validation step
val_metrics = self._validate_epoch(data, epoch)
# Calculate overfitting gap
acc_gap = train_metrics['acc'] - val_metrics['acc']
# Update history
self.training_history['train_loss'].append(train_metrics['loss'])
self.training_history['train_acc'].append(train_metrics['acc'])
self.training_history['val_loss'].append(val_metrics['loss'])
self.training_history['val_acc'].append(val_metrics['acc'])
self.training_history['lr'].append(self.optimizer.param_groups[0]['lr'])
# Step scheduler
self.scheduler.step(val_metrics['acc'])
# Check for improvement
if val_metrics['acc'] > self.best_val_acc:
self.best_val_acc = val_metrics['acc']
self.best_val_loss = val_metrics['loss']
self.best_gap = acc_gap
self.patience_counter = 0
if verbose:
print(f"π New best validation accuracy: {self.best_val_acc:.4f}")
else:
self.patience_counter += 1
# Aggressive overfitting detection
if acc_gap > self.overfitting_threshold:
if verbose:
print(f"π¨ OVERFITTING detected: {acc_gap:.3f} gap")
print(f" Train: {train_metrics['acc']:.3f}, Val: {val_metrics['acc']:.3f}")
# Progress logging
if verbose and (epoch == 0 or (epoch + 1) % 5 == 0 or epoch == self.epochs - 1):
elapsed = time.time() - start_time
gap_indicator = "π¨" if acc_gap > 0.25 else "β οΈ" if acc_gap > 0.15 else "β
"
print(f"Epoch {epoch:3d} | "
f"Train: {train_metrics['loss']:.4f} ({train_metrics['acc']:.4f}) | "
f"Val: {val_metrics['loss']:.4f} ({val_metrics['acc']:.4f}) | "
f"Gap: {acc_gap:.3f} {gap_indicator} | "
f"LR: {self.optimizer.param_groups[0]['lr']:.6f}")
# Enhanced early stopping conditions
if self.patience_counter >= self.patience:
if verbose:
print(f"π Early stopping at epoch {epoch} (patience)")
break
# Stop if gap exceeds threshold
if acc_gap > self.max_gap:
if verbose:
print(f"π Stopping due to overfitting gap: {acc_gap:.3f} > {self.max_gap:.3f}")
break
# Stop if severe overfitting (backup check)
if acc_gap > 0.6:
if verbose:
print(f"π Emergency stop - severe overfitting (gap: {acc_gap:.3f})")
break
if verbose:
total_time = time.time() - start_time
print(f"β
Training completed in {total_time:.2f}s")
print(f"π Best validation accuracy: {self.best_val_acc:.4f}")
print(f"π Best train-val gap: {self.best_gap:.4f}")
if self.best_gap < 0.1:
print("π Excellent generalization!")
elif self.best_gap < 0.2:
print("π Good generalization")
else:
print("β οΈ Some overfitting detected")
return self.training_history
def _train_epoch(self, data, epoch):
"""Single training epoch with regularization"""
self.model.train()
self.optimizer.zero_grad()
# Forward pass (with data augmentation)
h = self.model(data.x, data.edge_index)
logits = self.model.classifier(h)
# Compute loss on training nodes only
train_loss = self.criterion(logits[data.train_mask], data.y[data.train_mask])
# Add stronger L2 regularization
l2_reg = 0.0
for param in self.model.parameters():
l2_reg += torch.norm(param, p=2)
train_loss += 5e-5 * l2_reg # Increased from 1e-5
# Backward pass with gradient clipping
train_loss.backward()
torch.nn.utils.clip_grad_norm_(self.model.parameters(), max_norm=0.5) # Reduced from 1.0
self.optimizer.step()
# Compute accuracy
with torch.no_grad():
train_pred = logits[data.train_mask].argmax(dim=1)
train_acc = (train_pred == data.y[data.train_mask]).float().mean().item()
return {'loss': train_loss.item(), 'acc': train_acc}
def _validate_epoch(self, data, epoch):
"""Validation without augmentation"""
self.model.eval()
with torch.no_grad():
h = self.model(data.x, data.edge_index)
logits = self.model.classifier(h)
# Validation loss and accuracy
val_loss = self.criterion(logits[data.val_mask], data.y[data.val_mask])
val_pred = logits[data.val_mask].argmax(dim=1)
val_acc = (val_pred == data.y[data.val_mask]).float().mean().item()
return {'loss': val_loss.item(), 'acc': val_acc}
def test(self, data):
"""Test evaluation"""
self.model.eval()
with torch.no_grad():
h = self.model(data.x, data.edge_index)
if self.model.classifier is None:
num_classes = len(torch.unique(data.y))
self.model._init_classifier(num_classes, self.device)
logits = self.model.classifier(h)
# Test metrics
test_loss = self.criterion(logits[data.test_mask], data.y[data.test_mask])
test_pred = logits[data.test_mask]
test_target = data.y[data.test_mask]
metrics = {
'test_loss': test_loss.item(),
'test_acc': GraphMetrics.accuracy(test_pred, test_target),
'f1_macro': GraphMetrics.f1_score_macro(test_pred, test_target),
'f1_micro': GraphMetrics.f1_score_micro(test_pred, test_target),
}
precision, recall = GraphMetrics.precision_recall(test_pred, test_target)
metrics['precision'] = precision
metrics['recall'] = recall
return metrics
def get_embeddings(self, data):
"""Get node embeddings"""
self.model.eval()
with torch.no_grad():
return self.model(data.x, data.edge_index) |