OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
opennn::NeuralNetwork Class Reference

Container of layers forming a feed-forward neural network, with parameter storage and I/O. More...

#include <neural_network.h>

Inheritance diagram for opennn::NeuralNetwork:
[legend]

Public Member Functions

 NeuralNetwork ()
 Constructs an empty neural network.
 
virtual ~NeuralNetwork ()=default
 
 NeuralNetwork (const filesystem::path &)
 Constructs a neural network and loads its definition from a JSON file.
 
void add_layer (unique_ptr< Layer >, const vector< Index > &={})
 Appends a layer to the network.
 
const Configuration::Resolvedget_config () const
 
bool is_gpu () const
 
bool is_cpu () const
 
Type get_training_type () const
 
Type get_inference_type () const
 
vector< vector< TensorSpec > > get_parameter_specs () const
 Returns the tensor specs of trainable parameters for every layer.
 
vector< vector< TensorSpec > > get_state_specs () const
 Returns the tensor specs of persistent layer state (e.g. running statistics).
 
vector< vector< TensorSpec > > get_forward_specs (Index b) const
 Returns the tensor specs of the forward-propagation workspace for each layer.
 
vector< vector< TensorSpec > > get_backward_specs (Index b) const
 Returns the tensor specs of the back-propagation workspace for each layer.
 
Index get_states_size () const
 Returns the total byte size required to hold all persistent layer states.
 
void compile ()
 Allocates buffers, resolves devices, and wires layer/operator views; call once after all layers are added.
 
bool has (const string &) const
 Returns whether the network contains a layer with the given label.
 
bool has (LayerType) const
 Returns whether the network contains at least one layer of the given type.
 
bool is_empty () const
 
float * get_parameters_data ()
 
const float * get_parameters_data () const
 
Index get_parameters_size () const
 
const vector< Variable > & get_input_variables () const
 
vector< string > get_input_feature_names () const
 Returns the flat list of input feature names (expanding categorical variables).
 
const vector< Variable > & get_output_variables () const
 
vector< string > get_output_feature_names () const
 Returns the flat list of output feature names (expanding categorical variables).
 
const vector< unique_ptr< Layer > > & get_layers () const
 
const unique_ptr< Layer > & get_layer (const Index i) const
 
const unique_ptr< Layer > & get_layer (const string &) const
 Returns the layer with the given label.
 
Index get_layer_index (const string &) const
 Returns the index of the layer with the given label, or -1 if not found.
 
const vector< vector< Index > > & get_source_layers () const
 
vector< vector< Index > > get_consumer_layers () const
 Returns the inverse adjacency: for each layer, the indices of layers that consume its output.
 
Layerget_first (const string &)
 Returns the first layer matching the given label, or nullptr if not found.
 
Layerget_first (LayerType)
 Returns the first layer of the given type, or nullptr if not found.
 
const Layerget_first (const string &) const
 Returns the first layer matching the given label, or nullptr if not found.
 
const Layerget_first (LayerType) const
 Returns the first layer of the given type, or nullptr if not found.
 
void set_source_layers (const vector< vector< Index > > &new_source_layers)
 Replaces the layer connectivity graph.
 
void set_source_layers (const Index layer_index, const vector< Index > &new_sources)
 Replaces the source layers for one specific layer.
 
void set_source_layers (const string &, const vector< string > &)
 Sets the source layers of a layer using labels for identification.
 
void set_source_layers (const string &, initializer_list< string >)
 Sets the source layers of a layer using labels for identification.
 
void set_source_layers (const string &, const string &)
 Convenience overload for a single source layer.
 
void set_input_variables (const vector< Variable > &new_input_variables)
 
void set_output_variables (const vector< Variable > &new_output_variables)
 
void set_input_names (const vector< string > &)
 Sets the names of every input feature.
 
void set_output_names (const vector< string > &)
 Sets the names of every output feature.
 
void set_input_shape (const Shape &)
 Sets the shape of the input of the first layer and propagates it through the graph.
 
void clear ()
 Removes all layers and resets the network to an empty state.
 
Index get_layers_number () const
 
Index get_layers_number (const string &) const
 Returns the number of layers whose label contains the given substring.
 
Index get_layers_number (LayerType) const
 Returns the number of layers of the given type.
 
