FlexNN 1
Fully connected neural network built from scratch with flexible n-layer design and multiple activations.
Loading...
Searching...
No Matches
Tanh.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <Eigen/Dense>
9#include <cmath>
10
12
16inline Eigen::MatrixXd tanh_forward(const Eigen::MatrixXd& Z) {
17 return Z.unaryExpr([](double x) { return std::tanh(x); });
18}
19
25inline Eigen::MatrixXd tanh_backward(const Eigen::MatrixXd& dA,
26 const Eigen::MatrixXd& /*Z*/,
27 const Eigen::MatrixXd& A) noexcept {
28 return dA.array() * (1.0 - A.array().square());
29}
30
31} // namespace FlexNN::Activations::detail
Definition Activation.hpp:81
Eigen::MatrixXd tanh_backward(const Eigen::MatrixXd &dA, const Eigen::MatrixXd &, const Eigen::MatrixXd &A) noexcept
Tanh backward: dZ = dA * (1 - A^2).
Definition Tanh.hpp:25
Eigen::MatrixXd tanh_forward(const Eigen::MatrixXd &Z)
Tanh forward: A = tanh(Z) elementwise.
Definition Tanh.hpp:16