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

Namespace for the FlexNN neural network library. More...

Namespaces

namespace  Activations
 
namespace  Layers
 

Classes

struct  ExportOptions
 Options for export (currently version). More...
 
class  Layer
 Represents a single layer in a neural network. More...
 
struct  LayerHeader
 
struct  MainHeader
 
class  NeuralNetwork
 Linear stack of heterogeneous layers (vector<Layers::Layer>). More...
 
struct  Status
 Result of export/import. More...
 

Functions

Status exportModel (const NeuralNetwork &net, const std::string &path, ExportOptions opts={})
 Export a trained NeuralNetwork to flat binary model.bin.
 
Status importModel (NeuralNetwork &net, const std::string &path)
 Import a flat binary model.bin into a NeuralNetwork.
 
Eigen::MatrixXd oneHotEncode (const Eigen::VectorXd &Y, int num_classes)
 One-hot encodes a vector of class labels.
 
void readCSV_XY (const std::string &filename, Eigen::MatrixXd &X, Eigen::VectorXd &Y)
 Reads a CSV where first column is label, rest are features.
 
void setRandomSeed (uint32_t seed)
 Set the global RNG seed for splitXY (deterministic splits).
 
uint32_t getRandomSeed () noexcept
 Get the current global RNG seed.
 
std::vector< std::pair< Eigen::MatrixXd, Eigen::VectorXd > > splitXY (const Eigen::MatrixXd &X, const Eigen::VectorXd &Y, const std::vector< double > &proportions)
 Splits dataset into multiple sets by proportions (uses global seed).
 
std::vector< std::pair< Eigen::MatrixXd, Eigen::VectorXd > > splitXY (const Eigen::MatrixXd &X, const Eigen::VectorXd &Y, const std::vector< double > &proportions, uint32_t seed)
 Splits dataset with explicit seed (deterministic, no global state).
 

Detailed Description

Namespace for the FlexNN neural network library.

This namespace contains all the classes and functions related to the FlexNN library, including the NeuralNetwork class and Layer class. It provides a structured way to organize the library's components and avoid naming conflicts with other libraries.

Function Documentation

◆ exportModel()

Status FlexNN::exportModel ( const NeuralNetwork &  net,
const std::string &  path,
ExportOptions  opts = {} 
)

Export a trained NeuralNetwork to flat binary model.bin.

Validates each layer (e.g., Conv1D dilation==1, Softmax only on last layer, unknown DType, LeakyReLU alpha in (0,1)) and returns Status::Err on impossible export without writing. Otherwise writes main header (20B) + N*52B LayerHeaders + blobs (float32 LE, row-major) + file CRC32 and returns Ok. v2 writes ActivationParameters for LeakyReLU as aux.

Parameters
netNetwork to export (weights are double, cast to float32)
pathDestination file (truncated if exists)
optsExport options (formatVersion 1 or 2)
Returns
Status::Ok on success, Err with message on failure

◆ getRandomSeed()

uint32_t FlexNN::getRandomSeed ( )
noexcept

Get the current global RNG seed.

Returns
Current seed (initially from random_device once)

◆ importModel()

Status FlexNN::importModel ( NeuralNetwork &  net,
const std::string &  path 
)

Import a flat binary model.bin into a NeuralNetwork.

Validates magic, version, header_len, layer_count, header/file CRC32, offsets, and per-type counts. Reconstructs Layers::Dense / Conv1D with the same activation and float32 blobs cast back to double.

Parameters
netOutput network (overwritten on success, unchanged on failure)
pathSource file
Returns
Status::Ok on success, Err on failure

◆ oneHotEncode()

Eigen::MatrixXd FlexNN::oneHotEncode ( const Eigen::VectorXd &  Y,
int  num_classes 
)

One-hot encodes a vector of class labels.

Parameters
YInput vector of class labels (size = batch)
num_classesNumber of unique classes
Returns
Matrix [num_classes × batch] where each column is one-hot

◆ readCSV_XY()

void FlexNN::readCSV_XY ( const std::string &  filename,
Eigen::MatrixXd &  X,
Eigen::VectorXd &  Y 
)

Reads a CSV where first column is label, rest are features.

Parameters
filenamePath to CSV
XOutput features [samples × features]
YOutput labels [samples]

◆ setRandomSeed()

void FlexNN::setRandomSeed ( uint32_t  seed)

Set the global RNG seed for splitXY (deterministic splits).

Thread-safe (atomic). The thread_local engine is reseeded on next splitXY call without explicit seed. For per-call determinism, use splitXY(X,Y,props,seed) instead.

Parameters
seedNew seed

◆ splitXY() [1/2]

std::vector< std::pair< Eigen::MatrixXd, Eigen::VectorXd > > FlexNN::splitXY ( const Eigen::MatrixXd &  X,
const Eigen::VectorXd &  Y,
const std::vector< double > &  proportions 
)

Splits dataset into multiple sets by proportions (uses global seed).

Shuffles indices via a thread_local mt19937 seeded from the global getRandomSeed() (or random_device on first call). For reproducibility, call setRandomSeed(42) before splitXY, or use the overload with explicit seed.

Parameters
XFeatures [samples × features]
YLabels [samples]
proportionsSplit fractions (sum ≤1, last is remainder)
Returns
Vector of (X_split, Y_split) pairs

◆ splitXY() [2/2]

std::vector< std::pair< Eigen::MatrixXd, Eigen::VectorXd > > FlexNN::splitXY ( const Eigen::MatrixXd &  X,
const Eigen::VectorXd &  Y,
const std::vector< double > &  proportions,
uint32_t  seed 
)

Splits dataset with explicit seed (deterministic, no global state).

Uses a local mt19937(seed) so the global seed is unchanged. Prefer this in tests for hermetic determinism.

Parameters
XFeatures [samples × features]
YLabels [samples]
proportionsSplit fractions
seedExplicit seed
Returns
Vector of splits