FlexNN 1
Fully connected neural network built from scratch with flexible n-layer design and multiple activations.
Loading...
Searching...
No Matches
Layer.h
Go to the documentation of this file.
1
11#ifndef FlexNN_Layer_H
12#define FlexNN_Layer_H
13
14#include <string>
15#include <utility>
16#include <Eigen/Dense>
17
26namespace FlexNN
27{
38 class Layer
39 {
40 public:
52 Layer(int inputSize, int outputSize, const std::string &activationFunction = "relu")
53 : inputSize(inputSize), outputSize(outputSize), activationFunction(activationFunction)
54 {
55 // Initialize weights and biases
56 W = Eigen::MatrixXd::Random(outputSize, inputSize) * 0.5;
57 b = Eigen::VectorXd::Random(outputSize) * 0.5;
58 }
59
67 Eigen::MatrixXd getWeights() const
68 {
69 return W; // Return the weights of the layer
70 }
71
79 Eigen::VectorXd getBiases() const
80 {
81 return b; // Return the biases of the layer
82 }
83
93 std::string getActivationFunction() const { return activationFunction; }
94
105 void updateWeights(const Eigen::MatrixXd &dW, const Eigen::VectorXd &db, double learningRate)
106 {
107 W -= learningRate * dW; // Update weights
108 b -= learningRate * db; // Update biases
109 }
110
120 std::pair<Eigen::MatrixXd, Eigen::MatrixXd> forward(const Eigen::MatrixXd &input);
121
133 Eigen::MatrixXd backward(const Eigen::MatrixXd &nextW, const Eigen::MatrixXd &nextdZ, const Eigen::MatrixXd &currZ);
134
135 private:
139 int inputSize;
143 int outputSize;
149 std::string activationFunction;
156 Eigen::MatrixXd W;
162 Eigen::VectorXd b;
163 };
164}
165
166#endif // FlexNN_Layer_H
Represents a single layer in a neural network.
Definition Layer.h:39
std::pair< Eigen::MatrixXd, Eigen::MatrixXd > forward(const Eigen::MatrixXd &input)
Forward pass through the layer.
Definition Layer.cpp:25
Eigen::MatrixXd backward(const Eigen::MatrixXd &nextW, const Eigen::MatrixXd &nextdZ, const Eigen::MatrixXd &currZ)
Backward pass through the layer.
Definition Layer.cpp:64
void updateWeights(const Eigen::MatrixXd &dW, const Eigen::VectorXd &db, double learningRate)
Update weights and biases.
Definition Layer.h:105
std::string getActivationFunction() const
Get activation string (for deprecated conversion to Layers::Dense).
Definition Layer.h:93
Eigen::MatrixXd getWeights() const
Getters for weights.
Definition Layer.h:67
Layer(int inputSize, int outputSize, const std::string &activationFunction="relu")
Constructor for the Layer class.
Definition Layer.h:52
Eigen::VectorXd getBiases() const
Getters for biases.
Definition Layer.h:79
Namespace for the FlexNN neural network library.
Definition Activation.hpp:31