Fixin' namespace neuralpp

This commit is contained in:
blacklight 2009-08-09 10:24:52 +02:00
parent 49b5472480
commit 7b16294784
6 changed files with 18 additions and 197 deletions

View file

@ -6,7 +6,8 @@
* neural++.hpp: Changed header name, added BETA0 macro
* synapsis.cpp: Added momentum() method to compute the inertial momentum
of a synapsis
* everything: Data type changed from float to double for everything
* everything: Data type changed from float to double for everything,
fixing neuralpp namespace
--- Release 0.2.2 ---

View file

@ -28,13 +28,13 @@
#include "neural++_exception.hpp"
using namespace std;
namespace neuralpp {
//! Default rand value: |sin(rand)|, always >= 0 and <= 1
#define RAND ( (double) abs( sinf((double) rand()) ) )
#define RAND ( abs( sin(rand()) ) )
//! Initial value for the inertial momentum of the synapses
#define BETA0 0.7
namespace neuralpp {
class Synapsis;
class Neuron;
class Layer;

View file

@ -15,12 +15,7 @@
#include "neural++.hpp"
using namespace neuralpp;
/**
* @brief Constructor
* @param sz Size of the layer
* @param a Activation function
* @param d Its derivate
*/
namespace neuralpp {
Layer::Layer (size_t sz, double(*a)(double), double(*d)(double)) {
for (size_t i=0; i<sz; i++) {
Neuron n(a,d);
@ -31,30 +26,16 @@ Layer::Layer (size_t sz, double(*a)(double), double(*d)(double)) {
deriv=d;
}
/**
* @brief Alternative constructor. It directly gets a vector of neurons to build
* the layer
*/
Layer::Layer (vector< Neuron > &el, double (*a)(double), double(*d)(double)) {
elements=el;
actv_f=a;
deriv=d;
}
/**
* @return Number of neurons in the layer
*/
size_t Layer::size() { return elements.size(); }
/**
* @brief Redefinition for operator []. It gets the neuron at <i>i</i>
*/
Neuron& Layer::operator[] (size_t i) { return elements[i]; }
/**
* @brief It links a layer to another
* @param l Layer to connect to the current as input layer
*/
void Layer::link (Layer& l) {
srand ((unsigned) time(NULL));
@ -71,27 +52,16 @@ void Layer::link (Layer& l) {
}
}
/**
* @brief It sets a vector of propagation values to all its neurons
* @param v Vector of values to write as propagation values
*/
void Layer::setProp (vector<double> &v) {
for (size_t i=0; i<size(); i++)
elements[i].setProp(v[i]);
}
/**
* @brief It sets a vector of activation values to all its neurons
* @param v Vector of values to write as activation values
*/
void Layer::setActv (vector<double> &v) {
for (size_t i=0; i<size(); i++)
elements[i].setActv(v[i]);
}
/**
* @brief It propagates its activation values to the output layers
*/
void Layer::propagate() {
for (size_t i=0; i<size(); i++) {
Neuron *n = &(elements[i]);
@ -100,4 +70,5 @@ void Layer::propagate() {
n->setActv( actv_f(n->getProp()) );
}
}
}

View file

@ -14,28 +14,13 @@
#include "neural++.hpp"
#include "Markup.h"
#include <iostream>
using namespace neuralpp;
/**
* @brief Built-in function. The default activation function: f(x)=x
*/
namespace neuralpp {
double __actv(double prop) { return prop; }
/**
* @brief Default derivate for default activation function: f'(x)=1
*/
double __deriv(double prop) { return 1; }
/**
* @brief Constructor
* @param in_size Size of the input layer
* @param hidden_size Size of the hidden layer
* @param out_size Size of the output layer
* @param l learn rate (get it after doing some experiments, but generally try to
* keep its value quite low to be more accurate)
* @param e Epochs (cycles) to execute (the most you execute, the most the network
* can be accurate for its purpose)
*/
NeuralNet::NeuralNet (size_t in_size, size_t hidden_size, size_t out_size, double l, int e) {
epochs=e;
ref_epochs=epochs;
@ -49,18 +34,6 @@ NeuralNet::NeuralNet (size_t in_size, size_t hidden_size, size_t out_size, doubl
link();
}
/**
* @brief Constructor
* @param in_size Size of the input layer
* @param hidden_size Size of the hidden layer
* @param out_size Size of the output layer
* @param actv Activation function to use (default: f(x)=x)
* @param deriv Derivate for the activation function to use (default: f'(x)=1)
* @param l learn rate (get it after doing some experiments, but generally try to
* keep its value quite low to be more accurate)
* @param e Epochs (cycles) to execute (the most you execute, the most the network
* can be accurate for its purpose)
*/
NeuralNet::NeuralNet (size_t in_size, size_t hidden_size, size_t out_size,
double(*a)(double), double(*d)(double), double l, int e) {
epochs=e;
@ -76,15 +49,8 @@ NeuralNet::NeuralNet (size_t in_size, size_t hidden_size, size_t out_size,
link();
}
/**
* @brief It gets the output of the network (note: the layer output should contain
* an only neuron)
*/
double NeuralNet::getOutput() { return (*output)[0].getActv(); }
/**
* @brief It gets the output of the network in case the output layer contains more neurons
*/
vector<double> NeuralNet::getVectorOutput() {
vector<double> v;
@ -93,58 +59,30 @@ vector<double> NeuralNet::getVectorOutput() {
return v;
}
/**
* @brief It get the error made on the expected result as |v-v'|/v
* @param Expected value
* @return Mean error
*/
double NeuralNet::error(double expected) {
return abs( (getOutput() - expected*
deriv(getOutput())) / (abs(expected)) );
}
/**
* @brief 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 NeuralNet::propagate() {
hidden->propagate();
output->propagate();
}
/**
* @brief It sets the input for the network
* @param v Vector of doubles, containing the values to give to your network
*/
void NeuralNet::setInput(vector<double>& v) {
input->setProp(v);
input->setActv(v);
}
/**
* @brief 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 NeuralNet::link() {
hidden->link(*input);
output->link(*hidden);
}
/**
* @brief It sets the value you expect from your network
*/
void NeuralNet::setExpected(double e) { ex=e; }
/**
* @brief It gets the value expected. Of course you should specify this when you
* build your network by using setExpected.
*/
double NeuralNet::expected() { return ex; }
/**
* @brief It updates the weights of the net's synapsis through back-propagation.
* In-class use only
*/
void NeuralNet::updateWeights() {
double out_delta;
@ -180,11 +118,6 @@ void NeuralNet::updateWeights() {
}
}
/**
* @brief It commits the changes made by updateWeights() to the layer l.
* In-class use only
* @param l Layer to commit the changes
*/
void NeuralNet::commitChanges (Layer *l) {
for (size_t i=0; i<l->size(); i++) {
Neuron *n = &(*l)[i];
@ -197,11 +130,6 @@ void NeuralNet::commitChanges (Layer *l) {
}
}
/**
* @brief It updates through back-propagation the weights of the synapsis and
* computes again the output value for <i>epochs</i> times, calling back
* updateWeights and commitChanges functions
*/
void NeuralNet::update() {
while ((epochs--)>0) {
updateWeights();
@ -211,11 +139,6 @@ void NeuralNet::update() {
}
}
/**
* @brief Save an already trained neural network to a binary file
* @param fname Name of the file to write
* @return true in case of success, false otherwise
*/
bool NeuralNet::save(const char *fname) {
FILE *fp;
struct netrecord record;
@ -309,12 +232,6 @@ bool NeuralNet::save(const char *fname) {
return true;
}
/**
* @brief Constructs a neural network from a previously saved file
* (saved using 'save()' method)
* @param fname File name to load the network from
* @throw NetworkFileNotFoundException
*/
NeuralNet::NeuralNet (const char *fname) throw() {
struct netrecord record;
FILE *fp;
@ -423,13 +340,6 @@ NeuralNet::NeuralNet (const char *fname) throw() {
fclose(fp);
}
/**
* @brief Train a network using a training set loaded from an XML file. A sample XML file
* is available in examples/adder.xml
* @param xml XML file containing our training set
* @param src Source type from which the XML will be loaded (from a file [default] or from a string)
* @throw InvalidXMLException
*/
void NeuralNet::train (string xmlsrc, NeuralNet::source src = file) throw() {
double out;
CMarkup xml;
@ -487,10 +397,6 @@ void NeuralNet::train (string xmlsrc, NeuralNet::source src = file) throw() {
return;
}
/**
* @brief Initialize the training XML for the neural network
* @param xml String that will contain the XML
*/
void NeuralNet::initXML (string& xml) {
xml.append("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
"<!DOCTYPE NETWORK SYSTEM \"http://blacklight.gotdns.org/prog/neuralpp/trainer.dtd\">\n"
@ -499,12 +405,6 @@ void NeuralNet::initXML (string& xml) {
);
}
/**
* @brief Splits a string into a vector of doubles, given a delimitator
* @param delim Delimitator
* @param str String to be splitted
* @return Vector of doubles containing splitted values
*/
vector<double> NeuralNet::split (char delim, string str) {
char tmp[1024];
vector<double> v;
@ -522,18 +422,6 @@ vector<double> NeuralNet::split (char delim, string str) {
return v;
}
/**
* @brief 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:
* '&lt;training id="0"&gt;&lt;input id="0"&gt;2&lt;/input&gt;&lt;input id="1"&gt;3&lt;/input&gt;&lt;output id="0"&gt;5&lt;/output&gt;
* &lt;/training&gt;'
*
* @param id ID for the given training set (0,1,..,n)
* @param set String containing input values and expected outputs
* @return XML string
*/
string NeuralNet::XMLFromSet (int id, string set) {
string xml;
vector<double> in, out;
@ -579,11 +467,8 @@ string NeuralNet::XMLFromSet (int id, string set) {
return xml;
}
/**
* @brief Closes an open XML document generated by "initXML" and "XMLFromSet"
* @param XML string to close
*/
void NeuralNet::closeXML(string &xml) {
xml.append("</NETWORK>\n\n");
}
}

View file

@ -12,21 +12,14 @@
**************************************************************************************************/
#include "neural++.hpp"
using namespace neuralpp;
/**
* @brief Constructor
* @param a Activation function
* @param d Its derivate
*/
namespace neuralpp {
Neuron::Neuron (double (*a)(double), double (*d)(double)) {
actv_f=a;
deriv=d;
}
/**
* @brief Alternative constructor, that gets also the synapsis linked to the neuron
*/
Neuron::Neuron (vector< Synapsis > i, vector< Synapsis > o, double (*a)(double), double(*d)(double)) {
in=i;
out=o;
@ -35,59 +28,26 @@ Neuron::Neuron (vector< Synapsis > i, vector< Synapsis > o, double (*a)(double),
deriv=d;
}
/**
* @brief Gets the i-th synapsis connected on the input of the neuron
*/
Synapsis& Neuron::synIn (size_t i) { return in[i]; }
/**
* @brief Gets the i-th synapsis connected on the output of the neuron
*/
Synapsis& Neuron::synOut (size_t i) { return out[i]; }
/**
* @brief It pushes a new input synapsis
*/
void Neuron::push_in (Synapsis& s) { in.push_back(s); }
/**
* @brief It pushes a new output synapsis
*/
void Neuron::push_out (Synapsis& s) { out.push_back(s); }
/**
* @brief Change the propagation value of the neuron
*/
void Neuron::setProp (double val) { prop_val=val; }
/**
* @brief Change the activation value of the neuron
*/
void Neuron::setActv (double val) { actv_val=actv_f(val); }
/**
* @return Number of input synapsis
*/
size_t Neuron::nIn() { return in.size(); }
/**
* @return Number of output synapsis
*/
size_t Neuron::nOut() { return out.size(); }
/**
* @brief It gets the propagation value of the neuron
*/
double Neuron::getProp() { return prop_val; }
/**
* @brief It gets the activation value of the neuron
*/
double Neuron::getActv() { return actv_val; }
/**
* @brief Propagate a neuron's activation value to the connected neurons
*/
double Neuron::propagate() {
double aux=0;
@ -95,4 +55,5 @@ double Neuron::propagate() {
aux += (in[i].getWeight() * in[i].getIn()->actv_val);
return aux;
}
}

View file

@ -15,6 +15,8 @@
#include "neural++.hpp"
using namespace neuralpp;
namespace neuralpp {
Synapsis::Synapsis(Neuron* i, Neuron* o, double w, double d) {
in=i; out=o;
weight=w;
@ -66,4 +68,5 @@ void Synapsis::setDelta(double d) {
double Synapsis::momentum(int N, int x) {
return (BETA0*N)/(20*x + N);
}
}