added feedForward and moving Reccurent neuron to normal

This commit is contained in:
2016-01-28 22:17:36 +01:00
parent 13b179dd57
commit 3e383e9add
12 changed files with 265 additions and 252 deletions

View File

@@ -3,13 +3,19 @@
#include <string>
#include <vector>
#include <sstream>
#include <limits>
#include <NeuralNetwork/ActivationFunction/Sigmoid.h>
#include <NeuralNetwork/BasisFunction/Linear.h>
namespace NeuralNetwork
{
/**
* @author Tomas Cernik (Tom.Cernik@gmail.com)
* @brief Abstract class of neuron. All Neuron classes should derive from this on
*/
class Neuron
class NeuronInterface
{
public:
@@ -21,7 +27,7 @@ namespace NeuralNetwork
/**
* @brief virtual destructor for Neuron
*/
virtual ~Neuron() {};
virtual ~NeuronInterface() {};
/**
* @brief This is a virtual function for storing network
@@ -33,14 +39,14 @@ namespace NeuralNetwork
* @brief Gets weight
* @param n is neuron
*/
virtual float getWeight(const Neuron &n) const =0;
virtual float getWeight(const NeuronInterface &n) const =0;
/**
* @brief Sets weight
* @param n is neuron
* @param w is new weight for input neuron n
*/
virtual void setWeight(const Neuron& n ,const float &w) =0;
virtual void setWeight(const NeuronInterface& n ,const float &w) =0;
/**
* @brief Returns output of neuron
@@ -73,21 +79,133 @@ namespace NeuralNetwork
/**
* @brief Function returns clone of object
*/
virtual Neuron* clone() const = 0;
virtual NeuronInterface* clone() const = 0;
protected:
};
class BiasNeuron: public Neuron {
/**
* @author Tomas Cernik (Tom.Cernik@gmail.com)
* @brief Class of FeedForward neuron.
*/
class Neuron: public NeuronInterface
{
public:
Neuron(unsigned long _id=0): NeuronInterface(), basis(new BasisFunction::Linear),
activation(new ActivationFunction::Sigmoid(-4.9)),
id_(_id),weights(_id+1),_output(0),_value(0) {
}
Neuron(const Neuron &r): NeuronInterface(), basis(r.basis->clone()), activation(r.activation->clone()),id_(r.id_),
weights(r.weights), _output(r._output), _value(r._value) {
}
virtual ~Neuron() {
delete basis;
delete activation;
};
virtual std::string stringify(const std::string &prefix="") const override;
Neuron& operator=(const Neuron&r) {
id_=r.id_;
weights=r.weights;
basis=r.basis->clone();
activation=r.activation->clone();
return *this;
}
virtual long unsigned int id() const override {
return id_;
};
/**
* @brief Gets weight
* @param n is neuron
*/
virtual float getWeight(const NeuronInterface &n) const override {
return weights[n.id()];
}
/**
* @brief Sets weight
* @param n is neuron
* @param w is new weight for input neuron n
*/
virtual void setWeight(const NeuronInterface& n ,const float &w) override {
if(weights.size()<n.id()+1) {
weights.resize(n.id()+1);
}
weights[n.id()]=w;
}
/**
* @brief Returns output of neuron
*/
virtual float output() const override {
return _output;
}
/**
* @brief Returns input of neuron
*/
virtual float value() const override {
return _value;
}
/**
* @brief Function sets bias for neuron
* @param _bias is new bias (initial value for neuron)
*/
virtual void setBias(const float &_bias) override {
weights[0]=_bias;
}
/**
* @brief Function returns bias for neuron
*/
virtual float getBias() const override {
return weights[0];
}
float operator()(const std::vector<float>& inputs) {
//compute value
_value=basis->operator()(weights,inputs);
//compute output
_output=activation->operator()(_value);
return _output;
}
virtual Neuron* clone() const override {
Neuron *n = new Neuron;
*n=*this;
return n;
}
protected:
BasisFunction::BasisFunction *basis;
ActivationFunction::ActivationFunction *activation;
unsigned long id_;
std::vector<float> weights;
float _output;
float _value;
};
class BiasNeuron: public NeuronInterface {
public:
virtual float getBias() const override { return 0; };
virtual float getWeight(const Neuron&) const override { return 0; }
virtual float getWeight(const NeuronInterface&) const override { return 0; }
virtual void setBias(const float&) override{ }
virtual float output() const override { return 1.0; };
virtual void setWeight(const Neuron&, const float&) override { }
virtual void setWeight(const NeuronInterface&, const float&) override { }
virtual std::string stringify(const std::string& prefix = "") const override { return prefix+"{ \"class\" : \"NeuralNetwork::BiasNeuron\" }"; }
@@ -97,10 +215,10 @@ namespace NeuralNetwork
virtual float operator()(const std::vector< float >&) override { return 1.0; }
virtual Neuron* clone() const { return new BiasNeuron(); }
virtual BiasNeuron* clone() const { return new BiasNeuron(); }
};
class InputNeuron: public Neuron {
class InputNeuron: public NeuronInterface {
public:
InputNeuron(long unsigned int _id): id_(_id) {
@@ -108,13 +226,13 @@ namespace NeuralNetwork
virtual float getBias() const override { return 0; };
virtual float getWeight(const Neuron&) const override { return 0; }
virtual float getWeight(const NeuronInterface&) const override { return 0; }
virtual void setBias(const float&) override{ }
virtual float output() const override { return 1.0; };
virtual void setWeight(const Neuron&, const float&) override { }
virtual void setWeight(const NeuronInterface&, const float&) override { }
virtual std::string stringify(const std::string& prefix = "") const override { return prefix+"{ \"class\" : \"NeuralNetwork::InputNeuron\", \"id\": "+std::to_string(id_)+" }"; }
@@ -124,7 +242,7 @@ namespace NeuralNetwork
virtual float operator()(const std::vector< float >&) override { return 1.0; }
virtual Neuron* clone() const { return new InputNeuron(id_); }
virtual InputNeuron* clone() const { return new InputNeuron(id_); }
protected:
long unsigned int id_;
};