NNFS
Neural network library from scratch
Loading...
Searching...
No Matches
NNFS::Dense Class Reference

Dense layer. More...

#include <Dense.hpp>

Inheritance diagram for NNFS::Dense:
[legend]

Public Member Functions

 Dense (int n_input, int n_output, double l1_weights_regularizer=.0, double l1_biases_regularizer=.0, double l2_weights_regularizer=.0, double l2_biases_regularizer=.0)
 Construct a new Dense object.
 
void forward (Eigen::MatrixXd &out, const Eigen::MatrixXd &x)
 Forward pass of the dense layer.
 
void backward (Eigen::MatrixXd &out, const Eigen::MatrixXd &dx)
 Backward pass of the dense layer.
 
const Eigen::MatrixXd & weights () const
 Get weights.
 
const Eigen::MatrixXd & biases () const
 Get biases.
 
const Eigen::MatrixXd & dweights () const
 Get weights gradients.
 
const Eigen::MatrixXd & dbiases () const
 Get biases gradients.
 
void weights (Eigen::MatrixXd &weights)
 Set's the weights of the dense layer.
 
void biases (Eigen::MatrixXd &biases)
 Set's the biases of the dense layer.
 
void weights_optimizer (Eigen::MatrixXd woptimizer)
 Set's the weights optimizer matrix of the dense layer.
 
void biases_optimizer (Eigen::MatrixXd boptimizer)
 Set's the biases optimizer matrix of the dense layer.
 
const Eigen::MatrixXd & weights_optimizer () const
 Get weights optimizer matrix.
 
const Eigen::MatrixXd & biases_optimizer () const
 Get biases optimizer matrix.
 
void weights_optimizer_additional (Eigen::MatrixXd woptimizer)
 Set's the additional weights optimizer matrix of the dense layer.
 
void biases_optimizer_additional (Eigen::MatrixXd boptimizer)
 Set's the additional biases optimizer matrix of the dense layer.
 
const Eigen::MatrixXd & weights_optimizer_additional () const
 Get additional weights optimizer matrix.
 
const Eigen::MatrixXd & biases_optimizer_additional () const
 Get additional biases optimizer matrix.
 
const double & l1_weights_regularizer () const
 Get L1 weights regularizer.
 
const double & l2_weights_regularizer () const
 Get L2 weights regularizer.
 
const double & l1_biases_regularizer () const
 Get L1 biases regularizer.
 
const double & l2_biases_regularizer () const
 Get L2 biases regularizer.
 
int parameters () const
 Calculates the number of trainable of the dense layer.
 
void shape (int &n_input, int &n_output) const
 Gives the shape of the dense layer.
 
- Public Member Functions inherited from NNFS::Layer
 Layer (LayerType type)
 Construct a new Layer object.
 
virtual ~Layer ()=default
 Basic destructor.
 
virtual void forward (Eigen::MatrixXd &out, const Eigen::MatrixXd &x)=0
 Forward pass of the layer.
 
virtual void backward (Eigen::MatrixXd &out, const Eigen::MatrixXd &dx)=0
 Backward pass of the layer.
 

Additional Inherited Members

- Public Attributes inherited from NNFS::Layer
LayerType type
 

Detailed Description

Dense layer.

This class implements the dense layer. It is the most basic layer in a neural network.

Constructor & Destructor Documentation

◆ Dense()

NNFS::Dense::Dense ( int  n_input,
int  n_output,
double  l1_weights_regularizer = .0,
double  l1_biases_regularizer = .0,
double  l2_weights_regularizer = .0,
double  l2_biases_regularizer = .0 
)
inline

Construct a new Dense object.

This constructor initializes the weights and biases of the layer. It also sets all regularization matrices to zero.

Parameters
n_inputNumber of input neurons
n_outputNumber of output neurons
l1_weights_regularizerL1 regularization for weights (default: 0.0)
l1_biases_regularizerL1 regularization for biases (default: 0.0)
l2_weights_regularizerL2 regularization for weights (default: 0.0)
l2_biases_regularizerL2 regularization for biases (default: 0.0)

Member Function Documentation

◆ backward()

void NNFS::Dense::backward ( Eigen::MatrixXd &  out,
const Eigen::MatrixXd &  dx 
)
inlinevirtual

Backward pass of the dense layer.

Parameters
[out]outInput gradient
[in]dxOutput gradient

Implements NNFS::Layer.

◆ biases() [1/2]

const Eigen::MatrixXd & NNFS::Dense::biases ( ) const
inline

Get biases.

Returns
Eigen::MatrixXd& Biases

◆ biases() [2/2]

void NNFS::Dense::biases ( Eigen::MatrixXd &  biases)
inline

Set's the biases of the dense layer.

