FlexNN 1
Fully connected neural network built from scratch with flexible n-layer design and multiple activations.
Loading...
Searching...
No Matches
FlexNN::Layers::Layer Class Reference

Type-erased layer — holds one concrete layer type. More...

#include <Layer.hpp>

Public Member Functions

 Layer (Dense d)
 
 Layer (Conv1D c)
 
 Layer (BatchNorm1D b)
 
 Layer (MaxPool1D m)
 
 Layer (AvgPool1D a)
 
LayerType type () const noexcept
 Layer kind (Dense, Conv1D, ...).
 
Activations::Activation activation () const noexcept
 Activation for this layer.
 
Activations::ActivationParameters activationParams () const noexcept
 Activation hyperparameters (e.g., LeakyReLU alpha).
 
std::pair< Eigen::MatrixXd, Eigen::MatrixXd > forward (const Eigen::MatrixXd &input) const
 Forward pass for this layer (dispatch via visit).
 
Eigen::MatrixXd backward (const Eigen::MatrixXd &nextW, const Eigen::MatrixXd &nextdZ, const Eigen::MatrixXd &currZ) const
 Backward pass for this layer (dispatch via visit).
 
Eigen::MatrixXd backward (const Eigen::MatrixXd &upstream, const Eigen::MatrixXd &currZ) const
 Backward with already-propagated upstream (W_next^T*dZ_next).
 
void updateWeights (const Eigen::MatrixXd &dW, const Eigen::VectorXd &db, double lr) noexcept
 SGD update — dispatches to concrete update.
 
void update (const Eigen::MatrixXd &dW, const Eigen::VectorXd &db, double lr) noexcept
 
Eigen::MatrixXd weightsMatrix () const
 Weight matrix for backward — Dense/Conv1D.
 
Eigen::MatrixXd propagate (const Eigen::MatrixXd &dZ) const
 Propagate gradient to previous layer (W^T * dZ or col2im).
 
std::pair< Eigen::MatrixXd, Eigen::VectorXd > grad (const Eigen::MatrixXd &dZ, const Eigen::MatrixXd &input) const
 Compute dW/db for this layer given dZ and input.
 
const auto & variant () const noexcept
 Direct access to the underlying variant (for ModelIO).
 
auto & variant () noexcept
 
const Dense * asDense () const noexcept
 
Dense * asDense () noexcept
 
const Conv1D * asConv1D () const noexcept
 
Conv1D * asConv1D () noexcept
 
const BatchNorm1D * asBatchNorm1D () const noexcept
 
BatchNorm1D * asBatchNorm1D () noexcept
 
const MaxPool1D * asMaxPool1D () const noexcept
 
MaxPool1D * asMaxPool1D () noexcept
 
const AvgPool1D * asAvgPool1D () const noexcept
 
AvgPool1D * asAvgPool1D () noexcept
 

Detailed Description

Type-erased layer — holds one concrete layer type.

In PR-05 the variant holds Dense and Conv1D. Later PRs expand it:

Each concrete type implements the same interface: LayerType type() const noexcept Activations::Activation activation() const noexcept pair<MatrixXd,MatrixXd> forward(const MatrixXd&) const MatrixXd backward(const MatrixXd& nextW, const MatrixXd& nextdZ, const MatrixXd& currZ) const void update(const MatrixXd& dW, const VectorXd& db, double lr) noexcept

The wrapper forwards to the active alternative via std::visit.

Constructor & Destructor Documentation

◆ Layer() [1/5]

FlexNN::Layers::Layer::Layer ( Dense  d)
inline

◆ Layer() [2/5]

FlexNN::Layers::Layer::Layer ( Conv1D  c)
inline

◆ Layer() [3/5]

FlexNN::Layers::Layer::Layer ( BatchNorm1D  b)
inline

◆ Layer() [4/5]

FlexNN::Layers::Layer::Layer ( MaxPool1D  m)
inline

◆ Layer() [5/5]

FlexNN::Layers::Layer::Layer ( AvgPool1D  a)
inline

Member Function Documentation

◆ activation()

Activations::Activation FlexNN::Layers::Layer::activation ( ) const
inlinenoexcept

Activation for this layer.

◆ activationParams()

Activations::ActivationParameters FlexNN::Layers::Layer::activationParams ( ) const
inlinenoexcept

Activation hyperparameters (e.g., LeakyReLU alpha).

Returns stored ActivationParameters for layers that carry it (Dense, Conv1D); otherwise default. Non-Leaky activations ignore the returned struct.

◆ asAvgPool1D() [1/2]

const AvgPool1D * FlexNN::Layers::Layer::asAvgPool1D ( ) const
inlinenoexcept

◆ asAvgPool1D() [2/2]

AvgPool1D * FlexNN::Layers::Layer::asAvgPool1D ( )
inlinenoexcept

◆ asBatchNorm1D() [1/2]

const BatchNorm1D * FlexNN::Layers::Layer::asBatchNorm1D ( ) const
inlinenoexcept

