1#ifndef PROG3_NN_FINAL_PROJECT_V2025_01_ACTIVATION_H
2#define PROG3_NN_FINAL_PROJECT_V2025_01_ACTIVATION_H
29 return z.apply([](
const T t) {
return std::max(
static_cast<T
>(0), t); });
43 return input.apply([](
const T t) {
return T(t > 0); }) * g;
65 algebra::Tensor<T, 2> output{0, 0};
75 output = z.apply([](
const T t) {
return 1 / (1 + std::exp(-t)); });
89 return output.apply([](
const T t) {
return t * (1 - t); }) * g;
110 template <
typename T>
127 for (std::size_t i = 0; i < z.shape()[0]; ++i) {
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);
136 for (std::size_t j = 0; j < z.shape()[1]; ++j) {
137 output(i, j) /= sum_exp;
157 for (std::size_t i = 0; i < output.shape()[0]; ++i) {
158 for (std::size_t j = 0; j < output.shape()[1]; ++j) {
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));
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