Index get_first_trainable_layer_index () const
 Returns the index of the first trainable layer (cached).
 
Index get_last_trainable_layer_index () const
 Returns the index of the last trainable layer (cached).
 
Index get_inputs_number () const
 Returns the number of input features expected by the first layer.
 
Index get_outputs_number () const
 Returns the number of output features produced by the last layer.
 
Shape get_input_shape () const
 Returns the shape of the input of the first layer.
 
Shape get_output_shape () const
 Returns the shape of the output of the last layer.
 
ActivationOp::Function get_output_activation () const
 Returns the activation function of the output layer.
 
Index get_parameters_number () const
 Returns the total number of trainable parameters across all layers.
 
void set_parameters (const VectorR &new_parameters)
 Copies the contents of new_parameters into the network's parameter buffer.
 
void set_parameters_random ()
 Initializes every parameter with random values.
 
void set_parameters_glorot ()
 Initializes every parameter using Glorot (Xavier) initialization.
 
void link_parameters ()
 Wires the contiguous parameter buffer to per-layer / per-operator views.
 
void link_states ()
 Wires the contiguous state buffer to per-layer / per-operator views.
 
MatrixR calculate_outputs (const vector< TensorView > &)
 Computes outputs for the given input tensor views.
 
MatrixR calculate_outputs (const MatrixR &)
 Computes outputs for a 2D input matrix.
 
MatrixR calculate_outputs (const Tensor3 &)
 Computes outputs for a 3D input tensor.
 
MatrixR calculate_outputs (const Tensor4 &)
 Computes outputs for a 4D input tensor.
 
MatrixR calculate_directional_inputs (const Index, const VectorR &, float, float, Index=101) const
 Generates samples by sweeping one input dimension across a range while keeping the others fixed.
 
Tensor3 calculate_outputs (const Tensor3 &, const Tensor3 &)
 Computes outputs for an encoder/decoder model.
 
Index calculate_image_output (const filesystem::path &)
 Reads an image file and returns the predicted class index.
 
MatrixR calculate_text_outputs (const Tensor< string, 1 > &)
 Tokenizes the given strings and returns the network's outputs.
 
void from_JSON (const JsonDocument &)
 Restores the network architecture and parameters from a JSON document.
 
void to_JSON (JsonWriter &) const
 Serializes the network architecture and parameters to a JSON writer.
 
void save (const filesystem::path &) const
 Saves the full network (architecture + parameters) to a JSON file.
 
void save_parameters (const filesystem::path &) const
 Saves only the parameter values to a JSON file.
 
void save_parameters_binary (const filesystem::path &) const
 Saves only the parameter values to a binary file.
 
void load (const filesystem::path &)
 Loads the full network (architecture + parameters) from a JSON file.
 
void load_parameters_binary (const filesystem::path &)
 Loads parameter values from a binary file produced by save_parameters_binary().
 
vector< string > get_names_string () const
 Returns the labels of all layers as a vector of strings.
 
void save_outputs (MatrixR &, const filesystem::path &)
 Writes the output matrix to a CSV file.
 
void save_outputs (Tensor3 &, const filesystem::path &)
 Writes the 3D output tensor to a CSV file.
 
void forward_propagate (const vector< TensorView > &, ForwardPropagation &, bool=false) const
 Runs a forward pass over all layers.
 
void forward_propagate (const vector< TensorView > &, ForwardPropagation &, bool is_training, Index first_layer_index, Index last_layer_index) const
 Runs a forward pass over a contiguous sub-range of layers.
 
void forward_propagate (const vector< TensorView > &, const VectorR &, ForwardPropagation &)
 Runs a forward pass after temporarily overwriting the parameter buffer.
 
vector< string > get_layer_labels () const
 Returns the labels of all layers in order.
 

Protected Attributes

vector< Variableinput_variables
 
vector< Variableoutput_variables
 
vector< unique_ptr< Layer > > layers
 
vector< vector< Index > > source_layers
 
Buffer parameters
 
Buffer parameters_bf16 {Device::CUDA}
 
Buffer states
 
Configuration::Resolved config
 
Index first_trainable_cache_ = -1
 
Index last_trainable_cache_ = -1
 

Detailed Description

Container of layers forming a feed-forward neural network, with parameter storage and I/O.

