diff --git a/cpp-docs/BCE_8hpp_source.html b/cpp-docs/BCE_8hpp_source.html index 841cc83..e1a1593 100644 --- a/cpp-docs/BCE_8hpp_source.html +++ b/cpp-docs/BCE_8hpp_source.html @@ -86,26 +86,39 @@
1#pragma once
2
3#include "Loss.hpp"
-
4
-
5namespace NeuralNet {
-
-
9class BCE : public Loss {
-
10 public:
-
11 static double cmpLoss(const Eigen::MatrixXd &o, const Eigen::MatrixXd &y) {
-
12 Eigen::MatrixXd loss = -(y.array() * o.array().log() +
-
13 (1.0 - y.array()) * (1.0 - o.array()).log());
-
14 return loss.sum();
-
15 }
+
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 oTrim = trim(o, threshold);
+
15 Eigen::MatrixXd yTrim = trim(y, threshold);
16
-
17 static Eigen::MatrixXd cmpLossGrad(const Eigen::MatrixXd &yHat,
-
18 const Eigen::MatrixXd &y) {
-
19 return (yHat.array() - y.array()) / (yHat.array() * (1.0 - y.array()));
-
20 }
-
21};
+
17 Eigen::MatrixXd loss =
+
18 -(yTrim.array() * oTrim.array().log() +
+
19 (1.0 - yTrim.array()) * (1.0 - oTrim.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};
-
22
-
23} // namespace NeuralNet
-
Definition BCE.hpp:9
+
35
+
36} // namespace NeuralNet
+
Definition BCE.hpp:10
Definition Loss.hpp:6
diff --git a/cpp-docs/Functions_8hpp_source.html b/cpp-docs/Functions_8hpp_source.html index bbcc2da..255f6f1 100644 --- a/cpp-docs/Functions_8hpp_source.html +++ b/cpp-docs/Functions_8hpp_source.html @@ -162,7 +162,7 @@
128
129/* MATHEMATICAL FUNCTIONS */
130
-
138inline double sqr(const double x) { return x * x; };
+
138inline constexpr double sqr(const double x) { return x * x; };
139
140/* VECTOR OPERATIONS */
141
@@ -284,16 +284,21 @@
323 return hardmaxMatrix;
324}
325
-
326/* SIGNAL HANDLING */
-
327static void signalHandler(int signum) {
-
328 std::cout << "Interrupt signal (" << signum << ") received.\n";
-
329
-
330 // cleanup and close up stuff here
-
331 // terminate program
-
332 exit(signum);
-
333};
-
334
-
335} // namespace NeuralNet
+
334static Eigen::MatrixXd trim(const Eigen::MatrixXd &logits,
+
335 double threshold = 0.01) {
+
336 return (logits.array() < threshold).select(0, logits);
+
337}
+
338
+
339/* SIGNAL HANDLING */
+
340static void signalHandler(int signum) {
+
341 std::cout << "Interrupt signal (" << signum << ") received.\n";
+
342
+
343 // cleanup and close up stuff here
+
344 // terminate program
+
345 exit(signum);
+
346};
+
347
+
348} // namespace NeuralNet