NNFS
Neural network library from scratch
Loading...
Searching...
No Matches
Tanh.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Activation.hpp"
4
5namespace NNFS
6{
7 class Tanh : public Activation
8 {
9 public:
11
12 void forward(Eigen::MatrixXd &out, const Eigen::MatrixXd &x) override
13 {
15
16 out = x.array().tanh();
17
18 _forward_output = out;
19 }
20
21 void backward(Eigen::MatrixXd &out, const Eigen::MatrixXd &dx) override
22 {
23 out = (1.0 - _forward_output.array().square()) * dx.array();
24 }
25
26 private:
27 Eigen::MatrixXd _forward_output;
28 };
29} // namespace NNFS
Base class for all activation functions.
Definition Activation.hpp:24
Eigen::MatrixXd _forward_input
Definition Activation.hpp:37
Definition Tanh.hpp:8
void forward(Eigen::MatrixXd &out, const Eigen::MatrixXd &x) override
Forward pass of the layer.
Definition Tanh.hpp:12
void backward(Eigen::MatrixXd &out, const Eigen::MatrixXd &dx) override
Backward pass of the layer.
Definition Tanh.hpp:21
Tanh()
Definition Tanh.hpp:10
Definition Activation.hpp:6
ActivationType
Enum class for activation types.
Definition Activation.hpp:11