|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rubix\ML\NeuralNet\CostFunctions\CrossEntropy; |
| 6 | + |
| 7 | +use NDArray; |
| 8 | +use NumPower; |
| 9 | +use Rubix\ML\NeuralNet\CostFunctions\Base\Contracts\ClassificationLoss; |
| 10 | +use Rubix\ML\Traits\AssertsShapes; |
| 11 | +use const Rubix\ML\EPSILON; |
| 12 | + |
| 13 | +/** |
| 14 | + * Cross Entropy |
| 15 | + * |
| 16 | + * Cross Entropy, or log loss, measures the performance of a classification model |
| 17 | + * whose output is a probability value between 0 and 1. Cross-entropy loss |
| 18 | + * increases as the predicted probability diverges from the actual label. So |
| 19 | + * predicting a probability of .012 when the actual observation label is 1 would |
| 20 | + * be bad and result in a high loss value. A perfect score would have a log loss |
| 21 | + * of 0. |
| 22 | + * |
| 23 | + * @category Machine Learning |
| 24 | + * @package Rubix/ML |
| 25 | + * @author Andrew DalPino |
| 26 | + * @author Samuel Akopyan <[email protected]> |
| 27 | + */ |
| 28 | +class CrossEntropy implements ClassificationLoss |
| 29 | +{ |
| 30 | + use AssertsShapes; |
| 31 | + |
| 32 | + /** |
| 33 | + * Compute the loss score. |
| 34 | + * |
| 35 | + * L(y, ŷ) = -Σ(y * log(ŷ)) / n |
| 36 | + * |
| 37 | + * @param NDArray $output The output of the network |
| 38 | + * @param NDArray $target The target values |
| 39 | + * @return float |
| 40 | + */ |
| 41 | + public function compute(NDArray $output, NDArray $target) : float |
| 42 | + { |
| 43 | + $this->assertSameShape($output, $target); |
| 44 | + |
| 45 | + // Clip values to avoid log(0) |
| 46 | + $output = NumPower::clip($output, EPSILON, 1.0); |
| 47 | + |
| 48 | + $logOutput = NumPower::log($output); |
| 49 | + $product = NumPower::multiply($target, $logOutput); |
| 50 | + $negated = NumPower::multiply($product, -1.0); |
| 51 | + |
| 52 | + return NumPower::mean($negated); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Calculate the gradient of the cost function with respect to the output. |
| 57 | + * |
| 58 | + * ∂L/∂ŷ = (ŷ - y) / (ŷ * (1 - ŷ)) |
| 59 | + * |
| 60 | + * @param NDArray $output The output of the network |
| 61 | + * @param NDArray $target The target values |
| 62 | + * @return NDArray |
| 63 | + */ |
| 64 | + public function differentiate(NDArray $output, NDArray $target) : NDArray |
| 65 | + { |
| 66 | + $this->assertSameShape($output, $target); |
| 67 | + |
| 68 | + // Numerator = ŷ - y (calculate before clipping to preserve zeros) |
| 69 | + $numerator = NumPower::subtract($output, $target); |
| 70 | + |
| 71 | + // Clip values to avoid division by zero |
| 72 | + $output = NumPower::clip($output, EPSILON, 1.0 - EPSILON); |
| 73 | + |
| 74 | + // Denominator = ŷ * (1 - ŷ) |
| 75 | + $oneMinusOutput = NumPower::subtract(1.0, $output); |
| 76 | + $denominator = NumPower::multiply($output, $oneMinusOutput); |
| 77 | + $denominator = NumPower::clip($denominator, EPSILON, 1.0); |
| 78 | + |
| 79 | + return NumPower::divide($numerator, $denominator); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Return the string representation of the object. |
| 84 | + * |
| 85 | + * @return string |
| 86 | + */ |
| 87 | + public function __toString() : string |
| 88 | + { |
| 89 | + return 'Cross Entropy'; |
| 90 | + } |
| 91 | +} |
0 commit comments