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 <utility>
15#include <Eigen/Dense>
16
25namespace FlexNN
26{
37 class Layer
38 {
39 public:
51 Layer(int inputSize, int outputSize, const std::string &activationFunction = "relu")
52 : inputSize(inputSize), outputSize(outputSize), activationFunction(activationFunction)
53 {
54 // Initialize weights and biases
55 W = Eigen::MatrixXd::Random(outputSize, inputSize) * 0.5;
56 b = Eigen::VectorXd::Random(outputSize) * 0.5;
57 }
58
66 Eigen::MatrixXd getWeights() const
67 {
68 return W; // Return the weights of the layer
69 }
70
78 Eigen::VectorXd getBiases() const
79 {
80 return b; // Return the biases of the layer
81 }
82
93 void updateWeights(const Eigen::MatrixXd &dW, const Eigen::VectorXd &db, double learningRate)
94 {
95 W -= learningRate * dW; // Update weights
96 b -= learningRate * db; // Update biases
97 }
98
108 std::pair<Eigen::MatrixXd, Eigen::MatrixXd> forward(const Eigen::MatrixXd &input);
109
121 Eigen::MatrixXd backward(const Eigen::MatrixXd &nextW, const Eigen::MatrixXd &nextdZ, const Eigen::MatrixXd &currZ);
122
123 private:
127 int inputSize;
131 int outputSize;
137 std::string activationFunction;
144 Eigen::MatrixXd W;
150 Eigen::VectorXd b;
151 };
152}
153
154#endif // FlexNN_Layer_H
Represents a single layer in a neural network.
Definition Layer.h:38
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:93
Eigen::MatrixXd getWeights() const
Getters for weights.
Definition Layer.h:66
Layer(int inputSize, int outputSize, const std::string &activationFunction="relu")
Constructor for the Layer class.
Definition Layer.h:51
Eigen::VectorXd getBiases() const
Getters for biases.
Definition Layer.h:78
Namespace for the FlexNN neural network library.