Spaces:
Sleeping
Sleeping
File size: 19,926 Bytes
b4263ca |
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 |
import torch
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, ndcg_score
from typing import Dict, List, Tuple
import json
import os
from train_model import HybridMusicRecommender, MusicRecommenderDataset
from torch.utils.data import DataLoader
import logging
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import ParameterGrid, train_test_split
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('model_evaluation.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
class ModelEvaluator:
def __init__(self, model_path: str, test_data: pd.DataFrame, batch_size: int = 32):
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.model_path = model_path
self.test_data = test_data
self.batch_size = batch_size
# Load model and config
torch.serialization.add_safe_globals([LabelEncoder])
self.checkpoint = torch.load(model_path, map_location=self.device, weights_only=False)
self.config = self.checkpoint['config']
self.encoders = self.checkpoint['encoders']
# Initialize model
self.model = self._initialize_model()
self.test_loader = self._prepare_data()
# Create metrics directory with absolute path
self.metrics_dir = os.path.join(os.path.dirname(model_path), 'metrics')
os.makedirs(self.metrics_dir, exist_ok=True)
def _initialize_model(self, custom_config: Dict = None) -> HybridMusicRecommender:
"""Initialize and load the model from checkpoint."""
# Use custom config if provided, otherwise use default
config = custom_config if custom_config else self.config
model = HybridMusicRecommender(
num_users=len(self.encoders['user_encoder'].classes_),
num_music=len(self.encoders['music_encoder'].classes_),
num_artists=len(self.encoders['artist_encoder'].classes_),
num_genres=len(self.encoders['genre_encoder'].classes_),
num_numerical=12,
embedding_dim=config['embedding_dim'],
layers=config['hidden_layers'],
dropout=config['dropout']
)
# Only load state dict if using default config
if not custom_config:
model.load_state_dict(self.checkpoint['model_state_dict'])
model = model.to(self.device)
model.eval()
return model
def _prepare_data(self) -> DataLoader:
"""Prepare test data loader using saved encoders."""
# Create a custom dataset for test data with the saved encoders
test_dataset = MusicRecommenderDataset(
self.test_data,
mode='test',
encoders=self.encoders
)
logger.info(f"Prepared test dataset with {len(self.test_data)} samples")
return DataLoader(test_dataset, batch_size=self.batch_size, shuffle=False)
def calculate_metrics(self) -> Dict[str, float]:
"""Calculate various performance metrics."""
true_values = []
predictions = []
with torch.no_grad():
for batch in self.test_loader:
batch = {k: v.to(self.device) for k, v in batch.items()}
pred = self.model(batch)
true_values.extend(batch['playcount'].cpu().numpy())
predictions.extend(pred.cpu().numpy())
true_values = np.array(true_values)
predictions = np.array(predictions)
metrics = {
'mse': float(mean_squared_error(true_values, predictions)),
'rmse': float(np.sqrt(mean_squared_error(true_values, predictions))),
'mae': float(mean_absolute_error(true_values, predictions)),
'r2': float(r2_score(true_values, predictions))
}
# Calculate prediction distribution statistics
metrics.update({
'pred_mean': float(np.mean(predictions)),
'pred_std': float(np.std(predictions)),
'true_mean': float(np.mean(true_values)),
'true_std': float(np.std(true_values))
})
return metrics
def analyze_prediction_bias(self) -> Dict[str, float]:
"""Analyze prediction bias across different value ranges."""
true_values = []
predictions = []
with torch.no_grad():
for batch in self.test_loader:
batch = {k: v.to(self.device) for k, v in batch.items()}
pred = self.model(batch)
true_values.extend(batch['playcount'].cpu().numpy())
predictions.extend(pred.cpu().numpy())
true_values = np.array(true_values)
predictions = np.array(predictions)
# Calculate bias for different value ranges
percentiles = np.percentile(true_values, [25, 50, 75])
ranges = [
(float('-inf'), percentiles[0]),
(percentiles[0], percentiles[1]),
(percentiles[1], percentiles[2]),
(percentiles[2], float('inf'))
]
bias_analysis = {}
for i, (low, high) in enumerate(ranges):
mask = (true_values >= low) & (true_values < high)
if np.any(mask):
bias = np.mean(predictions[mask] - true_values[mask])
bias_analysis[f'bias_range_{i+1}'] = float(bias)
return bias_analysis
def plot_prediction_distribution(self, save_dir: str = None):
"""Plot the distribution of predictions vs true values."""
if save_dir is None:
save_dir = self.metrics_dir
true_values = []
predictions = []
with torch.no_grad():
for batch in self.test_loader:
batch = {k: v.to(self.device) for k, v in batch.items()}
pred = self.model(batch)
true_values.extend(batch['playcount'].cpu().numpy())
predictions.extend(pred.cpu().numpy())
true_values = np.array(true_values)
predictions = np.array(predictions)
# Create scatter plot
plt.figure(figsize=(10, 6))
plt.scatter(true_values, predictions, alpha=0.5)
plt.plot([true_values.min(), true_values.max()],
[true_values.min(), true_values.max()],
'r--', lw=2)
plt.xlabel('True Values')
plt.ylabel('Predictions')
plt.title('Prediction vs True Values')
try:
# Save plot with absolute path
plot_path = os.path.join(save_dir, 'prediction_distribution.png')
plt.savefig(plot_path)
plt.close()
logger.info(f"Saved prediction distribution plot to: {plot_path}")
except Exception as e:
logger.error(f"Error saving prediction distribution plot: {str(e)}")
def plot_error_distribution(self, save_dir: str = None):
"""Plot the distribution of prediction errors."""
if save_dir is None:
save_dir = self.metrics_dir
true_values = []
predictions = []
with torch.no_grad():
for batch in self.test_loader:
batch = {k: v.to(self.device) for k, v in batch.items()}
pred = self.model(batch)
true_values.extend(batch['playcount'].cpu().numpy())
predictions.extend(pred.cpu().numpy())
errors = np.array(predictions) - np.array(true_values)
plt.figure(figsize=(10, 6))
sns.histplot(errors, kde=True)
plt.xlabel('Prediction Error')
plt.ylabel('Count')
plt.title('Distribution of Prediction Errors')
try:
plot_path = os.path.join(save_dir, 'error_distribution.png')
plt.savefig(plot_path)
plt.close()
logger.info(f"Saved error distribution plot to: {plot_path}")
except Exception as e:
logger.error(f"Error saving error distribution plot: {str(e)}")
def evaluate_top_k_recommendations(self, k: int = 10) -> Dict[str, float]:
"""Evaluate top-K recommendation metrics."""
user_metrics = []
# Group by user to evaluate per-user recommendations
for user_id in self.test_data['user_id'].unique():
user_mask = self.test_data['user_id'] == user_id
user_data = self.test_data[user_mask]
# Skip users with too few interactions
if len(user_data) < k:
continue
user_dataset = MusicRecommenderDataset(
user_data,
mode='test',
encoders=self.encoders
)
user_loader = DataLoader(user_dataset, batch_size=len(user_data), shuffle=False)
with torch.no_grad():
batch = next(iter(user_loader))
batch = {k: v.to(self.device) for k, v in batch.items()}
predictions = self.model(batch).cpu().numpy()
true_values = batch['playcount'].cpu().numpy()
# Normalize predictions and true values to [0, 1] range
true_values = (true_values - true_values.min()) / (true_values.max() - true_values.min() + 1e-8)
predictions = (predictions - predictions.min()) / (predictions.max() - predictions.min() + 1e-8)
# Calculate metrics for this user
top_k_pred_idx = np.argsort(predictions)[-k:][::-1]
top_k_true_idx = np.argsort(true_values)[-k:][::-1]
# Calculate NDCG
dcg = self._calculate_dcg(true_values, top_k_pred_idx, k)
idcg = self._calculate_dcg(true_values, top_k_true_idx, k)
# Handle edge case where idcg is 0
ndcg = dcg / idcg if idcg > 0 else 0.0
# Calculate precision and recall
relevant_items = set(top_k_true_idx)
recommended_items = set(top_k_pred_idx)
precision = len(relevant_items & recommended_items) / k
recall = len(relevant_items & recommended_items) / len(relevant_items)
user_metrics.append({
'ndcg': ndcg,
'precision': precision,
'recall': recall
})
# Average metrics across users
avg_metrics = {
'ndcg@10': float(np.mean([m['ndcg'] for m in user_metrics])),
'precision@10': float(np.mean([m['precision'] for m in user_metrics])),
'recall@10': float(np.mean([m['recall'] for m in user_metrics]))
}
return avg_metrics
def _calculate_dcg(self, true_values: np.ndarray, indices: np.ndarray, k: int) -> float:
"""Helper method to calculate DCG with numerical stability."""
relevance = true_values[indices[:k]]
# Cap the relevance values to prevent overflow
max_relevance = 10 # Set a reasonable maximum value
relevance = np.clip(relevance, 0, max_relevance)
# Use log2(rank + 1) directly instead of creating array
gains = (2 ** relevance - 1) / np.log2(np.arange(2, len(relevance) + 2))
return float(np.sum(gains))
def evaluate_cold_start(self, min_interactions: int = 5) -> Dict[str, Dict[str, float]]:
"""
Evaluate model performance on cold-start scenarios.
Args:
min_interactions: Minimum number of interactions to consider a user/item as non-cold
Returns:
Dictionary containing metrics for different cold-start scenarios
"""
# Get all unique users and items
all_users = self.test_data['user_id'].unique()
all_items = self.test_data['music_id'].unique()
# Count interactions per user and item
user_counts = self.test_data['user_id'].value_counts()
item_counts = self.test_data['music_id'].value_counts()
# Identify cold users and items
cold_users = set(user_counts[user_counts < min_interactions].index)
cold_items = set(item_counts[item_counts < min_interactions].index)
# Create masks for different scenarios
cold_user_mask = self.test_data['user_id'].isin(cold_users)
cold_item_mask = self.test_data['music_id'].isin(cold_items)
cold_user_warm_item = cold_user_mask & ~cold_item_mask
warm_user_cold_item = ~cold_user_mask & cold_item_mask
cold_both = cold_user_mask & cold_item_mask
warm_both = ~cold_user_mask & ~cold_item_mask
scenarios = {
'cold_user_warm_item': cold_user_warm_item,
'warm_user_cold_item': warm_user_cold_item,
'cold_both': cold_both,
'warm_both': warm_both
}
results = {}
for scenario_name, mask in scenarios.items():
if not any(mask):
logger.warning(f"No samples found for scenario: {scenario_name}")
continue
scenario_data = self.test_data[mask].copy()
# Create a temporary dataset and dataloader for this scenario
scenario_dataset = MusicRecommenderDataset(
scenario_data,
mode='test',
encoders=self.encoders
)
scenario_loader = DataLoader(
scenario_dataset,
batch_size=self.batch_size,
shuffle=False
)
# Collect predictions and true values
true_values = []
predictions = []
with torch.no_grad():
for batch in scenario_loader:
batch = {k: v.to(self.device) for k, v in batch.items()}
pred = self.model(batch)
true_values.extend(batch['playcount'].cpu().numpy())
predictions.extend(pred.cpu().numpy())
true_values = np.array(true_values)
predictions = np.array(predictions)
# Calculate metrics
metrics = {
'count': len(true_values),
'mse': float(mean_squared_error(true_values, predictions)),
'rmse': float(np.sqrt(mean_squared_error(true_values, predictions))),
'mae': float(mean_absolute_error(true_values, predictions)),
'r2': float(r2_score(true_values, predictions)),
'pred_mean': float(np.mean(predictions)),
'pred_std': float(np.std(predictions)),
'true_mean': float(np.mean(true_values)),
'true_std': float(np.std(true_values))
}
results[scenario_name] = metrics
# Log results for this scenario
logger.info(f"\n{scenario_name} Metrics (n={metrics['count']}):")
for metric, value in metrics.items():
if metric != 'count':
logger.info(f"{metric}: {value:.4f}")
return results
def save_evaluation_results(self, save_dir: str = 'metrics'):
"""Run all evaluations and save results."""
os.makedirs(save_dir, exist_ok=True)
# Calculate all metrics
results = {
'basic_metrics': self.calculate_metrics(),
'bias_analysis': self.analyze_prediction_bias(),
'top_k_metrics': self.evaluate_top_k_recommendations(),
'cold_start_metrics': self.evaluate_cold_start(min_interactions=5)
}
# Save results to JSON
results_file = os.path.join(save_dir, 'evaluation_results.json')
with open(results_file, 'w') as f:
json.dump(results, f, indent=4)
logger.info(f"Evaluation completed. Results saved to: {save_dir}")
return results
def tune_hyperparameters(self, param_grid: Dict[str, List], val_data: pd.DataFrame) -> Dict:
"""
Tune hyperparameters using validation set.
Args:
param_grid: Dictionary of parameters to try
val_data: Validation data
Returns:
Best parameters found
"""
best_score = float('inf')
best_params = None
# Create validation dataset
val_dataset = MusicRecommenderDataset(val_data, mode='test', encoders=self.encoders)
val_loader = DataLoader(val_dataset, batch_size=self.batch_size, shuffle=False)
# Try all parameter combinations
for params in ParameterGrid(param_grid):
# Create a new config with updated parameters
current_config = self.config.copy()
current_config.update(params)
# Initialize model with current parameters
self.model = self._initialize_model(custom_config=current_config)
# Evaluate on validation set
metrics = self.calculate_metrics()
score = metrics['rmse'] # Use RMSE as scoring metric
if score < best_score:
best_score = score
best_params = params
logger.info(f"New best parameters found: {params} (RMSE: {score:.4f})")
return best_params
def main():
# Load test data and check for data compatibility
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
test_path = os.path.join(ROOT_DIR, 'data', 'test_data.csv')
model_path = os.path.join(ROOT_DIR, 'data_engineered_v3', 'rs_main_v2_refactored', 'checkpoints', 'best_model.pth')
test_data = pd.read_csv(test_path)
logger.info(f"Loaded test data with {len(test_data)} samples")
# Split test data into validation and test
val_data, test_data = train_test_split(test_data, test_size=0.5, random_state=42)
try:
# Initialize evaluator
evaluator = ModelEvaluator(
model_path=model_path,
test_data=test_data,
batch_size=32
)
# Tune hyperparameters
param_grid = {
'embedding_dim': [32, 64, 128],
'dropout': [0.1, 0.2, 0.3],
'hidden_layers': [[128, 64], [256, 128, 64], [512, 256, 128]]
}
best_params = evaluator.tune_hyperparameters(param_grid, val_data)
logger.info(f"Best parameters: {best_params}")
# Run evaluation
results = evaluator.save_evaluation_results()
# Print summary
logger.info("\nEvaluation Summary:")
logger.info("Basic Metrics:")
for metric, value in results['basic_metrics'].items():
logger.info(f"{metric}: {value:.4f}")
logger.info("\nTop-K Metrics:")
for metric, value in results['top_k_metrics'].items():
logger.info(f"{metric}: {value:.4f}")
logger.info("\nBias Analysis:")
for range_name, bias in results['bias_analysis'].items():
logger.info(f"{range_name}: {bias:.4f}")
except Exception as e:
logger.error(f"Error during evaluation: {str(e)}")
raise
if __name__ == "__main__":
main()
|