Proyecto Final - Turinmachin
Recreación del minijuego de matemáticas de Brain-Age usando redes neuronales
Loading...
Searching...
No Matches
loss.h
Go to the documentation of this file.
1#ifndef PROG3_NN_FINAL_PROJECT_V2025_01_LOSS_H
2#define PROG3_NN_FINAL_PROJECT_V2025_01_LOSS_H
3
4#include <cmath>
5
6#include "interfaces.h"
7
8namespace utec::neural_network {
9
17 template <typename T>
18 class MSELoss final : public ILoss<T, 2> {
20 algebra::Tensor<T, 2> y_prediction;
21
24
25 public:
33 template <typename Prediction, typename Expected>
34 MSELoss(Prediction&& y_prediction, Expected&& y_true)
35 : y_prediction(std::forward<Prediction>(y_prediction)),
36 y_true(std::forward<Expected>(y_true)) {
37 if (y_prediction.shape() != y_true.shape()) {
38 throw std::invalid_argument("algebra::Tensors have incompatible shapes");
39 }
40 }
41
47 auto loss() const -> T override {
48 T sum = 0;
49 const size_t num_elements = y_true.size();
50 for (size_t i = 0; i < num_elements; ++i) {
51 T difference = y_prediction[i] - y_true[i];
52 sum += difference * difference;
53 }
54 return sum / static_cast<T>(num_elements);
55 }
56
62 auto loss_gradient() const -> algebra::Tensor<T, 2> override {
63 return T(2) / y_true.size() * (y_prediction - y_true);
64 }
65 };
66
72 template <typename T>
73 class BCELoss final : public ILoss<T, 2> {
74 algebra::Tensor<T, 2> y_prediction;
76
77 public:
85 template <typename Prediction, typename Expected>
86 BCELoss(Prediction&& y_prediction, Expected&& y_true)
87 : y_prediction(std::forward<Prediction>(y_prediction)),
88 y_true(std::forward<Expected>(y_true)) {
89 if (y_prediction.shape() != y_true.shape()) {
90 throw std::invalid_argument("algebra::Tensors have incompatible shapes");
91 }
92 }
93
99 auto loss() const -> T override {
100 T sum = 0;
101 const size_t num_elements = y_true.size();
102 for (size_t i = 0; i < num_elements; ++i) {
103 sum += y_true[i] * std::log(y_prediction[i]) +
104 (1 - y_true[i]) * std::log(1 - y_prediction[i]);
105 }
106 return -sum / static_cast<T>(num_elements);
107 }
108
114 auto loss_gradient() const -> algebra::Tensor<T, 2> override {
115 return -((y_true / y_prediction) -
116 ((static_cast<T>(1) - y_true) / (static_cast<T>(1) - y_prediction))) /
117 y_true.size();
118 }
119 };
120
126 template <typename T>
127 class CrossEntropyLoss final : public ILoss<T, 2> {
128 algebra::Tensor<T, 2> y_prediction;
130 T epsilon;
131
132 public:
140 template <typename Prediction, typename Expected>
141 CrossEntropyLoss(Prediction&& y_prediction, Expected&& y_true, const T epsilon = 1e-7)
142 : y_prediction(std::forward<Prediction>(y_prediction)),
143 y_true(std::forward<Expected>(y_true)),
144 epsilon(epsilon) {
145 if (y_prediction.shape() != y_true.shape()) {
146 throw std::invalid_argument("algebra::Tensors have incompatible shapes");
147 }
148 }
149
156 auto loss() const -> T override {
157 T sum = 0;
158 const std::size_t num_samples = y_true.shape()[0];
159 const std::size_t num_classes = y_true.shape()[1];
160
161 for (std::size_t i = 0; i < num_samples; ++i) {
162 for (std::size_t j = 0; j < num_classes; ++j) {
163 const T pred = std::clamp(y_prediction(i, j), epsilon, 1 - epsilon);
164 sum += y_true(i, j) * std::log(pred);
165 }
166 }
167
168 return -sum / num_samples;
169 }
170
177 auto loss_gradient() const -> algebra::Tensor<T, 2> override {
178 const std::size_t num_samples = y_true.shape()[0];
179 algebra::Tensor<T, 2> grad = y_prediction - y_true;
180 return grad / num_samples;
181 }
182 };
183
184} // namespace utec::neural_network
185
186#endif
Representa un tensor de tipo T y rango Rank.
Definition tensor.h:63
auto size() const -> size_t
Definition tensor.h:175
auto shape() const noexcept -> const std::array< size_t, Rank > &
Definition tensor.h:179
Representa un tensor de tipo T y rango Rank.
Definition tensor.h:63
auto loss_gradient() const -> algebra::Tensor< T, 2 > override
Gradiente de la pérdida BCE con respecto a las predicciones.
Definition loss.h:114
auto loss() const -> T override
Devuelve el valor de la pérdida BCE.
Definition loss.h:99
BCELoss(Prediction &&y_prediction, Expected &&y_true)
Constructor con predicciones y etiquetas verdaderas.
Definition loss.h:86
CrossEntropyLoss(Prediction &&y_prediction, Expected &&y_true, const T epsilon=1e-7)
Constructor que recibe tensores de predicciones y etiquetas reales.
Definition loss.h:141
auto loss() const -> T override
Valor de la pérdida Cross Entropy. Aplica logaritmo y protección contra valores extremos.
Definition loss.h:156
auto loss_gradient() const -> algebra::Tensor< T, 2 > override
Gradiente de la pérdida Cross Entropy. Simplemente calcula la diferencia entre predicción y etiqueta.
Definition loss.h:177
auto loss() const -> T override
Devuelve el valor de la pérdida MSE.
Definition loss.h:47
MSELoss(Prediction &&y_prediction, Expected &&y_true)
Constructor que recibe predicciones y valores reales.
Definition loss.h:34
auto loss_gradient() const -> algebra::Tensor< T, 2 > override
Gradiente de la pérdida con respecto a las predicciones.
Definition loss.h:62
Definition tensor.h:49
Capa de activación de Rectified Linear Unit (ReLU). Los valores negativos del input se convierten en ...
Definition activation.h:14
Interfaz para una función de pérdida (loss). Se encarga de calcular qué tan mal lo hizo la red con re...
Definition interfaces.h:102