Constructor & Destructor Documentation

◆ NeuralNetwork() [1/2]

opennn::NeuralNetwork::NeuralNetwork ( )

Constructs an empty neural network.

◆ ~NeuralNetwork()

virtual opennn::NeuralNetwork::~NeuralNetwork ( )
virtualdefault

◆ NeuralNetwork() [2/2]

opennn::NeuralNetwork::NeuralNetwork ( const filesystem::path & )

Constructs a neural network and loads its definition from a JSON file.

Parameters
pathPath to the JSON file describing the network.

Member Function Documentation

◆ add_layer()

void opennn::NeuralNetwork::add_layer ( unique_ptr< Layer > ,
const vector< Index > & = {} )

Appends a layer to the network.

Parameters
layerOwning pointer to the layer; the network takes ownership.
source_indicesIndices of layers feeding into this one (empty means the previous layer).

◆ calculate_directional_inputs()

MatrixR opennn::NeuralNetwork::calculate_directional_inputs ( const Index ,
const VectorR & ,
float ,
float ,
Index = 101 ) const
nodiscard

Generates samples by sweeping one input dimension across a range while keeping the others fixed.

Parameters
directionIndex of the input to vary.
pointBaseline values for the remaining inputs.
minimumLower bound of the sweep range.
maximumUpper bound of the sweep range.
points_numberNumber of points sampled in the range.
Returns
Matrix with one row per sampled point.

◆ calculate_image_output()

Index opennn::NeuralNetwork::calculate_image_output ( const filesystem::path & )
nodiscard

Reads an image file and returns the predicted class index.

◆ calculate_outputs() [1/5]

MatrixR opennn::NeuralNetwork::calculate_outputs ( const MatrixR & )
nodiscard

Computes outputs for a 2D input matrix.

◆ calculate_outputs() [2/5]

MatrixR opennn::NeuralNetwork::calculate_outputs ( const Tensor3 & )
nodiscard

Computes outputs for a 3D input tensor.

◆ calculate_outputs() [3/5]

Tensor3 opennn::NeuralNetwork::calculate_outputs ( const Tensor3 & ,
const Tensor3 &  )
nodiscard

Computes outputs for an encoder/decoder model.

Parameters
encoder_inputEncoder side input tensor.
decoder_inputDecoder side input tensor.

◆ calculate_outputs() [4/5]

MatrixR opennn::NeuralNetwork::calculate_outputs ( const Tensor4 & )
nodiscard

Computes outputs for a 4D input tensor.

◆ calculate_outputs() [5/5]

MatrixR opennn::NeuralNetwork::calculate_outputs ( const vector< TensorView > & )
nodiscard

Computes outputs for the given input tensor views.

Parameters
inputsTensor views of the inputs (one per input variable).
Returns
Matrix of outputs with one row per sample.

◆ calculate_text_outputs()

MatrixR opennn::NeuralNetwork::calculate_text_outputs ( const Tensor< string, 1 > & )
nodiscard

Tokenizes the given strings and returns the network's outputs.

◆ clear()

void opennn::NeuralNetwork::clear ( )

Removes all layers and resets the network to an empty state.

◆ compile()

void opennn::NeuralNetwork::compile ( )

Allocates buffers, resolves devices, and wires layer/operator views; call once after all layers are added.

◆ forward_propagate() [1/3]

void opennn::NeuralNetwork::forward_propagate ( const vector< TensorView > & ,
const VectorR & ,
ForwardPropagation &  )

Runs a forward pass after temporarily overwriting the parameter buffer.

Parameters
inputsTensor views of the inputs.
parametersReplacement parameter values used for this pass.
forward_propagationWorkspace receiving per-layer activations.

◆ forward_propagate() [2/3]

void opennn::NeuralNetwork::forward_propagate ( const vector< TensorView > & ,
ForwardPropagation & ,
bool is_training,
Index first_layer_index,
Index last_layer_index ) const

Runs a forward pass over a contiguous sub-range of layers.

Parameters
inputsTensor views of the inputs.
forward_propagationWorkspace receiving per-layer activations.
is_trainingEnables training-only behavior.
first_layer_indexFirst layer index (inclusive) to evaluate.
last_layer_indexLast layer index (inclusive) to evaluate.

