clipLower(EPSILON)->log(); return $target->negate()->multiply($entropy)->mean()->mean(); } /** * Calculate the gradient of the cost function with respect to the output. * * @internal * * @param Matrix $output * @param Matrix $target * @return Matrix */ public function differentiate(Matrix $output, Matrix $target) : Matrix { $denominator = Matrix::ones(...$target->shape()) ->subtract($output) ->multiply($output) ->clipLower(EPSILON); return $output->subtract($target) ->divide($denominator); } /** * Return the string representation of the object. * * @internal * * @return string */ public function __toString() : string { return 'Cross Entropy'; } }