VNetworkBase class

A template defining the interface to a neural network.

Abstract base class for Neural networks. Initially aimed at replacing instances of an lwtnn network with a network that could be either lwtnn or ONNX, so it is an interface which mirrors that of lwtnn graphs. At least 3 derived classes are available;

The TFCSNetworkFactory::Create function has this class as its return type so that it can make a run-time decision about which derived class to use, based on the file or data presented. Has various subclasses to cover differing network libraries and save formats.

Base classes

class ISF_FCS::MLogging

Derived classes

class TFCSONNXHandler
A handler specific for an ONNX network.
class VNetworkLWTNN
A template defining the interface to a lwtnn network.

Public types

using NetworkInputs = std::map<std::string, std::map<std::string, double>>
Format for network inputs.
using NetworkOutputs = std::map<std::string, double>
Format for network outputs.

Public static variables

static const std::string m_defaultTreeName
Default name for the TTree to save in.

Public static functions

static auto representNetworkInputs(NetworkInputs const& inputs, int maxValues = 3) -> std::string
String representation of network inputs.
static auto representNetworkOutputs(NetworkOutputs const& outputs, int maxValues = 3) -> std::string
String representation of network outputs.
static auto isFile(std::string const& inputFile) -> bool
Check if a string is the path of a file on disk.

Constructors, destructors, conversion operators

VNetworkBase()
VNetworkBase default constructor.
VNetworkBase(const std::string& inputFile) explicit
VNetworkBase constructor.
VNetworkBase(const VNetworkBase& copy_from)
VNetworkBase copy constructor.
~VNetworkBase() virtual

Public functions

auto compute(NetworkInputs const& inputs) const -> NetworkOutputs pure virtual
Function to pass values to the network.
void writeNetToTTree(TTree& tree) pure virtual
Save the network to a TTree.
void writeNetToTTree(TFile& root_file, std::string const& tree_name = m_defaultTreeName)
Save the network to a TTree.
void writeNetToTTree(std::string const& root_name, std::string const& tree_name = m_defaultTreeName)
Save the network to a TTree.
auto getOutputLayers() const -> std::vector<std::string> pure virtual
List the names of the outputs.
auto isFile() const -> bool
Check if the argument inputFile is the path of a file on disk.
void deleteAllButNet() pure virtual
Get rid of any memory objects that aren't needed to run the net.

Protected functions

void setupPersistedVariables() pure virtual
Perform actions that prep data to create the net.
void setupNet() pure virtual
Perform actions that prepare network for use.
void print(std::ostream& strm) const virtual
Write a short description of this net to the string stream.
auto isRootFile(std::string const& filename = "") const -> bool
Check if a string is possibly a root file path.
void removePrefixes(NetworkOutputs& outputs) const
Remove any common prefix from the outputs.
void removePrefixes(std::vector<std::string>& output_names) const
Remove any common prefix from the outputs.

Protected variables

std::string m_inputFile
Path to the file describing the network, including filename.

Friends

auto operator<<(std::ostream& strm, const VNetworkBase& vNetworkBase) -> std::ostream&
Put-to operator to facilitate printing.

Typedef documentation

typedef std::map<std::string, std::map<std::string, double>> VNetworkBase::NetworkInputs

Format for network inputs.

The doubles are the values to be passed into the network. Strings in the outer map identify the input node, which must correspond to the names of the nodes as read from the description of the network found by the constructor. Strings in the inner map identify the part of the input node, for some networks these must be simple integers, in string form, as parts of nodes do not always have the ability to carry real string labels.

typedef std::map<std::string, double> VNetworkBase::NetworkOutputs

Format for network outputs.

The doubles are the values generated by the network. Strings identify which node this value came from, and when nodes have multiple values, are suffixed with a number to indicate which part of the node they came from. So in multi-value nodes the format becomes "<node_name>_<part_n>"

Function documentation

static std::string VNetworkBase::representNetworkInputs(NetworkInputs const& inputs, int maxValues = 3)

String representation of network inputs.

Parameters
inputs values to be evaluated by the network
maxValues maximum number of values to include in the representation
Returns string represetning the inputs

Create a string that summarizes a set of network inputs. Gives basic dimensions plus a few values, up to the maxValues

static std::string VNetworkBase::representNetworkOutputs(NetworkOutputs const& outputs, int maxValues = 3)