◆ forward_propagate() [3/3]

void opennn::NeuralNetwork::forward_propagate ( const vector< TensorView > & ,
ForwardPropagation & ,
bool = false ) const

Runs a forward pass over all layers.

Parameters
inputsTensor views of the inputs (one per input variable).
forward_propagationWorkspace receiving per-layer activations.
is_trainingIf true, enables training-only behavior (dropout, batch-norm stats).

◆ from_JSON()

void opennn::NeuralNetwork::from_JSON ( const JsonDocument & )

Restores the network architecture and parameters from a JSON document.

◆ get_backward_specs()

vector< vector< TensorSpec > > opennn::NeuralNetwork::get_backward_specs ( Index b) const
inlinenodiscard

Returns the tensor specs of the back-propagation workspace for each layer.

Parameters
bBatch size used to size the per-layer gradient buffers.

◆ get_config()

const Configuration::Resolved & opennn::NeuralNetwork::get_config ( ) const
inlinenodiscard

◆ get_consumer_layers()

vector< vector< Index > > opennn::NeuralNetwork::get_consumer_layers ( ) const
nodiscard

Returns the inverse adjacency: for each layer, the indices of layers that consume its output.

◆ get_first() [1/4]

Layer * opennn::NeuralNetwork::get_first ( const string & )
nodiscard

Returns the first layer matching the given label, or nullptr if not found.

◆ get_first() [2/4]

const Layer * opennn::NeuralNetwork::get_first ( const string & ) const
nodiscard

Returns the first layer matching the given label, or nullptr if not found.

◆ get_first() [3/4]

Layer * opennn::NeuralNetwork::get_first ( LayerType )
nodiscard

Returns the first layer of the given type, or nullptr if not found.

◆ get_first() [4/4]

const Layer * opennn::NeuralNetwork::get_first ( LayerType ) const
nodiscard

Returns the first layer of the given type, or nullptr if not found.

◆ get_first_trainable_layer_index()

Index opennn::NeuralNetwork::get_first_trainable_layer_index ( ) const
nodiscard

Returns the index of the first trainable layer (cached).

◆ get_forward_specs()

vector< vector< TensorSpec > > opennn::NeuralNetwork::get_forward_specs ( Index b) const
inlinenodiscard

Returns the tensor specs of the forward-propagation workspace for each layer.

Parameters
bBatch size used to size the per-layer activations.

◆ get_inference_type()

Type opennn::NeuralNetwork::get_inference_type ( ) const
inlinenodiscard

◆ get_input_feature_names()

vector< string > opennn::NeuralNetwork::get_input_feature_names ( ) const
nodiscard

Returns the flat list of input feature names (expanding categorical variables).

◆ get_input_shape()

Shape opennn::NeuralNetwork::get_input_shape ( ) const
nodiscard

Returns the shape of the input of the first layer.

◆ get_input_variables()

const vector< Variable > & opennn::NeuralNetwork::get_input_variables ( ) const
inlinenodiscard

◆ get_inputs_number()

Index opennn::NeuralNetwork::get_inputs_number ( ) const
nodiscard

Returns the number of input features expected by the first layer.

◆ get_last_trainable_layer_index()

Index opennn::NeuralNetwork::get_last_trainable_layer_index ( ) const
nodiscard

Returns the index of the last trainable layer (cached).

◆ get_layer() [1/2]

const unique_ptr< Layer > & opennn::NeuralNetwork::get_layer ( const Index i) const
inlinenodiscard

◆ get_layer() [2/2]

const unique_ptr< Layer > & opennn::NeuralNetwork::get_layer ( const string & ) const
nodiscard

Returns the layer with the given label.

Parameters
labelLabel assigned to the layer (e.g. via Layer::set_label).

◆ get_layer_index()

Index opennn::NeuralNetwork::get_layer_index ( const string & ) const
nodiscard

Returns the index of the layer with the given label, or -1 if not found.

◆ get_layer_labels()

vector< string > opennn::NeuralNetwork::get_layer_labels ( ) const
nodiscard

Returns the labels of all layers in order.

◆ get_layers()

const vector< unique_ptr< Layer > > & opennn::NeuralNetwork::get_layers ( ) const
inlinenodiscard

◆ get_layers_number() [1/3]

