#include <neural++.hpp>
Public Types | |
enum | source { file, str } |
Enum to choose the eventual training source for our network (XML from a file or from a string). More... | |
Public Member Functions | |
NeuralNet () | |
Empty constructor for the class - it just makes nothing. | |
NeuralNet (size_t in_size, size_t hidden_size, size_t out_size, double l, int e) | |
Constructor. | |
NeuralNet (const string file) throw (NetworkFileNotFoundException) | |
Constructor. | |
NeuralNet (size_t in_size, size_t hidden_size, size_t out_size, double(*actv)(double), double l, int e) | |
Constructor. | |
double | getOutput () const |
It gets the output of the network (note: the layer output should contain an only neuron). | |
vector< double > | getOutputs () |
It gets the output of the network in case the output layer contains more neurons. | |
double | expected () const |
It gets the value expected. | |
void | setExpected (double ex) |
It sets the value you expect from your network. | |
void | update () |
It updates through back-propagation the weights of the synapsis and computes again the output value for epochs times, calling back updateWeights and commitChanges functions. | |
void | propagate () |
It propagates values through the network. | |
void | setInput (vector< double > &v) |
It sets the input for the network. | |
void | link () |
It links the layers of the network (input, hidden, output). | |
void | save (const char *fname) throw (NetworkFileWriteException) |
Save a trained neural network to a binary file. | |
void | train (string xml, source src) throw (InvalidXMLException) |
Train a network using a training set loaded from an XML file. | |
Static Public Member Functions | |
static void | initXML (string &xml) |
Initialize the training XML for the neural network. | |
static vector< double > | split (char delim, string str) |
Splits a string into a vector of doubles, given a delimitator. | |
static string | XMLFromSet (int id, string set) |
Get a training set from a string and copies it to an XML For example, these strings could be training sets for making sums: "2,3;5" - "5,6;11" - "2,2;4" - "4,5:9" This method called on the first string will return an XML such this: '<training id="0"><input id="0">2</input><input id="1">3</input><output id="0">5</output> </training>'. | |
static void | closeXML (string &xml) |
Closes an open XML document generated by "initXML" and "XMLFromSet". | |
Public Attributes | |
Layer * | input |
Layer * | hidden |
Layer * | output |
Private Member Functions | |
void | updateWeights () |
It updates the weights of the net's synapsis through back-propagation. | |
void | commitChanges (Layer *l) |
It commits the changes made by updateWeights() to the layer l. | |
double | error (double ex) const |
Get the error made on the expected result as |v-v'|/v. | |
Private Attributes | |
int | epochs |
int | ref_epochs |
double | l_rate |
double | ex |
double(* | actv_f )(double) |
Private pointer to function, containing the function to be used as activation function. |
Use *ONLY* this class, unless you know what you're doing
neuralpp::NeuralNet::NeuralNet | ( | ) | [inline] |
Empty constructor for the class - it just makes nothing.
neuralpp::NeuralNet::NeuralNet | ( | size_t | in_size, | |
size_t | hidden_size, | |||
size_t | out_size, | |||
double | l, | |||
int | e | |||
) |
Constructor.
in_size | Size of the input layer | |
hidden_size | Size of the hidden layer | |
out_size | Size of the output layer | |
l | learn rate (get it after doing some experiments, but generally try to keep its value quite low to be more accurate) | |
e | Epochs (cycles) to execute (the most you execute, the most the network can be accurate for its purpose) |
neuralpp::NeuralNet::NeuralNet | ( | const string | file | ) | throw (NetworkFileNotFoundException) |
Constructor.
file | Binary file containing a neural network previously saved by save() method |
NetworkFileNotFoundException |
neuralpp::NeuralNet::NeuralNet | ( | size_t | in_size, | |
size_t | hidden_size, | |||
size_t | out_size, | |||
double(*)(double) | actv, | |||
double | l, | |||
int | e | |||
) |
Constructor.
in_size | Size of the input layer | |
hidden_size | Size of the hidden layer | |
out_size | Size of the output layer | |
actv | Activation function to use (default: f(x)=x) | |
l | learn rate (get it after doing some experiments, but generally try to keep its value quite low to be more accurate) | |
e | Epochs (cycles) to execute (the most you execute, the most the network can be accurate for its purpose) |
void neuralpp::NeuralNet::updateWeights | ( | ) | [private] |
It updates the weights of the net's synapsis through back-propagation.
In-class use only
void neuralpp::NeuralNet::commitChanges | ( | Layer * | l | ) | [private] |
It commits the changes made by updateWeights() to the layer l.
In-class use only
l | Layer to commit the changes |
double neuralpp::NeuralNet::error | ( | double | ex | ) | const [private] |
Get the error made on the expected result as |v-v'|/v.
ex | Expected value |
double neuralpp::NeuralNet::getOutput | ( | ) | const |
It gets the output of the network (note: the layer output should contain an only neuron).
vector<double> neuralpp::NeuralNet::getOutputs | ( | ) |
It gets the output of the network in case the output layer contains more neurons.
double neuralpp::NeuralNet::expected | ( | ) | const |
It gets the value expected.
Of course you should specify this when you build your network by using setExpected.
void neuralpp::NeuralNet::setExpected | ( | double | ex | ) |
It sets the value you expect from your network.
ex | Expected output value |
void neuralpp::NeuralNet::update | ( | ) |
It updates through back-propagation the weights of the synapsis and computes again the output value for epochs times, calling back updateWeights and commitChanges functions.
void neuralpp::NeuralNet::propagate | ( | ) |
It propagates values through the network.
Use this when you want to give an already trained network some new values the get to the output
void neuralpp::NeuralNet::setInput | ( | vector< double > & | v | ) |
It sets the input for the network.
v | Vector of doubles, containing the values to give to your network |
void neuralpp::NeuralNet::link | ( | ) |
It links the layers of the network (input, hidden, output).
Don't use unless you exactly know what you're doing, it is already called by the constructor
void neuralpp::NeuralNet::save | ( | const char * | fname | ) | throw (NetworkFileWriteException) |
Save a trained neural network to a binary file.
fname | Binary file where you're going to save your network |
NetworkFileWriteException | When you get an error writing the network's information to a file |
void neuralpp::NeuralNet::train | ( | string | xml, | |
source | src | |||
) | throw (InvalidXMLException) |
Train a network using a training set loaded from an XML file.
A sample XML file is available in examples/adder.xml
xml | XML file containing our training set | |
src | Source type from which the XML will be loaded (from a file [default] or from a string) |
InvalidXMLException |
static void neuralpp::NeuralNet::initXML | ( | string & | xml | ) | [static] |
Initialize the training XML for the neural network.
xml | String that will contain the XML |
static vector<double> neuralpp::NeuralNet::split | ( | char | delim, | |
string | str | |||
) | [static] |
Splits a string into a vector of doubles, given a delimitator.
delim | Delimitator | |
str | String to be splitted |
static string neuralpp::NeuralNet::XMLFromSet | ( | int | id, | |
string | set | |||
) | [static] |
Get a training set from a string and copies it to an XML For example, these strings could be training sets for making sums: "2,3;5" - "5,6;11" - "2,2;4" - "4,5:9" This method called on the first string will return an XML such this: '<training id="0"><input id="0">2</input><input id="1">3</input><output id="0">5</output> </training>'.
id | ID for the given training set (0,1,..,n) | |
set | String containing input values and expected outputs |
static void neuralpp::NeuralNet::closeXML | ( | string & | xml | ) | [static] |
Closes an open XML document generated by "initXML" and "XMLFromSet".
xml | XML string to be closed |
int neuralpp::NeuralNet::epochs [private] |
int neuralpp::NeuralNet::ref_epochs [private] |
double neuralpp::NeuralNet::l_rate [private] |
double neuralpp::NeuralNet::ex [private] |
double(* neuralpp::NeuralNet::actv_f)(double) [private] |
Private pointer to function, containing the function to be used as activation function.