mirror of
https://github.com/MLSysBook/TinyTorch.git
synced 2026-06-01 15:50:58 -05:00
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:
1591
modules/source/09_training/training_dev.ipynb
Normal file
1591
modules/source/09_training/training_dev.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user