Repository navigation
neural_network
The neural_network class represents a feedforward neural network. It consists of multiple layers (neural_layer) and provides functionalities for forward propagation, backward propagation, and serialization/deserialization.
The neural_network class has the following structure:
class neural_network {
private:
std::vector<neural_layer<double>> layers;
double learning_rate = 0.4;
public:
neural_network() = default;
neural_network(std::initializer_list<neural_layer<double>> list);
void add_layer(neural_layer<double>& layer);
void add_layer(size_t input_size, size_t output_size);
Matrix<double> inference(const Matrix<double>& input);
Matrix<double> cost(const Matrix<double>& target, Matrix<double>& prediction);
std::vector<std::pair<Matrix<double>, Matrix<double>>> forward(const Matrix<double>& input);
Matrix<double> backward(const Matrix<double>& input, const Matrix<double>& label);
std::vector<neural_layer<double>>& get_layers();
neural_layer<double>& operator[](size_t pos);
size_t size();
double get_learning_rate() const;
static std::string trim(std::string& str);
void serialize(const std::string& filename);
void deserialize(const std::string& filename);
};classDiagram
class neural_network {
- std::vector<neural_layer<double>> layers
- double learning_rate
+ neural_network()
+ neural_network(std::initializer_list<neural_layer<double>> list)
+ void add_layer(neural_layer<double>& layer)
+ void add_layer(size_t input_size, size_t output_size)
+ Matrix<double> inference(const Matrix<double>& input)
+ Matrix<double> cost(const Matrix<double>& target, Matrix<double>& prediction)
+ std::vector<std::pair<Matrix<double>,Matrix<double>>> forward(const Matrix<double>& input)
+ Matrix<double> backward(const Matrix<double>& input, const Matrix<double>& label)
+ std::vector<neural_layer<double>>& get_layers()
+ neural_layer<double>& operator[](size_t pos)
+ size_t size()
+ double get_learning_rate() const
+ static std::string trim(std::string &str)
+ void serialize(const string& filename)
+ void deserialize(const string& filename)
}
The neural_network class provides the following functionality:
-
Constructors:
-
neural_network(): Default constructor. -
neural_network(std::initializer_list<neural_layer<double>> list): Constructor that takes an initializer list ofneural_layerobjects and initializes the network with those layers.
-
-
Layer Management:
-
void add_layer(neural_layer<double>& layer): Adds aneural_layerobject to the network. -
void add_layer(size_t input_size, size_t output_size): Creates a newneural_layerobject with the specified input and output sizes and adds it to the network.
-
-
Forward Propagation:
-
Matrix<double> inference(const Matrix<double>& input): Performs forward propagation on the network with the given input and returns the output prediction. -
Matrix<double> cost(const Matrix<double>& target, Matrix<double>& prediction): Calculates the cost between the target output and the predicted output.
-
-
Backward Propagation:
-
std::vector<std::pair<Matrix<double>, Matrix<double>>> forward(const Matrix<double>& input): Performs forward propagation on the network with the given input and returns a vector of pairs containing the weighted sum (Z) and activation (A) matrices for each layer. -
Matrix<double> backward(const Matrix<double>& input, const Matrix<double>& label): Performs backward propagation on the network with the given input and label. Updates the weights and biases of each layer and returns the loss.
-
-
Layer Access:
-
std::vector<neural_layer<double>>& get_layers(): Returns a reference to the vector ofneural_layerobjects in the network. -
neural_layer<double>& operator[](size_t pos): Returns a reference to theneural_layerobject at the specified position in the network. -
size_t size(): Returns the number of layers in the network.
-
-
Learning Rate:
-
double get_learning_rate() const: Returns the learning rate of the network.
-
-
Serialization/Deserialization:
-
static std::string trim(std::string& str): Trims leading and trailing spaces from a string. -
void serialize(const std::string& filename): Serializes the network to a file in a custom format. -
void deserialize(const std::string& filename): Deserializes the network from a file in the custom format.
-
- The
neural_networkclass uses a vector ofneural_layerobjects to represent the layers of the network. - The learning rate is set to a default value of 0.4 but can be modified.
- The
inferencefunction performs forward propagation on the network and returns the output prediction. - The
costfunction calculates the cost between the target output and the predicted output. - The
forwardfunction performs forward propagation on the network and returns a vector of pairs containing the weighted sum (Z) and activation (A) matrices for each layer. - The
backwardfunction performs backward propagation on the network, updates the weights and biases of each layer, and returns the loss. - The
get_layersfunction returns a reference to the vector ofneural_layerobjects in the network. - The
operator[]function allows accessing a specific layer in the network. - The
sizefunction returns the number of layers in the network. - The
serializefunction serializes the network to a file in a custom format. - The
deserializefunction deserializes the network from a file in the custom format.
The neural_network class provides a high-level interface for creating, training, and using feedforward neural networks. It encapsulates the layers, forward and backward propagation, and serialization/deserialization functionality. Developers can use this class to easily create and manipulate neural networks for various machine learning tasks.