|
OpenNN
Open-source neural networks library
|
Stack of Layers forming a trainable model. More...
#include <neural_network.h>
Public Member Functions | |
| NeuralNetwork () | |
| Default-constructs an empty network. | |
| virtual | ~NeuralNetwork ()=default |
| Defaulted virtual destructor for safe polymorphic deletion. | |
| NeuralNetwork (const filesystem::path &file_name) | |
| Constructs a network by loading a serialized model from disk. | |
| void | add_layer (unique_ptr< Layer > new_layer, const vector< Index > &input_indices=vector< Index >()) |
| Appends a layer to the stack. | |
| const Configuration::Resolved & | get_config () const |
| Returns the resolved device/precision configuration. | |
| bool | is_gpu () const |
| Reports whether the network is configured to run on CUDA. | |
| bool | is_cpu () const |
| Reports whether the network is configured to run on CPU. | |
| Type | get_training_type () const |
| Returns the precision used for training. | |
| Type | get_inference_type () const |
| Returns the precision used for inference. | |
| vector< vector< Shape > > | get_parameter_shapes () const |
| Returns the parameter shapes of every layer. | |
| vector< vector< Shape > > | get_state_shapes () const |
| Returns the persistent state shapes of every layer. | |
| vector< vector< Shape > > | get_forward_shapes (Index batch_size) const |
| Returns the forward-propagation buffer shapes for a given batch size. | |
| vector< vector< Shape > > | get_backward_shapes (Index batch_size) const |
| Returns the backward-propagation buffer shapes for a given batch size. | |
| Index | get_states_size () const |
| Returns the total size in floats of all persistent state buffers. | |
| Index | get_forward_size (Index batch_size) const |
| Returns the total size in floats of the forward-propagation workspace. | |
| Index | get_backward_size (Index batch_size) const |
| Returns the total size in floats of the backward-propagation workspace. | |
| void | compile () |
| Allocates parameters and resolves layer-input indices. | |
| bool | has (const string &layer_name) const |
| Reports whether the network contains a layer of the given name. | |
| bool | has (LayerType type) const |
| Reports whether the network contains a layer of the given type. | |
| bool | is_empty () const |
| Reports whether the network has zero layers. | |
| float * | get_parameters_data () |
| Returns the parameter buffer as a raw float pointer. | |
| const float * | get_parameters_data () const |
| Returns the parameter buffer as a raw float pointer (const overload). | |
| Index | get_parameters_size () const |
| Returns the parameter count in floats. | |
| const vector< Variable > & | get_input_variables () const |
| Returns the input variables describing each input feature. | |
| const vector< string > | get_input_feature_names () const |
| Returns the names of the input features. | |
| const vector< Variable > & | get_output_variables () const |
| Returns the output variables describing each output target. | |
| const vector< string > | get_output_feature_names () const |
| Returns the names of the output features. | |
| const vector< unique_ptr< Layer > > & | get_layers () const |
| Returns the layer stack. | |
| const unique_ptr< Layer > & | get_layer (const Index i) const |
| Returns the layer at a given index. | |
| const unique_ptr< Layer > & | get_layer (const string &layer_name) const |
| Returns the layer with a given name. | |
| Index | get_layer_index (const string &layer_name) const |
| Returns the index of the layer with a given name. | |
| const vector< vector< Index > > & | get_layer_input_indices () const |
| Returns the per-layer input-layer indices. | |
| vector< vector< Index > > | get_layer_output_indices () const |
| Returns the per-layer output-layer indices. | |
| Layer * | get_first (const string &layer_name) |
| Returns the first layer of a given type by name. | |
| Layer * | get_first (LayerType type) |
| Returns the first layer of a given type. | |
| const Layer * | get_first (const string &layer_name) const |
| Returns the first layer of a given type by name (const overload). | |
| const Layer * | get_first (LayerType type) const |
| Returns the first layer of a given type (const overload). | |
| void | set_layers_number (const Index new_layers_number) |
| Resizes the layer stack. | |
| void | set_layer_input_indices (const vector< vector< Index > > &new_layer_input_indices) |
| Replaces the entire layer-input wiring. | |
| void | set_layer_input_indices (const Index layer_index, const vector< Index > &new_input_indices) |
| Replaces the input wiring of a single layer by index. | |
| void | set_layer_input_indices (const string &layer_name, const vector< string > &input_layer_names) |
| Replaces the input wiring of a single layer by name. | |
| void | set_layer_input_indices (const string &layer_name, initializer_list< string > input_layer_names) |
| Replaces the input wiring of a single layer by name (initializer-list overload). | |
| void | set_layer_input_indices (const string &layer_name, const string &input_layer_name) |
| Sets a single source layer as input to a destination layer. | |
| void | set_input_variables (const vector< Variable > &new_input_variables) |
| Replaces the input variables. | |
| void | set_output_variables (const vector< Variable > &new_output_variables) |
| Replaces the output variables. | |
| void | set_input_names (const vector< string > &new_input_names) |
| Sets the names of the input features. | |
| void | set_output_names (const vector< string > &new_output_names) |
| Sets the names of the output features. | |
| void | set_input_shape (const Shape &new_input_shape) |
| Sets the input shape of the network. | |
| void | set_default () |
| Resets non-architectural state to defaults. | |
| Index | get_layers_number () const |
| Returns the number of layers. | |
| Index | get_layers_number (const string &layer_name) const |
| Returns the number of layers of a given type by name. | |
| Index | get_layers_number (LayerType type) const |
| Returns the number of layers of a given type. | |
| Index | get_first_trainable_layer_index () const |
| Returns the index of the first trainable layer. | |
| Index | get_last_trainable_layer_index () const |
| Returns the index of the last trainable layer. | |
| Index | get_inputs_number () const |
| Returns the total number of input features. | |
| Index | get_outputs_number () const |
| Returns the total number of output features. | |
| Shape | get_input_shape () const |
| Returns the network's input shape. | |
| Shape | get_output_shape () const |
| Returns the network's output shape. | |
| Activation::Function | get_output_activation () const |
| Returns the activation function of the last layer. | |
| Index | get_parameters_number () const |
| Returns the total number of trainable parameters. | |
| vector< Index > | get_layer_parameter_numbers () const |
| Returns the parameter count of every layer. | |
| void | set_parameters (const VectorR &new_parameters) |
| Replaces all trainable parameters. | |
| void | set_parameters_random () |
| Initializes parameters with uniform random values. | |
| void | set_parameters_glorot () |
| Initializes parameters with Glorot (Xavier) uniform values. | |
| MatrixR | calculate_outputs (const vector< TensorView > &inputs) |
| Computes outputs from a list of input tensor views. | |
| MatrixR | calculate_outputs (const MatrixR &inputs) |
| Computes outputs for tabular inputs. | |
| MatrixR | calculate_outputs (const Tensor3 &inputs) |
| Computes outputs for rank-3 inputs (e.g. sequence data). | |
| MatrixR | calculate_outputs (const Tensor4 &inputs) |
| Computes outputs for rank-4 inputs (e.g. images). | |
| MatrixR | calculate_directional_inputs (const Index direction, const VectorR &point, float minimum, float maximum, Index points_number=101) const |
| Computes outputs along a directional sweep of one input. | |
| Tensor3 | calculate_outputs (const Tensor3 &inputs, const Tensor3 &context) |
| Computes outputs from two rank-3 inputs (encoder/decoder pair). | |
| Index | calculate_image_output (const filesystem::path &image_path) |
| Runs the network on a single image and returns the predicted class. | |
| MatrixR | calculate_text_outputs (const Tensor< string, 1 > &texts) |
| Runs the network on a batch of strings and returns class outputs. | |
| void | from_JSON (const JsonDocument &document) |
| Restores the network from a JSON document. | |
| void | to_JSON (JsonWriter &writer) const |
| Serializes the network to JSON. | |
| void | save (const filesystem::path &file_name) const |
| Saves the full network (architecture + parameters) to a JSON file. | |
| void | save_parameters (const filesystem::path &file_name) const |
| Saves only the parameters in binary form. | |
| void | load (const filesystem::path &file_name) |
| Loads the full network (architecture + parameters) from a JSON file. | |
| void | load_parameters_binary (const filesystem::path &file_name) |
| Loads parameters from a binary file produced by save_parameters(). | |
| vector< string > | get_names_string () const |
| Returns the names of every input and output feature. | |
| void | save_outputs (MatrixR &outputs, const filesystem::path &file_name) |
| Saves a tabular outputs tensor to a CSV file. | |
| void | save_outputs (Tensor3 &outputs, const filesystem::path &file_name) |
| Saves a rank-3 outputs tensor to a CSV file. | |
| void | forward_propagate (const vector< TensorView > &inputs, ForwardPropagation &forward, bool is_training=false) const |
Runs the forward pass and writes intermediate activations into forward. | |
| void | forward_propagate (const vector< TensorView > &inputs, const VectorR ¶meters, ForwardPropagation &forward) |
| Runs the forward pass with explicitly supplied parameters. | |
| vector< string > | get_layer_labels () const |
| Returns a label for every layer (name + key hyperparameters). | |
Protected Attributes | |
| string | name = "neural_network" |
| Network identifier; used as a JSON tag. | |
| vector< Variable > | input_variables |
| Description of every input feature (role, scaler, type). | |
| vector< Variable > | output_variables |
| Description of every output feature (role, scaler, type). | |
| vector< unique_ptr< Layer > > | layers |
| Owned layers in execution order. | |
| vector< vector< Index > > | layer_input_indices |
| Per-destination-layer list of source-layer indices. | |
| Buffer | parameters |
| Flat parameter buffer shared by all layers. | |
| Buffer | parameters_bf16 {Device::CUDA} |
BF16 mirror of parameters for mixed-precision CUDA training. | |
| vector< vector< vector< TensorView > > > | parameter_views |
Per-layer per-parameter-group views into parameters. | |
| Buffer | states |
| Flat persistent-state buffer shared by stateful layers (e.g. Recurrent). | |
| Configuration::Resolved | config |
| Resolved device/precision configuration applied at compile time. | |
Stack of Layers forming a trainable model.
The class is designed to be subclassed; the standard architectures (ApproximationNetwork, ClassificationNetwork, ForecastingNetwork, ImageClassificationNetwork, etc.) preconfigure the layer stack for common tasks. Direct users can also call add_layer() and compile() to build custom topologies.
Holds a single parameter Buffer that all layers index into, plus a parallel states Buffer for layers that maintain hidden state. CUDA mirrors are available when compiled with OPENNN_HAS_CUDA.
| opennn::NeuralNetwork::NeuralNetwork | ( | ) |
Default-constructs an empty network.
No layers, no input/output variables, default device/precision config.
|
virtualdefault |
Defaulted virtual destructor for safe polymorphic deletion.
| opennn::NeuralNetwork::NeuralNetwork | ( | const filesystem::path & | file_name | ) |
Constructs a network by loading a serialized model from disk.
| file_name | Path to a JSON file produced by save(). |
| void opennn::NeuralNetwork::add_layer | ( | unique_ptr< Layer > | new_layer, |
| const vector< Index > & | input_indices = vector< Index >() ) |
Appends a layer to the stack.
| new_layer | Owning pointer to the layer; ownership is transferred. |
| input_indices | Indices of the layers whose outputs feed this layer. Empty means "the previous layer". |
| MatrixR opennn::NeuralNetwork::calculate_directional_inputs | ( | const Index | direction, |
| const VectorR & | point, | ||
| float | minimum, | ||
| float | maximum, | ||
| Index | points_number = 101 ) const |
Computes outputs along a directional sweep of one input.
Holds the other inputs at point and varies input direction across points_number values in [minimum, maximum].
| direction | Index of the input to sweep. |
| point | Reference point for all other inputs. |
| minimum | Minimum value for the swept input. |
| maximum | Maximum value for the swept input. |
| points_number | Number of evaluation points along the sweep. |
| Index opennn::NeuralNetwork::calculate_image_output | ( | const filesystem::path & | image_path | ) |
Runs the network on a single image and returns the predicted class.
| image_path | Path to the image file. |
Computes outputs for tabular inputs.
| inputs | Input matrix [samples x features]. |
Computes outputs for rank-3 inputs (e.g. sequence data).
| inputs | Rank-3 input tensor. |
| Tensor3 opennn::NeuralNetwork::calculate_outputs | ( | const Tensor3 & | inputs, |
| const Tensor3 & | context ) |
Computes outputs from two rank-3 inputs (encoder/decoder pair).
| inputs | Rank-3 input tensor. |
| context | Rank-3 context tensor. |
Computes outputs for rank-4 inputs (e.g. images).
| inputs | Rank-4 input tensor. |
| MatrixR opennn::NeuralNetwork::calculate_outputs | ( | const vector< TensorView > & | inputs | ) |
Computes outputs from a list of input tensor views.
| inputs | Input tensor views indexed by input slot. |
| MatrixR opennn::NeuralNetwork::calculate_text_outputs | ( | const Tensor< string, 1 > & | texts | ) |
Runs the network on a batch of strings and returns class outputs.
| texts | One string per sample. |
| void opennn::NeuralNetwork::compile | ( | ) |
Allocates parameters and resolves layer-input indices.
Should be called after the layer stack is fully assembled. Validates input/output shape consistency and sets up the shared parameter buffer.
| void opennn::NeuralNetwork::forward_propagate | ( | const vector< TensorView > & | inputs, |
| const VectorR & | parameters, | ||
| ForwardPropagation & | forward ) |
Runs the forward pass with explicitly supplied parameters.
Used by line-search style optimizers that probe along a direction.
| inputs | Input tensor views. |
| parameters | Parameter vector used for this evaluation. |
| forward | Forward-propagation workspace; populated in place. |
| void opennn::NeuralNetwork::forward_propagate | ( | const vector< TensorView > & | inputs, |
| ForwardPropagation & | forward, | ||
| bool | is_training = false ) const |
Runs the forward pass and writes intermediate activations into forward.
| inputs | Input tensor views. |
| forward | Forward-propagation workspace; populated in place. |
| is_training | Whether to enable training-time behavior (e.g. dropout). |
| void opennn::NeuralNetwork::from_JSON | ( | const JsonDocument & | document | ) |
Restores the network from a JSON document.
| document | Parsed JSON produced by to_JSON(). |
|
inline |
Returns the backward-propagation buffer shapes for a given batch size.
| batch_size | Number of samples per backward pass. |
|
inline |
Returns the total size in floats of the backward-propagation workspace.
| batch_size | Number of samples per backward pass. |
|
inline |
Returns the resolved device/precision configuration.
| Layer * opennn::NeuralNetwork::get_first | ( | const string & | layer_name | ) |
Returns the first layer of a given type by name.
| layer_name | Layer type name (e.g. "Scaling", "Dense"). |
| const Layer * opennn::NeuralNetwork::get_first | ( | const string & | layer_name | ) | const |
Returns the first layer of a given type by name (const overload).
| layer_name | Layer type name. |
Returns the first layer of a given type.
| type | Layer type enum. |
Returns the first layer of a given type (const overload).
| type | Layer type enum. |
| Index opennn::NeuralNetwork::get_first_trainable_layer_index | ( | ) | const |
Returns the index of the first trainable layer.
|
inline |
Returns the forward-propagation buffer shapes for a given batch size.
| batch_size | Number of samples per forward pass. |
|
inline |
Returns the total size in floats of the forward-propagation workspace.
| batch_size | Number of samples per forward pass. |
|
inline |
Returns the precision used for inference.
| const vector< string > opennn::NeuralNetwork::get_input_feature_names | ( | ) | const |
Returns the names of the input features.
| Shape opennn::NeuralNetwork::get_input_shape | ( | ) | const |
Returns the network's input shape.
|
inline |
Returns the input variables describing each input feature.
| Index opennn::NeuralNetwork::get_inputs_number | ( | ) | const |
Returns the total number of input features.
| Index opennn::NeuralNetwork::get_last_trainable_layer_index | ( | ) | const |
Returns the index of the last trainable layer.
|
inline |
Returns the layer at a given index.
| i | Zero-based index into the layer stack. |
| const unique_ptr< Layer > & opennn::NeuralNetwork::get_layer | ( | const string & | layer_name | ) | const |
| Index opennn::NeuralNetwork::get_layer_index | ( | const string & | layer_name | ) | const |
Returns the index of the layer with a given name.
| layer_name | Layer name. |
|
inline |
Returns the per-layer input-layer indices.
| vector< string > opennn::NeuralNetwork::get_layer_labels | ( | ) | const |
Returns a label for every layer (name + key hyperparameters).
| vector< vector< Index > > opennn::NeuralNetwork::get_layer_output_indices | ( | ) | const |
Returns the per-layer output-layer indices.
| vector< Index > opennn::NeuralNetwork::get_layer_parameter_numbers | ( | ) | const |
Returns the parameter count of every layer.
|
inline |
Returns the layer stack.
|
inline |
Returns the number of layers.
| Index opennn::NeuralNetwork::get_layers_number | ( | const string & | layer_name | ) | const |
Returns the number of layers of a given type by name.
| layer_name | Layer type name. |
| Index opennn::NeuralNetwork::get_layers_number | ( | LayerType | type | ) | const |
Returns the number of layers of a given type.
| type | Layer type enum. |
| vector< string > opennn::NeuralNetwork::get_names_string | ( | ) | const |
Returns the names of every input and output feature.
| Activation::Function opennn::NeuralNetwork::get_output_activation | ( | ) | const |
Returns the activation function of the last layer.
| const vector< string > opennn::NeuralNetwork::get_output_feature_names | ( | ) | const |
Returns the names of the output features.
| Shape opennn::NeuralNetwork::get_output_shape | ( | ) | const |
Returns the network's output shape.
|
inline |
Returns the output variables describing each output target.
| Index opennn::NeuralNetwork::get_outputs_number | ( | ) | const |
Returns the total number of output features.
|
inline |
Returns the parameter shapes of every layer.
|
inline |
Returns the parameter buffer as a raw float pointer.
|
inline |
Returns the parameter buffer as a raw float pointer (const overload).
| Index opennn::NeuralNetwork::get_parameters_number | ( | ) | const |
Returns the total number of trainable parameters.
|
inline |
Returns the parameter count in floats.
|
inline |
Returns the persistent state shapes of every layer.
|
inline |
Returns the total size in floats of all persistent state buffers.
|
inline |
Returns the precision used for training.
| bool opennn::NeuralNetwork::has | ( | const string & | layer_name | ) | const |
Reports whether the network contains a layer of the given name.
| layer_name | Layer type name (e.g. "Dense", "Recurrent"). |
| bool opennn::NeuralNetwork::has | ( | LayerType | type | ) | const |
Reports whether the network contains a layer of the given type.
| type | Layer type enum. |
|
inline |
Reports whether the network is configured to run on CPU.
|
inline |
Reports whether the network has zero layers.
|
inline |
Reports whether the network is configured to run on CUDA.
| void opennn::NeuralNetwork::load | ( | const filesystem::path & | file_name | ) |
Loads the full network (architecture + parameters) from a JSON file.
| file_name | Source path. |
| void opennn::NeuralNetwork::load_parameters_binary | ( | const filesystem::path & | file_name | ) |
Loads parameters from a binary file produced by save_parameters().
The architecture must already match the saved network.
| file_name | Source path. |
| void opennn::NeuralNetwork::save | ( | const filesystem::path & | file_name | ) | const |
Saves the full network (architecture + parameters) to a JSON file.
| file_name | Destination path. |
| void opennn::NeuralNetwork::save_outputs | ( | MatrixR & | outputs, |
| const filesystem::path & | file_name ) |
Saves a tabular outputs tensor to a CSV file.
| outputs | Outputs matrix [samples x outputs]. |
| file_name | Destination path. |
| void opennn::NeuralNetwork::save_outputs | ( | Tensor3 & | outputs, |
| const filesystem::path & | file_name ) |
Saves a rank-3 outputs tensor to a CSV file.
| outputs | Rank-3 outputs tensor. |
| file_name | Destination path. |
| void opennn::NeuralNetwork::save_parameters | ( | const filesystem::path & | file_name | ) | const |
Saves only the parameters in binary form.
| file_name | Destination path. |
| void opennn::NeuralNetwork::set_default | ( | ) |
Resets non-architectural state to defaults.
| void opennn::NeuralNetwork::set_input_names | ( | const vector< string > & | new_input_names | ) |
Sets the names of the input features.
| new_input_names | One entry per input variable. |
| void opennn::NeuralNetwork::set_input_shape | ( | const Shape & | new_input_shape | ) |
Sets the input shape of the network.
Reshapes the first layer accordingly. Useful when reusing an architecture for a dataset with different feature dimensions.
| new_input_shape | New input shape. |
|
inline |
Replaces the input variables.
| new_input_variables | New input variables vector. |
|
inline |
Replaces the input wiring of a single layer by index.
| layer_index | Index of the destination layer. |
| new_input_indices | Indices of source layers feeding it. |
| void opennn::NeuralNetwork::set_layer_input_indices | ( | const string & | layer_name, |
| const string & | input_layer_name ) |
Sets a single source layer as input to a destination layer.
| layer_name | Name of the destination layer. |
| input_layer_name | Name of the source layer. |
| void opennn::NeuralNetwork::set_layer_input_indices | ( | const string & | layer_name, |
| const vector< string > & | input_layer_names ) |
Replaces the input wiring of a single layer by name.
| layer_name | Name of the destination layer. |
| input_layer_names | Names of layers feeding it. |
| void opennn::NeuralNetwork::set_layer_input_indices | ( | const string & | layer_name, |
| initializer_list< string > | input_layer_names ) |
Replaces the input wiring of a single layer by name (initializer-list overload).
| layer_name | Name of the destination layer. |
| input_layer_names | Names of layers feeding it. |
|
inline |
Replaces the entire layer-input wiring.
| new_layer_input_indices | Outer vector indexed by destination layer. |
|
inline |
Resizes the layer stack.
| new_layers_number | New layer count; existing entries are preserved. |
| void opennn::NeuralNetwork::set_output_names | ( | const vector< string > & | new_output_names | ) |
Sets the names of the output features.
| new_output_names | One entry per output variable. |
|
inline |
Replaces the output variables.
| new_output_variables | New output variables vector. |
| void opennn::NeuralNetwork::set_parameters | ( | const VectorR & | new_parameters | ) |
Replaces all trainable parameters.
| new_parameters | Flat vector with as many entries as get_parameters_number(). |
| void opennn::NeuralNetwork::set_parameters_glorot | ( | ) |
Initializes parameters with Glorot (Xavier) uniform values.
| void opennn::NeuralNetwork::set_parameters_random | ( | ) |
Initializes parameters with uniform random values.
| void opennn::NeuralNetwork::to_JSON | ( | JsonWriter & | writer | ) | const |
Serializes the network to JSON.
| writer | JSON writer that receives the network tree. |
|
protected |
Resolved device/precision configuration applied at compile time.
|
protected |
Description of every input feature (role, scaler, type).
|
protected |
Per-destination-layer list of source-layer indices.
|
protected |
Owned layers in execution order.
|
protected |
Network identifier; used as a JSON tag.
|
protected |
Description of every output feature (role, scaler, type).
|
protected |
Per-layer per-parameter-group views into parameters.
|
protected |
BF16 mirror of parameters for mixed-precision CUDA training.