clip(EPSILON, 1.0); $output = $output->clip(EPSILON, 1.0); return $target->divide($output)->log() ->multiply($target) ->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 { $target = $target->clip(EPSILON, 1.0); $output = $output->clip(EPSILON, 1.0); return $output->subtract($target) ->divide($output); } /** * Return the string representation of the object. * * @internal * * @return string */ public function __toString() : string { return 'Relative Entropy'; } }