FlexNN 1
Fully connected neural network built from scratch with flexible n-layer design and multiple activations.
Loading...
Searching...
No Matches
FlexNN.h
Go to the documentation of this file.
1
17#ifndef FlexNN_H
18#define FlexNN_H
19
20#include <vector>
21#include <Eigen/Dense>
22
23// New modular headers — must come before legacy Layer.h so `Layers::Layer`
24// is visible for the converting constructor.
26#include "layers/Layer.hpp"
27
28// Legacy monolithic Layer (string activation, Dense-only) — kept for
29// backward compat until PR-09 migrates `src/main.cpp` to the new API.
30// New code should use `FlexNN::Layers::Dense` + `FlexNN::Activations::Activation`.
31#include "Layer.h"
32
33namespace FlexNN {
34
48 public:
56 NeuralNetwork(const std::vector<Layers::Layer>& layers) : layers_(layers) {}
57 NeuralNetwork(std::vector<Layers::Layer>&& layers) : layers_(std::move(layers)) {}
58
69 [[deprecated("use vector<FlexNN::Layers::Layer> with FlexNN::Activations")]]
70 NeuralNetwork(const std::vector<Layer>& oldLayers);
71
80 void train(const Eigen::MatrixXd& input, const Eigen::VectorXd& target,
81 double learningRate, int epochs);
82
86 double accuracy(const Eigen::MatrixXd& X, const Eigen::VectorXd& Y);
87
91 Eigen::MatrixXd predict(const Eigen::MatrixXd& input) {
92 auto outputs = forward(input);
93 return outputs.back();
94 }
95
99 const std::vector<Layers::Layer>& layers() const noexcept { return layers_; }
100 std::vector<Layers::Layer>& layers() noexcept { return layers_; }
101
102 private:
103 // Heterogeneous stack — currently variant<Dense> only, expands in later PRs.
104 std::vector<Layers::Layer> layers_;
105
110 std::vector<Eigen::MatrixXd> forward(const Eigen::MatrixXd& input);
111
117 std::vector<Eigen::MatrixXd> backward(
118 const std::vector<Eigen::MatrixXd>& outputs,
119 const Eigen::MatrixXd& target);
120
124 void updateWeights(const std::vector<Eigen::MatrixXd>& gradients,
125 double learningRate);
126};
127
128} // namespace FlexNN
129
130#endif // FlexNN_H
Public activation enum and helpers for FlexNN.
Header file for the Layer class in the FlexNN neural network library.
Type-erased wrapper for FlexNN::Layers — heterogeneous stack without inheritance.
Linear stack of heterogeneous layers (vector<Layers::Layer>).
Definition FlexNN.h:47
NeuralNetwork(const std::vector< Layers::Layer > &layers)
Construct from new modular layers (preferred).
Definition FlexNN.h:56
void train(const Eigen::MatrixXd &input, const Eigen::VectorXd &target, double learningRate, int epochs)
Train the network on (X, Y) with SGD.
Definition FlexNN.cpp:54
const std::vector< Layers::Layer > & layers() const noexcept
Direct access to layers (for tests / ModelIO).
Definition FlexNN.h:99
double accuracy(const Eigen::MatrixXd &X, const Eigen::VectorXd &Y)
Accuracy on (X, Y) — argmax per column.
Definition FlexNN.cpp:70
NeuralNetwork(std::vector< Layers::Layer > &&layers)
Definition FlexNN.h:57
std::vector< Layers::Layer > & layers() noexcept
Definition FlexNN.h:100
Eigen::MatrixXd predict(const Eigen::MatrixXd &input)
Predict — forward pass, returns activation of last layer.
Definition FlexNN.h:91
Namespace for the FlexNN neural network library.
Definition Activation.hpp:31