Class BpNN

Inheritance Relationships

Derived Type

Class Documentation

class BpNN

Represents a Backpropagation Neural Network (BpNN).

This class implements the structure and functionalities of a neural network using backpropagation for training.

Subclassed by Dimlp

Public Functions

inline int GetNbLayers() const

Gets the number of layers.

Returns:

The number of layers.

inline int GetNbWeightLayers() const

Gets the number of weight layers.

Returns:

The number of weight layers.

int Max(const std::vector<float> &vec) const

Finds the index of the maximum value in a vector.

Parameters:

vec – The vector to search.

Returns:

The index of the maximum value.

inline std::shared_ptr<Layer> GetLayer(int indLayer)

Gets the layer at the specified index.

Parameters:

indLayer – The index of the layer.

Returns:

The shared pointer to the layer.

void SaveWeights(const std::string &str, int netId) const

Saves the weights to a specified file.

Parameters:
  • str – The file to which the weights are saved.

  • netId – The ID of the network.

virtual void ForwardOneExample1(DataSet &data, int index)

Forwards one example through the network.

Parameters:
  • data – The dataset containing the example.

  • index – The index of the example in the dataset.

virtual void ForwardOneExample1(float *ex)

Forwards one example through the network.

Parameters:

ex – The example to forward.

virtual void ForwardOneExample1()

Forwards one example through the network.

void ReadWeights() const

Reads the weights from a file.

void PrintSpecErrDimlp(float specErr, float specAcc) const

Prints the specific error for the Dimlp network.

Parameters:
  • specErr – The specific error.

  • specAcc – The specific accuracy.

inline virtual void PrintSpecErr(float err, float acc)

Prints the specific error.

Parameters:
  • err – The error.

  • acc – The accuracy.

void DefineSmlp()

Defines a Smlp (standard multilayer perceptron) network.

void DefineDimlp(int discrLevels)

Defines a Dimlp network with a Dimlp layer that uses a staircase activation function.

Parameters:

discrLevels – The number of discrete levels.

void DefineQmlp(int discrLevels)

Defines a Qmlp (Quantized multilayer perceptron) network with a Dimlp layer that uses a staircase activation function.

Parameters:

discrLevels – The number of discrete levels.

void DefineFdimlp(int discrLevels)

Defines a Fdimlp network with a Dimlp layer and a fuzzy Dimlp layer that use a staircase activation function.

Parameters:

discrLevels – The number of discrete levels.

void DefineFdimlp2(int discrLevels)

Defines a Fdimlp2 network with a Dimlp layer and a fuzzy Dimlp layer that use a staircase activation function.

Parameters:

discrLevels – The number of discrete levels.

void DefineSD(int discrLevels)

Defines a SD (Quantized Support Vector Machine Dot product (linear kernel)) network with a layer using an identity activation function and a Dimlp layer with a staircase activation function.

Parameters:

discrLevels – The number of discrete levels.

void DefineSP3(int discrLevels)

Defines a SP3 network with a layer using a cubic activation function and a Dimlp layer with a staircase activation function.

Parameters:

discrLevels – The number of discrete levels.

void DefineSP4(int discrLevels)

Defines a SP4 network with a layer using a quartic (fourth power) activation function and a Dimlp layer with a staircase activation function.

Parameters:

discrLevels – The number of discrete levels.

void DefineSP5(int discrLevels)

Defines a SP5 network with a layer using a quintic (fifth power) activation function and a Dimlp layer with a staircase activation function.

Parameters:

discrLevels – The number of discrete levels.

void DefineSR(int discrLevels)

Defines a SR (Quantized Support Vector Machine Radial basis function) network that uses a specialized radial basis function (RBF) layer with a Gaussian activation function and a Dimlp layer with a staircase activation function.

Parameters:

discrLevels – The number of discrete levels.

float ComputeError(DataSet &data, DataSet &target, float *accuracy)

Computes the error and accuracy by performing the forward pass.

Parameters:
  • data – The dataset containing the examples.

  • target – The target dataset.

  • accuracy – Pointer to store the accuracy.

Returns:

The computed error.

void TrainPhase(DataSet &train, DataSet &trainTar, DataSet &test, DataSet &testTar, DataSet &valid, DataSet &validTar, const std::string &accuracyFile, bool fromBT = false)

Trains the network.

Parameters:
  • train – The training dataset.

  • trainTar – The training target dataset.

  • test – The testing dataset.

  • testTar – The testing target dataset.

  • valid – The validation dataset.

  • validTar – The validation target dataset.

  • accuracyFile – The file to save accuracy statistics.

  • fromBT – Flag to indicate if training is done with bagging.

BpNN(const BpNN&) = default

Default copy constructor for BpNN.

This constructor creates a new instance of BpNN as a copy of an existing instance.

BpNN &operator=(const BpNN&) = default

Default copy assignment operator for BpNN.

This operator assigns the values from an existing instance of BpNN to another instance.

Returns:

A reference to the assigned BpNN instance.

virtual ~BpNN() = default

Virtual destructor for BpNN.

This destructor ensures that the destructor of the derived class is called when an object is deleted through a pointer to the base class.

BpNN(float eta, float mu, float flat, float errParam, float accuracyParam, float deltaErrParam, int showErrParam, int nbEpochsParam, int nbLayers, const std::vector<int> &nbNeurons, const std::string &saveFile, const std::string &printNetType, int seed = 0)

Constructs a BpNN with the specified parameters.

Parameters:
  • eta – Learning rate for weight updates.

  • mu – Momentum rate for weight updates.

  • flat – Flatness parameter used in some activation functions.

  • errParam – Error threshold parameter.

  • accuracyParam – Accuracy threshold parameter.

  • deltaErrParam – Delta error threshold parameter.

  • showErrParam – Number of epochs between error displays.

  • nbEpochsParam – Number of epochs for training.

  • nbLayers – Total number of layers including input and output layers.

  • nbNeurons – Number of neurons in each layer.

  • saveFile – The file to which the network’s weights are saved.

  • printNetType – The type of network to print.

  • seed – Seed for random number generation.

BpNN(const std::string &readFile, int nbLayers, const std::vector<int> &nbNeurons, const std::string &printNetType, int netId = 1)

Constructs a BpNN from a file.

Parameters:
  • readFile – The file from which to read the network’s weights.

  • nbLayers – Total number of layers including input and output layers.

  • nbNeurons – Number of neurons in each layer.

  • printNetType – The type of network to print.

  • netId – Network ID, used for identifying networks in a file.

BpNN(const std::string &readFile, float eta, float mu, float flat, float errParam, float accuracyParam, float deltaErrParam, int showErrParam, int nbEpochsParam, int nbLayers, const std::vector<int> &nbNeurons, const std::string &saveFile, const std::string &printNetType, int seed = 0)

Constructs a BpNN with the specified parameters and reads weights from a file.

Parameters:
  • readFile – The file from which to read the network’s weights.

  • eta – Learning rate for weight updates.

  • mu – Momentum rate for weight updates.

  • flat – Flatness parameter used in some activation functions.

  • errParam – Error threshold parameter.

  • accuracyParam – Accuracy threshold parameter.

  • deltaErrParam – Delta error threshold parameter.

  • showErrParam – Number of epochs between error displays.

  • nbEpochsParam – Number of epochs for training.

  • nbLayers – Total number of layers including input and output layers.

  • nbNeurons – Number of neurons in each layer.

  • saveFile – The file to which the network’s weights are saved.

  • printNetType – The type of network to print.

  • seed – Seed for random number generation.

Public Static Functions

static void resetInitRandomGen()

Resets the random generator initialization flag.