Skip to content

4. The nn Class

Nick Oppen edited this page Dec 1, 2013 · 2 revisions

Table of Contents

About Class nn

Class nn implements a three layer feed forward – back propagation neural network. It uses the C++ standard library with one of the C++ 2011 standard features.

Constructors

Create a new Network

nn(int inputLayerWidth, int hiddenLayerWidth, int outputLayerWidth, float learningRateParam, string & newName)

Create a new network with

  • inputLayerWidth input nodes,
  • hiddenLayerWidth hidden nodes,
  • outputLayerWidth output nodes and
  • learningRateParam as the learning rate with
  • newName as the network name

nn(network_description newNet)

Create a new network using the data in the object newNet

Use this call if you want to change default parameters that are not available in the more primative nn(int, int, int, float, string&) constructor

Read in a saved network

nn(string * strFilename)

nn(const char * cstrFilename)

nn(networkFile * newFile)

Reconstruct a network from a saved file.

The nn::nn(string *...) function calls the nn::nn(const char *...) after stripping off the C string. Therefore don't bother to create a string object if you have a C string.

Operation

Run

voidrun(string * strFilename, funcRunCallback runComplete = NULL)

voidrun(const char * cstrFilename, funcRunCallback runComplete = NULL)

voidrun(inputFile * inFile, funcRunCallback runComplete = NULL)

Open the input file given input file and run each input vector through the network, calling the runComplete callback each time. The first function (nn::run(string *...) calls the second (nn::run(const char *...) which calls the third (nn::run(inputFile *...). Therefore, if you have the file name as a C string don't bother wrapping it in a string object because I just pull out the C string from the string and throw the string object away.

The call back has the following form:

typedef void (*funcRunCallback)(const int index, void * thisNetwork);

  • index is the index of the row that has just been run
  • theNetwork is a void pointer to this object.
Call ((nn*)theNetwork)->runResult(vector<float>* existingVector) to retrieve the result

voidrun(vector<float> * inputVector, funcRunCallback runComplete = NULL, const int index = 0)

Pass inputVector to the input layer and trigger it to execute the network logic. Call the runComplete callback if it is not NULL.

voidrun(vector<float> * inputVector, vector<float> * outputVector)

Run a single input vector and return the result. This call is designed to run synchronously.

vector<float> * runResult(vector<float> * outputVector)

Set and return outputVector from the last run.

Call this quickly - I'm not sure how long it will be before the result is written over by the next output.

Train

void train(string * strFilename, funcTrainCallback trComplete = NULL)

voidtrain(const char * cstrFilename, funcTrainCallback trComplete = NULL)

void train(trainingFile * trFile, funcTrainCallback trComplete = NULL)

Train the network using the training file. Call the trComplete callback once when training is complete.

The callback has the following form:

typedef void (*funcTrainCallback)(void * nnObj)

It merely passes an anomymous pointer to this object. Call ((nn*)nnObj)->trainingError(vector<float>* existingVector); to retrieve the most recent training error vector

In a way similar to the run commands nn::train(string *...) calls nn::train(const char *...) calls nn::train(trainingFile *). Therefore don't bother to wrap a C string in a string object before calling this function.

voidtrain(vector<float> * inputVector, vector<float> * desiredVector, funcTrainCallback trComplete = NULL)

Train the network with the single pair, inputVector and desiredVector. Call the trComplete callback if it is not NULL when training is complete.

status_ttrainingError(vector<float> * errorVector)

Return the most recent error vector generated by the most recent training set.

Note: the errorVector must exist and be the right size

voidrandomise()

Randomise the weights and biases in the network thereby restarting the training cycle from a different place.

Test

voidtest(string * strTestFilename, funcTestCallback testComplete = NULL)

voidtest(const char * cstrTestFilename, funcTestCallback testComplete = NULL)

voidtest(trainingFile * testFile, funcTestCallback testComplete = NULL)

Run the data component of the training file and compare the output generated by the network to the desired output. Calculate the difference.

The call back function funcTestCallback is called once for every line in the input file and has the following form:

typedef void (*funcTestCallback)(const int index, vector<float>* inputVector, vector<float>* desiredOutput, vector<float>* outputVector, vector<float>* errorVector, void * thisObject);

  • index: the row number in the file
  • inputVector: a pointer to the test data vector
  • desiredOutput: a pointer to the desired output vector
  • outputVector: a pointer to the actual output from the net
  • errorVector: a pointer to a vector containing the desired minus the actual output
  • thisObject: an anonymous pointer to this object
voidtest(const int index, vector<float> * inputVector, vector<float> * desiredOutput, funcTestCallback testComplete = NULL)

Test a single input vector and compare the result with the givine output vector. Then compare the output generated by the network to the desired output. Calculate the difference.

Access

network_description * networkDescription()

Return the current network_descripton object.

unsigned int inputNodes()

unsigned int hiddenNodes()

unsigned int outputNodes()

Return the current number of input, hidden or output nodes individually.

Save

status_tsaveTo(string * strPath)

status_tsaveTo(const char * cstrPath)

status_tsaveTo(fstream * pFile)

Save the network to a file called <network Name>_<majorVersion>_<minorVersion>_<revision>.enn in the path supplied in string object strPath.

Note: if you have the path name already as a C string call saveTo(const char *) rather than this function

status_tsaveOn(string * strOut)

save the net in the given existing string

Alter

status_talter(int newIn, int newHidden, int newOut)

Alter the topology of the network to be

  • newIn: the new number of input nodes
  • newHidden: the new number of hidden nodes
  • newOut: the new number of output nodes
This will randomise the network and increment the major version resetting the minorVerions and revision

status_talter(network_description * newTopo)

Alter the topology of the network to be as described in the network_topology object newTopo. Use this call if you want to alter multiple features at once. This will randomise the network and increment the major version resetting the minorVersions and revision

status_talter(unsigned int layer, layer_modifier mod, bool boolAdd = true)

Alter a layer within the network. Currently you can only add or remove a bias node from layer zero (the input layer) This will randomise the network and increment the major version resetting the minorVersions and revision

Miscellaneous

char *defaultName(char * buffer)

Return the default name for the network, which is: <network Name>_<majorVersion>_<minorVersion>_<revision>.enn

Note: the calling function must make sure that there is enough room in the buffer

boolneedsSaving()

Return true if the network has changed since it was last saved.