Index opennn::NeuralNetwork::get_layers_number ( ) const
inlinenodiscard

◆ get_layers_number() [2/3]

Index opennn::NeuralNetwork::get_layers_number ( const string & ) const
nodiscard

Returns the number of layers whose label contains the given substring.

◆ get_layers_number() [3/3]

Index opennn::NeuralNetwork::get_layers_number ( LayerType ) const
nodiscard

Returns the number of layers of the given type.

◆ get_names_string()

vector< string > opennn::NeuralNetwork::get_names_string ( ) const
nodiscard

Returns the labels of all layers as a vector of strings.

◆ get_output_activation()

ActivationOp::Function opennn::NeuralNetwork::get_output_activation ( ) const
nodiscard

Returns the activation function of the output layer.

◆ get_output_feature_names()

vector< string > opennn::NeuralNetwork::get_output_feature_names ( ) const
nodiscard

Returns the flat list of output feature names (expanding categorical variables).

◆ get_output_shape()

Shape opennn::NeuralNetwork::get_output_shape ( ) const
nodiscard

Returns the shape of the output of the last layer.

◆ get_output_variables()

const vector< Variable > & opennn::NeuralNetwork::get_output_variables ( ) const
inlinenodiscard

◆ get_outputs_number()

Index opennn::NeuralNetwork::get_outputs_number ( ) const
nodiscard

Returns the number of output features produced by the last layer.

◆ get_parameter_specs()

vector< vector< TensorSpec > > opennn::NeuralNetwork::get_parameter_specs ( ) const
inlinenodiscard

Returns the tensor specs of trainable parameters for every layer.

◆ get_parameters_data() [1/2]

float * opennn::NeuralNetwork::get_parameters_data ( )
inlinenodiscard

◆ get_parameters_data() [2/2]

const float * opennn::NeuralNetwork::get_parameters_data ( ) const
inlinenodiscard

◆ get_parameters_number()

Index opennn::NeuralNetwork::get_parameters_number ( ) const
nodiscard

Returns the total number of trainable parameters across all layers.

◆ get_parameters_size()

Index opennn::NeuralNetwork::get_parameters_size ( ) const
inlinenodiscard

◆ get_source_layers()

const vector< vector< Index > > & opennn::NeuralNetwork::get_source_layers ( ) const
inlinenodiscard

◆ get_state_specs()

vector< vector< TensorSpec > > opennn::NeuralNetwork::get_state_specs ( ) const
inlinenodiscard

Returns the tensor specs of persistent layer state (e.g. running statistics).

◆ get_states_size()

Index opennn::NeuralNetwork::get_states_size ( ) const
inlinenodiscard

Returns the total byte size required to hold all persistent layer states.

◆ get_training_type()

Type opennn::NeuralNetwork::get_training_type ( ) const
inlinenodiscard

◆ has() [1/2]

bool opennn::NeuralNetwork::has ( const string & ) const
nodiscard

Returns whether the network contains a layer with the given label.

◆ has() [2/2]

bool opennn::NeuralNetwork::has ( LayerType ) const
nodiscard

Returns whether the network contains at least one layer of the given type.

◆ is_cpu()

bool opennn::NeuralNetwork::is_cpu ( ) const
inlinenodiscard

◆ is_empty()

bool opennn::NeuralNetwork::is_empty ( ) const
inlinenodiscard

◆ is_gpu()

bool opennn::NeuralNetwork::is_gpu ( ) const
inlinenodiscard

◆ link_parameters()

void opennn::NeuralNetwork::link_parameters ( )

Wires the contiguous parameter buffer to per-layer / per-operator views.

◆ link_states()

void opennn::NeuralNetwork::link_states ( )

Wires the contiguous state buffer to per-layer / per-operator views.

◆ load()

void opennn::NeuralNetwork::load ( const filesystem::path & )

Loads the full network (architecture + parameters) from a JSON file.

◆ load_parameters_binary()

void opennn::NeuralNetwork::load_parameters_binary ( const filesystem::path & )

Loads parameter values from a binary file produced by save_parameters_binary().

◆ save()

void opennn::NeuralNetwork::save ( const filesystem::path & ) const

Saves the full network (architecture + parameters) to a JSON file.

◆ save_outputs() [1/2]

