#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, double th=0.0, double(*a)(double)=__actv) | |
Constructor. | |
NeuralNet (const std::string file) throw (NetworkFileNotFoundException) | |
Constructor. | |
double | getOutput () const |
It gets the output of the network (note: the layer output should contain an only neuron). | |
std::vector< double > | getOutputs () |
It gets the output of the network in case the output layer contains more neurons. | |
double | getThreshold () const |
Get the threshold of the neurons in the network. | |
void | propagate () |
It propagates values through the network. | |
void | setInput (std::vector< double > v) |
It sets the input for the network. | |
void | save (const char *fname) throw (NetworkFileWriteException) |
Save a trained neural network to a binary file. | |
void | loadFromBinary (const std::string fname) throw (NetworkFileNotFoundException) |
DEPRECATED. | |
void | saveToBinary (const char *fname) throw (NetworkFileWriteException) |
DEPRECATED. | |
void | train (std::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 (std::string &xml) |
Initialize the training XML for the neural network. | |
static std::string | XMLFromSet (int &id, std::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 (std::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. | |
double | error (double ex) |
Get the error made on the expected result as squared deviance. | |
double | expected () const |
Get the expected value (in case you have an only neuron in output layer). | |
std::vector< double > | getExpected () const |
Get the expected value (in case you have an only neuron in output layer). | |
void | setExpected (double ex) |
It sets the value you expect from your network (in case the network has an only neuron in its output layer). | |
void | setExpected (std::vector< double > ex) |
Set the values 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 | link () |
It links the layers of the network (input, hidden, output). | |
Private Attributes | |
int | epochs |
int | ref_epochs |
double | l_rate |
double | threshold |
std::vector< double > | expect |
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
examples/adderFromString.cpp, examples/doAdd.cpp, examples/learnAdd.cpp, and examples/networkForSumsAndSubtractions.cpp.
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, | |||
double | th = 0.0 , |
|||
double(*)(double) | a = __actv | |||
) |
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) | |
th | Threshold, value in [0,1] that establishes how much a neuron must be 'sensitive' on variations of the input values | |
a | Activation function to use (default: f(x)=x) |
neuralpp::NeuralNet::NeuralNet | ( | const std::string | file | ) | throw (NetworkFileNotFoundException) |
Constructor.
file | Binary file containing a neural network previously saved by save() method |
NetworkFileNotFoundException |
void neuralpp::NeuralNet::updateWeights | ( | ) | [private] |
It updates the weights of the net's synapsis through back-propagation.
In-class use only
double neuralpp::NeuralNet::error | ( | double | ex | ) | [private] |
Get the error made on the expected result as squared deviance.
ex | Expected value |
double neuralpp::NeuralNet::expected | ( | ) | const [private] |
Get the expected value (in case you have an only neuron in output layer).
Of course you should specify this when you build your network by using setExpected.
std::vector<double> neuralpp::NeuralNet::getExpected | ( | ) | const [private] |
Get the expected value (in case you have an only neuron in output layer).
Of course you should specify this when you build your network by using setExpected.
void neuralpp::NeuralNet::setExpected | ( | double | ex | ) | [private] |
It sets the value you expect from your network (in case the network has an only neuron in its output layer).
ex | Expected output value |
void neuralpp::NeuralNet::setExpected | ( | std::vector< double > | ex | ) | [private] |
Set the values you expect from your network.
ex | Expected output values |
void neuralpp::NeuralNet::update | ( | ) | [private] |
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::link | ( | ) | [private] |
It links the layers of the network (input, hidden, output).
double neuralpp::NeuralNet::getOutput | ( | ) | const |
It gets the output of the network (note: the layer output should contain an only neuron).
std::vector<double> neuralpp::NeuralNet::getOutputs | ( | ) |
It gets the output of the network in case the output layer contains more neurons.
double neuralpp::NeuralNet::getThreshold | ( | ) | const |
Get the threshold of the neurons in the network.
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 | ( | std::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::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::loadFromBinary | ( | const std::string | fname | ) | throw (NetworkFileNotFoundException) |
DEPRECATED.
Load a trained neural network from a binary file. This function is deprecated and kept for back-compatibility. Use the XML format instead to load and neural networks and, respectly, the NeuralNetwork(const std::string) constructor or the save(const char*) methods.
fname | Name of the file to be loaded |
NetworkFileNotFoundException | When you're trying to load an invalid network file |
void neuralpp::NeuralNet::saveToBinary | ( | const char * | fname | ) | throw (NetworkFileWriteException) |
DEPRECATED.
Save a trained neural network to a binary file. This function is deprecated and kept for back-compatibility. Use the XML format instead to load and neural networks and, respectly, the NeuralNetwork(const std::string) constructor or the save(const char*) methods.
fname | Name of the file to be saved with the network information |
NetworkFileWriteException | When you try to write the network information to an invalid file |
void neuralpp::NeuralNet::train | ( | std::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 | ( | std::string & | xml | ) | [static] |
Initialize the training XML for the neural network.
xml | String that will contain the XML |
static std::string neuralpp::NeuralNet::XMLFromSet | ( | int & | id, | |
std::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 | ( | std::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::threshold [private] |
std::vector<double> neuralpp::NeuralNet::expect [private] |
double(* neuralpp::NeuralNet::actv_f)(double) [private] |
Private pointer to function, containing the function to be used as activation function.