◆ asBatchNorm1D() [2/2]

BatchNorm1D * FlexNN::Layers::Layer::asBatchNorm1D ( )
inlinenoexcept

◆ asConv1D() [1/2]

const Conv1D * FlexNN::Layers::Layer::asConv1D ( ) const
inlinenoexcept

◆ asConv1D() [2/2]

Conv1D * FlexNN::Layers::Layer::asConv1D ( )
inlinenoexcept

◆ asDense() [1/2]

const Dense * FlexNN::Layers::Layer::asDense ( ) const
inlinenoexcept

◆ asDense() [2/2]

Dense * FlexNN::Layers::Layer::asDense ( )
inlinenoexcept

◆ asMaxPool1D() [1/2]

const MaxPool1D * FlexNN::Layers::Layer::asMaxPool1D ( ) const
inlinenoexcept

◆ asMaxPool1D() [2/2]

MaxPool1D * FlexNN::Layers::Layer::asMaxPool1D ( )
inlinenoexcept

◆ backward() [1/2]

Eigen::MatrixXd FlexNN::Layers::Layer::backward ( const Eigen::MatrixXd &  nextW,
const Eigen::MatrixXd &  nextdZ,
const Eigen::MatrixXd &  currZ 
) const
inline

Backward pass for this layer (dispatch via visit).

For the last layer, NeuralNetwork::backward fuses Softmax+CE and never calls this for the last layer. For hidden layers this applies ‘f’(Z) ⊙ (W_next^T * dZ_next)`.

Kept for dense_test compatibility (takes nextW/nextdZ).

◆ backward() [2/2]

Eigen::MatrixXd FlexNN::Layers::Layer::backward ( const Eigen::MatrixXd &  upstream,
const Eigen::MatrixXd &  currZ 
) const
inline

Backward with already-propagated upstream (W_next^T*dZ_next).

Used by NeuralNetwork::backward after layers[i+1].propagate(dZ_next) so the caller handles Conv1D transpose via col2im.

◆ forward()

std::pair< Eigen::MatrixXd, Eigen::MatrixXd > FlexNN::Layers::Layer::forward ( const Eigen::MatrixXd &  input) const
inline

Forward pass for this layer (dispatch via visit).

Parameters
inputBatch input [in_dim × batch]
Returns
Pair (Z, A) each [out_dim × batch]

◆ grad()

std::pair< Eigen::MatrixXd, Eigen::VectorXd > FlexNN::Layers::Layer::grad ( const Eigen::MatrixXd &  dZ,
const Eigen::MatrixXd &  input 
) const
inline

Compute dW/db for this layer given dZ and input.

For Dense: dW = dZ * X^T / batch, db = rowMean(dZ). For Conv1D: dW/db via Conv1D::grad. Returns pair (dW, db) as two matrices (db as column vector promoted to MatrixXd).

◆ propagate()

Eigen::MatrixXd FlexNN::Layers::Layer::propagate ( const Eigen::MatrixXd &  dZ) const
inline

Propagate gradient to previous layer (W^T * dZ or col2im).

Used by NeuralNetwork::backward to compute upstream for hidden layers: upstream = layers[i+1].propagate(dZ_next). For Dense this is W^T * dZ; for Conv1D this is col2im(W^T * dZ) via Conv1D::propagate.

Note
Not noexcept: allocates MatrixXd (host, bad_alloc terminates).

◆ type()

LayerType FlexNN::Layers::Layer::type ( ) const
inlinenoexcept

Layer kind (Dense, Conv1D, ...).

◆ update()

void FlexNN::Layers::Layer::update ( const Eigen::MatrixXd &  dW,
const Eigen::VectorXd &  db,
double  lr 
)
inlinenoexcept

◆ updateWeights()

void FlexNN::Layers::Layer::updateWeights ( const Eigen::MatrixXd &  dW,
const Eigen::VectorXd &  db,
double  lr 
)
inlinenoexcept

SGD update — dispatches to concrete update.

Kept as updateWeights for compatibility with legacy Layer::updateWeights signature used by NeuralNetwork::updateWeights. New code may call update.

◆ variant() [1/2]

const auto & FlexNN::Layers::Layer::variant ( ) const
inlinenoexcept

Direct access to the underlying variant (for ModelIO).

Lets ModelIO do std::visit without public variant exposure of concrete types. Kept const — mutation goes via updateWeights.

◆ variant() [2/2]

auto & FlexNN::Layers::Layer::variant ( )
inlinenoexcept

◆ weightsMatrix()

Eigen::MatrixXd FlexNN::Layers::Layer::weightsMatrix ( ) const
inline

Weight matrix for backward — Dense/Conv1D.

For Dense this is W [out×in]; for Conv1D this is W [outCh×inCh*K]. Kept for ModelIO inspection; for backward prefer propagate() which handles Conv1D col2im correctly. Not noexcept (allocates on copy).


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