added feedForward and moving Reccurent neuron to normal
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Network.h"
|
||||
#include "Neuron.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
@@ -67,7 +66,7 @@ namespace Recurrent {
|
||||
*/
|
||||
std::vector<float> computeOutput(const std::vector<float>& input, unsigned int iterations);
|
||||
|
||||
std::vector<NeuralNetwork::Neuron*>& getNeurons () {
|
||||
std::vector<NeuronInterface*>& getNeurons () {
|
||||
return neurons;
|
||||
}
|
||||
|
||||
@@ -75,10 +74,10 @@ namespace Recurrent {
|
||||
|
||||
void stringify(std::ostream& out) const override;
|
||||
|
||||
NeuralNetwork::Neuron& addNeuron() {
|
||||
neurons.push_back(new Recurrent::Neuron(neurons.size()));
|
||||
NeuralNetwork::Neuron *newNeuron=neurons.back();
|
||||
for(size_t i=0;i<neurons.size();i++) {
|
||||
NeuronInterface& addNeuron() {
|
||||
neurons.push_back(new Neuron(neurons.size()));
|
||||
NeuronInterface *newNeuron=neurons.back();
|
||||
for(std::size_t i=0;i<neurons.size();i++) {
|
||||
neurons[i]->setWeight(*newNeuron,0.0);
|
||||
}
|
||||
return *newNeuron;
|
||||
@@ -95,7 +94,7 @@ namespace Recurrent {
|
||||
size_t inputSize=0;
|
||||
size_t outputSize=0;
|
||||
|
||||
std::vector<NeuralNetwork::Neuron*> neurons;
|
||||
std::vector<NeuronInterface*> neurons;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,191 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Neuron.h"
|
||||
#include <NeuralNetwork/ActivationFunction/Sigmoid.h>
|
||||
#include <NeuralNetwork/BasisFunction/Linear.h>
|
||||
#include <vector>
|
||||
|
||||
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <limits>
|
||||
|
||||
namespace NeuralNetwork {
|
||||
namespace Recurrent {
|
||||
|
||||
class Network;
|
||||
|
||||
/**
|
||||
* @author Tomas Cernik (Tom.Cernik@gmail.com)
|
||||
* @brief Class of recurrent neuron.
|
||||
*/
|
||||
class Neuron : public NeuralNetwork::Neuron
|
||||
{
|
||||
public:
|
||||
Neuron(unsigned long _id=0): NeuralNetwork::Neuron(), basis(new BasisFunction::Linear),
|
||||
activation(new ActivationFunction::Sigmoid(-4.9)),
|
||||
id_(_id),weights(_id+1),_output(0),_value(0) {
|
||||
}
|
||||
|
||||
Neuron(const Neuron &r): NeuralNetwork::Neuron(), 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;
|
||||
|
||||
Recurrent::Neuron& operator=(const NeuralNetwork::Recurrent::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 NeuralNetwork::Neuron &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 NeuralNetwork::Neuron& 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 Recurrent::Neuron;
|
||||
*n=*this;
|
||||
return n;
|
||||
}
|
||||
protected:
|
||||
BasisFunction::BasisFunction *basis;
|
||||
ActivationFunction::ActivationFunction *activation;
|
||||
|
||||
unsigned long id_;
|
||||
std::vector<float> weights;
|
||||
float _output;
|
||||
float _value;
|
||||
};
|
||||
|
||||
/**
|
||||
* @author Tomas Cernik (Tom.Cernik@gmail.com)
|
||||
* @brief Class of LSTM unit
|
||||
*/
|
||||
// input + input gate + forget gate + ouput gate
|
||||
// https://en.wikipedia.org/wiki/Long_short-term_memory
|
||||
|
||||
class LSTMNeuron : public Neuron
|
||||
{
|
||||
public:
|
||||
LSTMNeuron(unsigned long _id=0): Neuron(_id) {
|
||||
}
|
||||
|
||||
LSTMNeuron(const Neuron &r): Neuron(r) {
|
||||
}
|
||||
|
||||
virtual ~LSTMNeuron() {
|
||||
|
||||
};
|
||||
|
||||
virtual std::string stringify(const std::string &prefix="") const override;
|
||||
|
||||
LSTMNeuron& operator=(const LSTMNeuron&r) {
|
||||
this->Neuron::operator=(r);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns output of neuron
|
||||
*/
|
||||
virtual float output() const override {
|
||||
return _output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns input of neuron
|
||||
*/
|
||||
virtual float value() const override {
|
||||
return _value;
|
||||
}
|
||||
|
||||
float operator()(const std::vector<float>& inputs) override {
|
||||
|
||||
//compute value
|
||||
_value=basis->operator()(weights,inputs);
|
||||
|
||||
//compute output
|
||||
_output=activation->operator()(_value);
|
||||
|
||||
return _output;
|
||||
}
|
||||
|
||||
virtual Recurrent::LSTMNeuron* clone() const override {
|
||||
LSTMNeuron *n = new Recurrent::LSTMNeuron;
|
||||
*n=*this;
|
||||
return n;
|
||||
}
|
||||
protected:
|
||||
std::vector<float> forgetWeights;
|
||||
std::vector<float> outputWeights;
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user