Datasets:
File size: 9,819 Bytes
977dad7 | 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 | #!/usr/bin/env python3
"""
Training script for Ruby complexity prediction using Graph Neural Networks.
This script implements the main training and validation loop for the GNN model
that predicts Ruby method complexity based on AST structure.
"""
import sys
import os
import time
import argparse
import torch
import torch.nn.functional as F
from torch_geometric.data import Data
# Add src directory to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from data_processing import create_data_loaders
from models import RubyComplexityGNN
def train_epoch(model, train_loader, optimizer, criterion, device):
"""
Train the model for one epoch.
Args:
model: The GNN model
train_loader: Training data loader
optimizer: Optimizer instance
criterion: Loss function
device: Device to run on
Returns:
Average training loss for the epoch
"""
model.train()
total_loss = 0.0
num_batches = 0
for batch in train_loader:
# Convert to PyTorch tensors and move to device
x = torch.tensor(batch['x'], dtype=torch.float).to(device)
edge_index = torch.tensor(batch['edge_index'], dtype=torch.long).to(device)
y = torch.tensor(batch['y'], dtype=torch.float).to(device)
batch_idx = torch.tensor(batch['batch'], dtype=torch.long).to(device)
# Create PyTorch Geometric Data object
data = Data(x=x, edge_index=edge_index, batch=batch_idx)
# Forward pass
optimizer.zero_grad()
predictions = model(data)
loss = criterion(predictions.squeeze(), y)
# Backward pass
loss.backward()
optimizer.step()
total_loss += loss.item()
num_batches += 1
return total_loss / num_batches if num_batches > 0 else 0.0
def validate_epoch(model, val_loader, criterion, device):
"""
Validate the model for one epoch.
Args:
model: The GNN model
val_loader: Validation data loader
criterion: Loss function
device: Device to run on
Returns:
Average validation loss for the epoch
"""
model.eval()
total_loss = 0.0
num_batches = 0
with torch.no_grad():
for batch in val_loader:
# Convert to PyTorch tensors and move to device
x = torch.tensor(batch['x'], dtype=torch.float).to(device)
edge_index = torch.tensor(batch['edge_index'], dtype=torch.long).to(device)
y = torch.tensor(batch['y'], dtype=torch.float).to(device)
batch_idx = torch.tensor(batch['batch'], dtype=torch.long).to(device)
# Create PyTorch Geometric Data object
data = Data(x=x, edge_index=edge_index, batch=batch_idx)
# Forward pass
predictions = model(data)
loss = criterion(predictions.squeeze(), y)
total_loss += loss.item()
num_batches += 1
return total_loss / num_batches if num_batches > 0 else 0.0
def save_model(model, filepath, epoch, train_loss, val_loss):
"""
Save model weights and training metadata.
Args:
model: The model to save
filepath: Path to save the model
epoch: Current epoch number
train_loss: Training loss
val_loss: Validation loss
"""
torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
'train_loss': train_loss,
'val_loss': val_loss,
'model_config': {
'input_dim': 74,
'hidden_dim': model.convs[0].out_channels if hasattr(model.convs[0], 'out_channels') else 64,
'num_layers': model.num_layers,
'conv_type': model.conv_type,
'dropout': model.dropout
}
}, filepath)
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description='Train Ruby complexity prediction GNN model')
parser.add_argument('--dataset_path', type=str, default='dataset/',
help='Path to dataset directory (default: dataset/)')
parser.add_argument('--epochs', type=int, default=100,
help='Number of training epochs (default: 100)')
parser.add_argument('--output_path', type=str, default='models/best_model.pt',
help='Path to save the best model (default: models/best_model.pt)')
parser.add_argument('--batch_size', type=int, default=32,
help='Batch size for training (default: 32)')
parser.add_argument('--learning_rate', type=float, default=0.001,
help='Learning rate (default: 0.001)')
parser.add_argument('--hidden_dim', type=int, default=64,
help='Hidden dimension size (default: 64)')
parser.add_argument('--num_layers', type=int, default=3,
help='Number of GNN layers (default: 3)')
parser.add_argument('--conv_type', type=str, default='SAGE',
choices=['GCN', 'SAGE', 'GAT', 'GIN', 'GraphConv'],
help='GNN convolution type (default: SAGE)')
parser.add_argument('--dropout', type=float, default=0.1,
help='Dropout rate (default: 0.1)')
parser.add_argument('--num_workers', type=int, default=0,
help='DataLoader workers (default: 0 for Docker compat)')
return parser.parse_args()
def main():
"""Main training function."""
args = parse_args()
print("🚀 Ruby Complexity GNN Training")
print("=" * 50)
# Training configuration from args
config = {
'epochs': args.epochs,
'batch_size': args.batch_size,
'learning_rate': args.learning_rate,
'hidden_dim': args.hidden_dim,
'num_layers': args.num_layers,
'conv_type': args.conv_type,
'dropout': args.dropout
}
print("📋 Training Configuration:")
for key, value in config.items():
print(f" {key}: {value}")
print(f" dataset_path: {args.dataset_path}")
print(f" output_path: {args.output_path}")
print()
# Setup device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"🖥️ Using device: {device}")
# Create data loaders
print("📂 Loading datasets...")
# Handle sample dataset naming convention
if args.dataset_path.rstrip('/').endswith('samples'):
train_data_path = os.path.join(args.dataset_path, "train_sample.jsonl")
val_data_path = os.path.join(args.dataset_path, "validation_sample.jsonl")
else:
train_data_path = os.path.join(args.dataset_path, "train.jsonl")
val_data_path = os.path.join(args.dataset_path, "validation.jsonl")
train_loader, val_loader = create_data_loaders(
train_data_path,
val_data_path,
batch_size=config['batch_size'],
shuffle=True,
num_workers=args.num_workers
)
print(f" Training batches: {len(train_loader)}")
print(f" Validation batches: {len(val_loader)}")
print()
# Initialize model
print("🧠 Initializing model...")
model = RubyComplexityGNN(
input_dim=74, # AST node feature dimension
hidden_dim=config['hidden_dim'],
num_layers=config['num_layers'],
conv_type=config['conv_type'],
dropout=config['dropout']
).to(device)
param_count = sum(p.numel() for p in model.parameters())
print(f" Model: {model.get_model_info()}")
print(f" Parameters: {param_count:,}")
print()
# Setup optimizer and loss function
optimizer = torch.optim.Adam(model.parameters(), lr=config['learning_rate'])
criterion = torch.nn.MSELoss()
print("⚙️ Training setup:")
print(f" Optimizer: Adam (lr={config['learning_rate']})")
print(f" Loss function: MSELoss")
print()
# Ensure output directory exists
os.makedirs(os.path.dirname(args.output_path), exist_ok=True)
# Training loop
print("🏋️ Starting training...")
print("=" * 50)
best_val_loss = float('inf')
start_time = time.time()
for epoch in range(config['epochs']):
epoch_start = time.time()
# Train for one epoch
train_loss = train_epoch(model, train_loader, optimizer, criterion, device)
# Validate
val_loss = validate_epoch(model, val_loader, criterion, device)
epoch_time = time.time() - epoch_start
# Print results for each epoch (required by Definition of Done)
print(f"Epoch {epoch+1:2d}/{config['epochs']} | "
f"Train Loss: {train_loss:.4f} | "
f"Val Loss: {val_loss:.4f} | "
f"Time: {epoch_time:.2f}s")
# Save best model (required by Definition of Done)
if val_loss < best_val_loss:
best_val_loss = val_loss
save_model(model, args.output_path, epoch, train_loss, val_loss)
print(f" 💾 New best model saved (val_loss: {val_loss:.4f})")
total_time = time.time() - start_time
print("=" * 50)
print("🎉 Training completed successfully!")
print(f" Total time: {total_time:.2f}s")
print(f" Best validation loss: {best_val_loss:.4f}")
print(f" Best model saved to: {args.output_path}")
# Final model save (optional, keeping for compatibility)
final_path = args.output_path.replace('.pt', '_final.pt')
save_model(model, final_path, config['epochs']-1, train_loss, val_loss)
print(f" Final model saved to: {final_path}")
if __name__ == "__main__":
main() |