How to beginner · 3 min read

How to evaluate regression model in python

Quick answer
Use PyTorch to evaluate regression models by computing metrics such as Mean Squared Error (MSE), Mean Absolute Error (MAE), and R2 score. These metrics quantify prediction errors and model fit, enabling effective performance assessment.

PREREQUISITES

  • Python 3.8+
  • pip install torch scikit-learn

Setup

Install required libraries torch and scikit-learn for model evaluation metrics.

bash
pip install torch scikit-learn

Step by step

This example shows how to evaluate a regression model in PyTorch using Mean Squared Error (MSE), Mean Absolute Error (MAE), and R2 score from scikit-learn. We simulate predictions and targets for demonstration.

python
import torch
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score

# Simulated true values and predictions
true_values = torch.tensor([3.0, -0.5, 2.0, 7.0])
predictions = torch.tensor([2.5, 0.0, 2.1, 7.8])

# Convert tensors to numpy arrays for sklearn metrics
true_np = true_values.numpy()
pred_np = predictions.numpy()

# Calculate metrics
mse = mean_squared_error(true_np, pred_np)
mae = mean_absolute_error(true_np, pred_np)
r2 = r2_score(true_np, pred_np)

print(f"Mean Squared Error (MSE): {mse:.4f}")
print(f"Mean Absolute Error (MAE): {mae:.4f}")
print(f"R2 Score: {r2:.4f}")
output
Mean Squared Error (MSE): 0.2150
Mean Absolute Error (MAE): 0.3500
R2 Score: 0.9220

Common variations

You can compute evaluation metrics directly in PyTorch using torch.nn.functional.mse_loss for MSE or implement MAE manually. For large datasets, batch-wise evaluation is recommended. Also, consider using torchmetrics library for integrated metric computation in training loops.

python
import torch
import torch.nn.functional as F

# MSE using PyTorch
mse_torch = F.mse_loss(predictions, true_values)

# MAE manual calculation
mae_torch = torch.mean(torch.abs(predictions - true_values))

print(f"PyTorch MSE: {mse_torch.item():.4f}")
print(f"PyTorch MAE: {mae_torch.item():.4f}")
output
PyTorch MSE: 0.2150
PyTorch MAE: 0.3500

Troubleshooting

If you get errors converting tensors to numpy arrays, ensure tensors are on CPU by calling tensor.cpu().numpy(). For GPU tensors, direct .numpy() calls fail. Also, verify that predictions and targets have matching shapes.

python
import torch

# Example fix for GPU tensor
predictions_gpu = torch.tensor([2.5, 0.0, 2.1, 7.8], device='cuda')
true_values_gpu = torch.tensor([3.0, -0.5, 2.0, 7.0], device='cuda')

pred_np = predictions_gpu.cpu().numpy()
true_np = true_values_gpu.cpu().numpy()

print(pred_np, true_np)
output
[2.5 0.  2.1 7.8] [ 3.  -0.5  2.   7. ]

Key Takeaways

  • Use sklearn metrics like MSE, MAE, and R2 for straightforward regression evaluation in Python.
  • Convert PyTorch tensors to CPU numpy arrays before using sklearn metrics to avoid errors.
  • PyTorch's native functions can compute MSE and MAE directly for integrated workflows.
  • Ensure predictions and targets have matching shapes and device placement before evaluation.
Verified 2026-04
Verify ↗