12 static double cmpLoss(
const Eigen::MatrixXd &o,
const Eigen::MatrixXd &y) {
13 constexpr double threshold = 1.0e-5;
14 Eigen::MatrixXd oThresh = thresh(o, threshold);
15 Eigen::MatrixXd yThresh = thresh(y, threshold);
17 Eigen::MatrixXd loss =
18 -(yThresh.array() * oThresh.array().log() +
19 (1.0 - yThresh.array()) * (1.0 - oThresh.array()).log());
21 if (loss.array().isNaN().any())
22 throw std::runtime_error(
23 "NaN value encountered. Inputs might be too big");
28 static Eigen::MatrixXd cmpLossGrad(
const Eigen::MatrixXd &yHat,
29 const Eigen::MatrixXd &y) {
30 constexpr double epsilon = 1.0e-9;
31 return (yHat.array() - y.array()) /
32 ((yHat.array() * (1.0 - yHat.array())) + epsilon);