void opennn::NeuralNetwork::save_outputs ( MatrixR & ,
const filesystem::path &  )

Writes the output matrix to a CSV file.

◆ save_outputs() [2/2]

void opennn::NeuralNetwork::save_outputs ( Tensor3 & ,
const filesystem::path &  )

Writes the 3D output tensor to a CSV file.

◆ save_parameters()

void opennn::NeuralNetwork::save_parameters ( const filesystem::path & ) const

Saves only the parameter values to a JSON file.

◆ save_parameters_binary()

void opennn::NeuralNetwork::save_parameters_binary ( const filesystem::path & ) const

Saves only the parameter values to a binary file.

◆ set_input_names()

void opennn::NeuralNetwork::set_input_names ( const vector< string > & )

Sets the names of every input feature.

◆ set_input_shape()

void opennn::NeuralNetwork::set_input_shape ( const Shape & )

Sets the shape of the input of the first layer and propagates it through the graph.

◆ set_input_variables()

void opennn::NeuralNetwork::set_input_variables ( const vector< Variable > & new_input_variables)
inline

◆ set_output_names()

void opennn::NeuralNetwork::set_output_names ( const vector< string > & )

Sets the names of every output feature.

◆ set_output_variables()

void opennn::NeuralNetwork::set_output_variables ( const vector< Variable > & new_output_variables)
inline

◆ set_parameters()

void opennn::NeuralNetwork::set_parameters ( const VectorR & new_parameters)

Copies the contents of new_parameters into the network's parameter buffer.

◆ set_parameters_glorot()

void opennn::NeuralNetwork::set_parameters_glorot ( )

Initializes every parameter using Glorot (Xavier) initialization.

◆ set_parameters_random()

void opennn::NeuralNetwork::set_parameters_random ( )

Initializes every parameter with random values.

◆ set_source_layers() [1/5]

void opennn::NeuralNetwork::set_source_layers ( const Index layer_index,
const vector< Index > & new_sources )
inline

Replaces the source layers for one specific layer.

◆ set_source_layers() [2/5]

void opennn::NeuralNetwork::set_source_layers ( const string & ,
const string &  )

Convenience overload for a single source layer.

Parameters
targetLabel of the destination layer.
sourceLabel of the source layer feeding into the target.

◆ set_source_layers() [3/5]

void opennn::NeuralNetwork::set_source_layers ( const string & ,
const vector< string > &  )

Sets the source layers of a layer using labels for identification.

Parameters
targetLabel of the destination layer.
sourcesLabels of the source layers feeding into the target.

◆ set_source_layers() [4/5]

void opennn::NeuralNetwork::set_source_layers ( const string & ,
initializer_list< string >  )

Sets the source layers of a layer using labels for identification.

Parameters
targetLabel of the destination layer.
sourcesLabels of the source layers feeding into the target.

◆ set_source_layers() [5/5]

void opennn::NeuralNetwork::set_source_layers ( const vector< vector< Index > > & new_source_layers)
inline

Replaces the layer connectivity graph.

Parameters
new_source_layersFor each layer index, the indices of its source layers.

◆ to_JSON()

void opennn::NeuralNetwork::to_JSON ( JsonWriter & ) const

Serializes the network architecture and parameters to a JSON writer.

Member Data Documentation

◆ config

Configuration::Resolved opennn::NeuralNetwork::config
protected

◆ first_trainable_cache_

Index opennn::NeuralNetwork::first_trainable_cache_ = -1
mutableprotected

◆ input_variables

vector<Variable> opennn::NeuralNetwork::input_variables
protected

◆ last_trainable_cache_

Index opennn::NeuralNetwork::last_trainable_cache_ = -1
mutableprotected

◆ layers

vector<unique_ptr<Layer> > opennn::NeuralNetwork::layers
protected

◆ output_variables

vector<Variable> opennn::NeuralNetwork::output_variables
protected

◆ parameters

Buffer opennn::NeuralNetwork::parameters
protected

◆ parameters_bf16

Buffer opennn::NeuralNetwork::parameters_bf16 {Device::CUDA}
protected

◆ source_layers

vector<vector<Index> > opennn::NeuralNetwork::source_layers
protected

◆ states

Buffer opennn::NeuralNetwork::states
protected