This function sets the biases of the dense layer. The shape of the new biases matrix must match the shape of the initial biases matrix.

Parameters
[in]biasesNew biases matrix
Exceptions
std::invalid_argumentif the shape of the new biases matrix does not match the shape of the initial biases matrix.

◆ biases_optimizer() [1/2]

const Eigen::MatrixXd & NNFS::Dense::biases_optimizer ( ) const
inline

Get biases optimizer matrix.

Returns
Eigen::MatrixXd& Biases optimizer matrix

◆ biases_optimizer() [2/2]

void NNFS::Dense::biases_optimizer ( Eigen::MatrixXd  boptimizer)
inline

Set's the biases optimizer matrix of the dense layer.

Parameters
boptimizerNew biases optimizer matrix

◆ biases_optimizer_additional() [1/2]

const Eigen::MatrixXd & NNFS::Dense::biases_optimizer_additional ( ) const
inline

Get additional biases optimizer matrix.

Returns
Eigen::MatrixXd& Additional biases optimizer matrix

◆ biases_optimizer_additional() [2/2]

void NNFS::Dense::biases_optimizer_additional ( Eigen::MatrixXd  boptimizer)
inline

Set's the additional biases optimizer matrix of the dense layer.

Parameters
boptimizerNew additional biases optimizer matrix

◆ dbiases()

const Eigen::MatrixXd & NNFS::Dense::dbiases ( ) const
inline

Get biases gradients.

Returns
Eigen::MatrixXd& Biases gradients

◆ dweights()

const Eigen::MatrixXd & NNFS::Dense::dweights ( ) const
inline

Get weights gradients.

Returns
Eigen::MatrixXd& Weights gradients

◆ forward()

void NNFS::Dense::forward ( Eigen::MatrixXd &  out,
const Eigen::MatrixXd &  x 
)
inlinevirtual

Forward pass of the dense layer.

Parameters
[out]outOutput of the layer
[in]xInput of the layer

Implements NNFS::Layer.

◆ l1_biases_regularizer()

const double & NNFS::Dense::l1_biases_regularizer ( ) const
inline

Get L1 biases regularizer.

Returns
double L1 biases regularizer

◆ l1_weights_regularizer()

const double & NNFS::Dense::l1_weights_regularizer ( ) const
inline

Get L1 weights regularizer.

Returns
double L1 weights regularizer

◆ l2_biases_regularizer()

const double & NNFS::Dense::l2_biases_regularizer ( ) const
inline

Get L2 biases regularizer.

Returns
double L2 biases regularizer

◆ l2_weights_regularizer()

const double & NNFS::Dense::l2_weights_regularizer ( ) const
inline

Get L2 weights regularizer.

Returns
double L2 weights regularizer

◆ parameters()

int NNFS::Dense::parameters ( ) const
inline

Calculates the number of trainable of the dense layer.

Returns
int Number of parameters

◆ shape()

void NNFS::Dense::shape ( int &  n_input,
int &  n_output 
) const
inline

Gives the shape of the dense layer.

Parameters
[out]n_inputNumber of input neurons
[out]n_outputNumber of output neurons

◆ weights() [1/2]

const Eigen::MatrixXd & NNFS::Dense::weights ( ) const
inline

Get weights.

Returns
Eigen::MatrixXd& Weights

◆ weights() [2/2]

void NNFS::Dense::weights ( Eigen::MatrixXd &  weights)
inline

Set's the weights of the dense layer.

This function sets the weights of the dense layer. The shape of the new weights matrix must match the shape of the initial weights matrix.

Parameters
[in]weightsNew weights matrix
Exceptions
std::invalid_argumentif the shape of the new weights matrix does not match the shape of the initial weights matrix.

◆ weights_optimizer() [1/2]

const Eigen::MatrixXd & NNFS::Dense::weights_optimizer ( ) const
inline

Get weights optimizer matrix.

Returns
Eigen::MatrixXd& Weights optimizer matrix

◆ weights_optimizer() [2/2]

void NNFS::Dense::weights_optimizer ( Eigen::MatrixXd  woptimizer)
inline

Set's the weights optimizer matrix of the dense layer.

Parameters
woptimizerNew weights optimizer matrix

◆ weights_optimizer_additional() [1/2]

const Eigen::MatrixXd & NNFS::Dense::weights_optimizer_additional ( ) const
inline

Get additional weights optimizer matrix.

Returns
Eigen::MatrixXd& Additional weights optimizer matrix

◆ weights_optimizer_additional() [2/2]

void NNFS::Dense::weights_optimizer_additional ( Eigen::MatrixXd  woptimizer)
inline

Set's the additional weights optimizer matrix of the dense layer.

Parameters
woptimizerNew additional weights optimizer matrix

The documentation for this class was generated from the following file: