Proyecto Final - Turinmachin
Recreación del minijuego de matemáticas de Brain-Age usando redes neuronales
Loading...
Searching...
No Matches
activation.h
Go to the documentation of this file.
1#ifndef PROG3_NN_FINAL_PROJECT_V2025_01_ACTIVATION_H
2#define PROG3_NN_FINAL_PROJECT_V2025_01_ACTIVATION_H
3
4#include <algorithm>
5#include <cmath>
6#include "interfaces.h"
7
15 template <typename T>
16 class ReLU final : public ILayer<T> {
17 algebra::Tensor<T, 2> input{0, 0};
18
19 public:
28 input = z;
29 return z.apply([](const T t) { return std::max(static_cast<T>(0), t); });
30 }
31
43 return input.apply([](const T t) { return T(t > 0); }) * g;
44 }
45
51 [[nodiscard]] auto id() const -> LayerId override {
52 return LayerId::ReLU;
53 }
54 };
55
62 template <typename T>
63 class Sigmoid final : public ILayer<T> {
65 algebra::Tensor<T, 2> output{0, 0};
66
67 public:
75 output = z.apply([](const T t) { return 1 / (1 + std::exp(-t)); });
76 return output;
77 }
78
89 return output.apply([](const T t) { return t * (1 - t); }) * g;
90 }
91
97 [[nodiscard]] auto id() const -> LayerId override {
98 return LayerId::Sigmoid;
99 }
100 };
101
110 template <typename T>
111 class Softmax final : public ILayer<T> {
113 algebra::Tensor<T, 2> output{0, 0};
114
115 public:
125 output = algebra::Tensor<T, 2>(z.shape());
126
127 for (std::size_t i = 0; i < z.shape()[0]; ++i) {
128 // T max_val = *std::max_element(z.row(i).begin(), z.row(i).end());
129
130 T sum_exp = 0;
131 for (std::size_t j = 0; j < z.shape()[1]; ++j) {
132 output(i, j) = std::exp(z(i, j));
133 sum_exp += output(i, j);
134 }
135
136 for (std::size_t j = 0; j < z.shape()[1]; ++j) {
137 output(i, j) /= sum_exp;
138 }
139 }
140
141 return output;
142 }
143
155 algebra::Tensor<T, 2> grad(output.shape());
156
157 for (std::size_t i = 0; i < output.shape()[0]; ++i) {
158 for (std::size_t j = 0; j < output.shape()[1]; ++j) {
159 T sum = 0;
160 for (std::size_t k = 0; k < output.shape()[1]; ++k) {
161 const T delta = (j == k) ? 1 : 0;
162 sum += g(i, k) * output(i, j) * (delta - output(i, k));
163 }
164 grad(i, j) = sum;
165 }
166 }
167
168 return grad;
169 }
170
176 [[nodiscard]] auto id() const -> LayerId override {
177 return LayerId::Softmax;
178 }
179 };
180
181} // namespace utec::neural_network
182
183#endif
Representa un tensor de tipo T y rango Rank.
Definition tensor.h:63
Definition activation.h:16
auto forward(const algebra::Tensor< T, 2 > &z) -> algebra::Tensor< T, 2 > override
Propagación hacia adelante aplicando la función ReLU a cada elemento.
Definition activation.h:27
auto id() const -> LayerId override
Identificador único de la capa ReLU.
Definition activation.h:51
auto backward(const algebra::Tensor< T, 2 > &g) -> algebra::Tensor< T, 2 > override
Propagación hacia atrás del gradiente de ReLU. Devuelve 1 si el valor de entrada fue positivo,...
Definition activation.h:42
Capa de activación Sigmoid. Convierte cada valor en el rango (0, 1) abierto usando la función logísti...
Definition activation.h:63
auto forward(const algebra::Tensor< T, 2 > &z) -> algebra::Tensor< T, 2 > override
Propagación hacia adelante aplicando sigmoide.
Definition activation.h:74
auto id() const -> LayerId override
Identificador único de la capa Sigmoid.
Definition activation.h:97
auto backward(const algebra::Tensor< T, 2 > &g) -> algebra::Tensor< T, 2 > override
Propagación hacia atrás: gradiente de la función sigmoide.
Definition activation.h:88
Capa de activación Softmax. Convierte un vector de valores en probabilidades las cualesal ser sumadas...
Definition activation.h:111
auto backward(const algebra::Tensor< T, 2 > &g) -> algebra::Tensor< T, 2 > override
Propagación hacia atrás con el gradiente de Softmax. Usa la derivada de Softmax con respecto a su ent...
Definition activation.h:154
auto forward(const algebra::Tensor< T, 2 > &z) -> algebra::Tensor< T, 2 > override
Propagación hacia adelante: aplica Softmax por fila.
Definition activation.h:124
auto id() const -> LayerId override
Identificador único de la capa Softmax.
Definition activation.h:176
Capa de activación de Rectified Linear Unit (ReLU). Los valores negativos del input se convierten en ...
Definition activation.h:14
LayerId
Identificador para los diferentes tipos de capas en la red neuronal. Se emplea uint8_t (unsigned 8-bi...
Definition interfaces.h:11
@ Sigmoid
Definition interfaces.h:13
@ Softmax
Definition interfaces.h:15
@ ReLU
Definition interfaces.h:12
Interfaz para una capa de la red neuronal. Permite que distintas capas se conecten entre sí con polim...
Definition interfaces.h:52