NeuralNet 1.0
Loading...
Searching...
No Matches
BCE.hpp
1#pragma once
2
3#include "Loss.hpp"
4#include "utils/Functions.hpp"
5
6namespace NeuralNet {
10class BCE : public Loss {
11 public:
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);
16
17 Eigen::MatrixXd loss =
18 -(yThresh.array() * oThresh.array().log() +
19 (1.0 - yThresh.array()) * (1.0 - oThresh.array()).log());
20
21 if (loss.array().isNaN().any())
22 throw std::runtime_error(
23 "NaN value encountered. Inputs might be too big");
24
25 return loss.sum();
26 }
27
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);
33 }
34};
35
36} // namespace NeuralNet
Definition BCE.hpp:10
Definition Loss.hpp:6