String representation of network outputs.

Parameters
outputs output of the network
maxValues maximum number of values to include in the representation
Returns string represetning the outputs

Create a string that summarizes a set of network outputs. Gives basic dimensions plus a few values, up to the maxValues

static bool VNetworkBase::isFile(std::string const& inputFile)

Check if a string is the path of a file on disk.

Parameters
inputFile name of the pottential file
Returns is it a readable file on disk

Determines if a string corresponds to the path of a file that can be read on the disk.

VNetworkBase::VNetworkBase()

VNetworkBase default constructor.

For use in streamers.

VNetworkBase::VNetworkBase(const std::string& inputFile) explicit

VNetworkBase constructor.

Parameters
inputFile file-path on disk (with file name) of a readable file containing a description of the network to be constructed or the content of the file.

Only saves inputFile to m_inputFile; Inheriting classes should call setupPersistedVariables and setupNet in constructor;

VNetworkBase::VNetworkBase(const VNetworkBase& copy_from)

VNetworkBase copy constructor.

Parameters
copy_from existing network that we are copying

Does not call setupPersistedVariables or setupNet but will pass on m_inputFile. Inheriting classes should do whatever they need to move the variables created in the setup functions.

NetworkOutputs VNetworkBase::compute(NetworkInputs const& inputs) const pure virtual

Function to pass values to the network.

Parameters
inputs values to be evaluated by the network
Returns the output of the network

This function hides variations in the formatted needed by different network libraries, providing a uniform input and output type.

void VNetworkBase::writeNetToTTree(TTree& tree) pure virtual

Save the network to a TTree.

Parameters
tree The tree to save inside.

All data required to recreate the network object is saved into a TTree. The format is not specified.

void VNetworkBase::writeNetToTTree(TFile& root_file, std::string const& tree_name = m_defaultTreeName)

Save the network to a TTree.

Parameters
root_file The file to save inside.
tree_name The name of the TTree to save inside.

All data required to recreate the network object is saved into a TTree. The format is not specified.

void VNetworkBase::writeNetToTTree(std::string const& root_name, std::string const& tree_name = m_defaultTreeName)

Save the network to a TTree.

Parameters
root_name The path of the file to save inside.
tree_name The name of the TTree to save inside.

All data required to recreate the network object is saved into a TTree. The format is not specified.

std::vector<std::string> VNetworkBase::getOutputLayers() const pure virtual

List the names of the outputs.

Outputs are stored in an NetworkOutputs object which is indexed by strings. This function returns the list of all strings that will index the outputs.

bool VNetworkBase::isFile() const

Check if the argument inputFile is the path of a file on disk.

Returns is it a readable file on disk

Determines if the string that was passed to the constructor as inputFile corresponds to the path of a file that can be read on the disk.

void VNetworkBase::deleteAllButNet() pure virtual

Get rid of any memory objects that aren't needed to run the net.

Minimize memory usage by deleting any inputs that are no longer required to run the compute function. May prevent the net from being saved.

void VNetworkBase::setupPersistedVariables() pure virtual protected

Perform actions that prep data to create the net.

Will be called in the class constructor before calling setupNet, but not in the streamer. It sets any variables that the sreamer would persist when saving or loading to file.

void VNetworkBase::setupNet() pure virtual protected

Perform actions that prepare network for use.

Will be called in the streamer or class constructor after the inputs have been set (either automaically by the streamer or by setupPersistedVariables in the constructor). Does not delete any resources used.

void VNetworkBase::print(std::ostream& strm) const virtual protected

Write a short description of this net to the string stream.

Parameters
strm output parameter, to which the description will be written.

Intended to facilitate the put-to operator, allowing subclasses to change how this object is displayed.

bool VNetworkBase::isRootFile(std::string const& filename = "") const protected

Check if a string is possibly a root file path.

Returns is it the path of a root file

Just checks if the string ends in .root as there are almost no reliable rules for file paths.

void VNetworkBase::removePrefixes(NetworkOutputs& outputs) const protected

Remove any common prefix from the outputs.

Parameters
outputs The outputs, changed in place.

std::ostream& operator<<(std::ostream& strm, const VNetworkBase& vNetworkBase)

Put-to operator to facilitate printing.

It is useful to be able to display a reasonable representation of a network for debugging. This can be altered by subclasses by changing the protected print function of this class.