TheAlgorithms/Python · error · ValueError
Shape of y_true and y_pred must be the same.
Error message
Shape of y_true and y_pred must be the same.
What it means
Thrown by categorical_focal_cross_entropy when the y_true (one-hot labels) and y_pred (predicted probabilities) matrices have different shapes. The loss is computed element-wise (alpha * (1 - y_pred)^gamma * y_true * log(y_pred)), so both inputs must have identical dimensions (samples x classes). Any shape divergence makes the element-wise product undefined, hence the early validation.
Source
Thrown at machine_learning/loss_functions.py:228
ValueError: y_true must be one-hot encoded.
>>> true_labels = np.array([[1, 0, 0], [0, 1, 0]])
>>> pred_probs = np.array([[0.9, 0.1, 0.1], [0.2, 0.7, 0.1]])
>>> categorical_focal_cross_entropy(true_labels, pred_probs)
Traceback (most recent call last):
...
ValueError: Predicted probabilities must sum to approximately 1.
>>> true_labels = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
>>> pred_probs = np.array([[0.9, 0.1, 0.0], [0.2, 0.7, 0.1], [0.0, 0.1, 0.9]])
>>> alpha = np.array([0.6, 0.2])
>>> categorical_focal_cross_entropy(true_labels, pred_probs, alpha)
Traceback (most recent call last):
...
ValueError: Length of alpha must match the number of classes.
"""
if y_true.shape != y_pred.shape:
raise ValueError("Shape of y_true and y_pred must be the same.")
if alpha is None:
alpha = np.ones(y_true.shape[1])
if np.any((y_true != 0) & (y_true != 1)) or np.any(y_true.sum(axis=1) != 1):
raise ValueError("y_true must be one-hot encoded.")
if len(alpha) != y_true.shape[1]:
raise ValueError("Length of alpha must match the number of classes.")
if not np.all(np.isclose(np.sum(y_pred, axis=1), 1, rtol=epsilon, atol=epsilon)):
raise ValueError("Predicted probabilities must sum to approximately 1.")
# Clip predicted probabilities to avoid log(0)
y_pred = np.clip(y_pred, epsilon, 1 - epsilon)
# Calculate loss for each class and sum across classes
cfce_loss = -np.sum(View on GitHub (pinned to f5988cc097)
Solutions
- Print y_true.shape and y_pred.shape right before the call and make them equal, typically (num_samples, num_classes).
- One-hot encode y_true with the same num_classes as y_pred's last axis (e.g. np.eye(num_classes)[y_true_int]).
- If y_pred was transposed or reshaped during preprocessing, fix the reshaping so rows are samples and columns are classes.
- Verify the model's final dense layer size matches the number of classes in the label encoder.
Example fix
# before y_true = np.array([0, 2, 1]) # shape (3,) loss = categorical_focal_cross_entropy(y_true, y_pred) # y_pred shape (3, 3) # after y_true = np.eye(y_pred.shape[1])[y_true] # shape (3, 3) loss = categorical_focal_cross_entropy(y_true, y_pred)
Defensive patterns
Strategy: validation
Validate before calling
if y_true.shape != y_pred.shape:
raise ValueError(f"shape mismatch: {y_true.shape} vs {y_pred.shape}")
loss = categorical_focal_cross_entropy(y_true, y_pred, alpha) Type guard
def is_valid_cfce_input(y_true: np.ndarray, y_pred: np.ndarray) -> bool:
return y_true.ndim == 2 and y_true.shape == y_pred.shape Try / catch
try:
loss = categorical_focal_cross_entropy(y_true, y_pred, alpha)
except ValueError as e:
logger.error("focal loss input invalid: %s; shapes %s vs %s", e, y_true.shape, y_pred.shape)
raise Prevention
- One-hot encode labels with np.eye(num_classes) where num_classes equals y_pred.shape[1].
- Assert identical shapes in the data-prep pipeline before training loops.
- Keep model output layer size and label encoder class count derived from one shared constant.
When it happens
Trigger: Calling categorical_focal_cross_entropy(y_true, y_pred) where y_true.shape != y_pred.shape, e.g. labels one-hot encoded over 3 classes but predictions over 4 classes, or a 1-D label array paired with a 2-D probability matrix.
Common situations: Mismatch between the number of output units of the model and the one-hot encoder; forgetting to one-hot encode y_true (passing integer class labels of shape (N,) against y_pred of shape (N, C)); transposed predictions; batch slicing that drops a dimension.
Related errors
- Input data set must be one-dimensional
- Data set labels must be one-dimensional
- Input arrays must have the same shape.
- Length of predicted and actual array must be same.
- Data must have dimensions N x 1
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/0a8f9344b190fb49.
Report an issue: GitHub.