Fix: CrossEntropyLoss numerical stability for 1D inputs

- Fixed axis=1 error when CrossEntropyLoss receives 1D prediction arrays
- Added robust handling for both 1D and 2D prediction inputs
- Reshapes 1D arrays to 2D for consistent processing
- All integration tests now pass (17/17)
- All inline tests pass (6/6)
- tito CLI integration working correctly

Technical improvements:
- Handles single sample predictions correctly
- Maintains backward compatibility with batch inputs
- Prevents numpy axis errors in edge cases
- Ensures consistent shape handling across all loss functions
This commit is contained in:
Vijay Janapa Reddi
2025-07-14 00:57:38 -04:00
parent 356bea2c9e
commit 9896226bc9
2 changed files with 1599 additions and 1 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -366,8 +366,15 @@ class CrossEntropyLoss:
- Use np.log for logarithm computation
"""
### BEGIN SOLUTION
# Handle both 1D and 2D prediction arrays
if y_pred.data.ndim == 1:
# Reshape 1D to 2D for consistency (single sample)
y_pred_2d = y_pred.data.reshape(1, -1)
else:
y_pred_2d = y_pred.data
# Apply softmax to get probability distribution
exp_pred = np.exp(y_pred.data - np.max(y_pred.data, axis=1, keepdims=True))
exp_pred = np.exp(y_pred_2d - np.max(y_pred_2d, axis=1, keepdims=True))
softmax_pred = exp_pred / np.sum(exp_pred, axis=1, keepdims=True)
# Add small epsilon to avoid log(0)