FlexNN 1
Fully connected neural network built from scratch with flexible n-layer design and multiple activations.
Loading...
Searching...
No Matches
Activation.hpp
Go to the documentation of this file.
1
21#pragma once
22
23#include <cstdint>
24#include <string_view>
25#include <type_traits>
26
27#include <Eigen/Dense>
28
30
32
41enum class Activation : uint8_t {
42 None = 0,
43 ReLU = 1,
44 LeakyReLU = 2,
45 Sigmoid = 3,
46 Tanh = 4,
47 Softmax = 5
48};
49static_assert(sizeof(Activation) == 1, "Activation must be 1 byte for model.bin encoding");
50static_assert(std::is_same_v<std::underlying_type_t<Activation>, uint8_t>,
51 "Activation underlying type must be uint8_t");
52
60[[nodiscard]] const char* to_string(Activation a) noexcept;
61
79[[nodiscard]] bool try_parse(std::string_view s, Activation& out) noexcept;
80
81namespace detail {
82
96Eigen::MatrixXd forward(Activation act, const Eigen::MatrixXd& Z,
98
117Eigen::MatrixXd backward(Activation act, const Eigen::MatrixXd& dA,
118 const Eigen::MatrixXd& Z, const Eigen::MatrixXd& A,
119 const ActivationParameters& params = ActivationParameters{}) noexcept;
120
128Eigen::MatrixXd softmax_forward(const Eigen::MatrixXd& Z);
129
144Eigen::MatrixXd softmax_backward_hidden(const Eigen::MatrixXd& s,
145 const Eigen::MatrixXd& upstream) noexcept;
146
147} // namespace detail
148
149} // namespace FlexNN::Activations
Per-activation hyperparameters for FlexNN::Activations.
Eigen::MatrixXd softmax_forward(const Eigen::MatrixXd &Z)
Stable column-wise softmax forward.
Definition Softmax.hpp:23
Eigen::MatrixXd forward(Activation act, const Eigen::MatrixXd &Z, const ActivationParameters &params=ActivationParameters{})
Elementwise forward for a single activation.
Definition Activation.cpp:97
Eigen::MatrixXd softmax_backward_hidden(const Eigen::MatrixXd &s, const Eigen::MatrixXd &upstream) noexcept
Softmax hidden backward: dZ = J * upstream per column.
Definition Softmax.hpp:46
Eigen::MatrixXd backward(Activation act, const Eigen::MatrixXd &dA, const Eigen::MatrixXd &Z, const Eigen::MatrixXd &A, const ActivationParameters &params=ActivationParameters{}) noexcept
Elementwise backward for a single activation (hidden layers).
Definition Activation.cpp:117
Definition Activation.hpp:31
const char * to_string(Activation a) noexcept
Convert Activation to canonical lower-case string.
Definition Activation.cpp:24
Activation
Activation function identifier.
Definition Activation.hpp:41
@ Sigmoid
1/(1+exp(-z)), clamped
@ Softmax
Stable col-wise softmax — only on last layer in v0.1.
@ None
Linear, no activation.
@ LeakyReLU
z>0 ? z : alpha*z (alpha from ActivationParameters)
bool try_parse(std::string_view s, Activation &out) noexcept
Parse a string into an Activation.
Definition Activation.cpp:43
Hyperparameters for activations.
Definition ActivationParameters.hpp:30