tweaking speed

This commit is contained in:
2016-05-08 12:09:24 +02:00
parent 4b42a8c310
commit 79d094e1cb
6 changed files with 131 additions and 107 deletions

View File

@@ -20,7 +20,7 @@ namespace FeedForward {
* @brief Constructor for Network
* @param _inputSize is number of inputs to network
*/
inline Network(size_t _inputSize):NeuralNetwork::Network(_inputSize,_inputSize),layers() {
inline Network(size_t _inputSize):NeuralNetwork::Network(_inputSize,_inputSize),layers(),_partialInput(_inputSize),_partialOutput(_inputSize) {
appendLayer(_inputSize);
};
@@ -42,9 +42,17 @@ namespace FeedForward {
_inputs=size;
}
if(_partialInput.size() < size+1) {
_partialInput.resize(size+1);
}
if(_partialOutput.size() < size+1) {
_partialOutput.resize(size+1);
}
_outputs=size;
return *layers[layers.size()-1];//.back();
return *layers.back();
}
Layer& operator[](const std::size_t &id) {
@@ -80,6 +88,8 @@ namespace FeedForward {
protected:
std::vector<Layer*> layers;
std::vector<float> _partialInput = {};
std::vector<float> _partialOutput = {};
private:
inline Network():NeuralNetwork::Network(0,0),layers() {

View File

@@ -217,7 +217,7 @@ namespace NeuralNetwork
}
virtual ActivationFunction::ActivationFunction& getActivationFunction() override {
throw usageException("activation function");
throw usageException("biasNeuron - activation function");
}
virtual void setBasisFunction(const BasisFunction::BasisFunction&) override {
@@ -268,7 +268,7 @@ namespace NeuralNetwork
}
virtual ActivationFunction::ActivationFunction& getActivationFunction() override {
throw usageException("activation function");
throw usageException("input neuron - activation function");
}
virtual void setBasisFunction(const BasisFunction::BasisFunction&) override {

View File

@@ -9,111 +9,117 @@
#include <limits>
namespace NeuralNetwork {
namespace Recurrent {
namespace Recurrent {
/**
* @author Tomas Cernik (Tom.Cernik@gmail.com)
* @brief Reccurent model of Artifical neural network
*/
class Network: public NeuralNetwork::Network {
public:
/**
* @author Tomas Cernik (Tom.Cernik@gmail.com)
* @brief Reccurent model of Artifical neural network
*/
class Network : public NeuralNetwork::Network {
public:
/**
* @brief Constructor for Network
* @param _inputSize is number of inputs to network
* @param _outputSize is size of output from network
* @param hiddenUnits is number of hiddenUnits to be created
*/
inline Network(size_t inputSize, size_t outputSize,size_t hiddenUnits=0):NeuralNetwork::Network(inputSize,outputSize), neurons(0),outputs(0) {
neurons.push_back(new NeuralNetwork::BiasNeuron());
/**
* @brief Constructor for Network
* @param _inputSize is number of inputs to network
* @param _outputSize is size of output from network
* @param hiddenUnits is number of hiddenUnits to be created
*/
inline Network(size_t inputSize, size_t outputSize, size_t hiddenUnits = 0) : NeuralNetwork::Network(inputSize, outputSize), neurons(0), _outputsOfNeurons(0) {
neurons.push_back(new NeuralNetwork::BiasNeuron());
for(size_t i=0;i<inputSize;i++) {
neurons.push_back(new NeuralNetwork::InputNeuron(neurons.size()));
for(size_t i = 0; i < inputSize; i++) {
neurons.push_back(new NeuralNetwork::InputNeuron(neurons.size()));
}
for(size_t i = 0; i < outputSize; i++) {
addNeuron();
}
for(size_t i = 0; i < hiddenUnits; i++) {
addNeuron();
}
};
Network(const Network &r) : NeuralNetwork::Network(r), neurons(0), _outputsOfNeurons(r._outputsOfNeurons) {
neurons.push_back(new NeuralNetwork::BiasNeuron());
for(std::size_t i = 1; i < r.neurons.size(); i++) {
neurons.push_back(r.neurons[i]->clone());
}
}
for(size_t i=0;i<outputSize;i++) {
addNeuron();
Network &operator=(const Network &r);
/**
* @brief Virtual destructor for Network
*/
virtual ~Network() {
for(auto &a:neurons) {
delete a;
}
};
void reset() {
for(auto &output: _outputsOfNeurons) {
output=0.0;
}
}
for(size_t i=0;i<hiddenUnits;i++) {
addNeuron();
/**
* @brief This is a function to compute one iterations of network
* @param input is input of network
* @returns output of network
*/
inline virtual std::vector<float> computeOutput(const std::vector<float> &input) override {
return computeOutput(input, 1);
}
};
Network(const Network &r) : NeuralNetwork::Network(r), neurons(0), outputs(r.outputs) {
neurons.push_back(new NeuralNetwork::BiasNeuron());
for(std::size_t i=1;i<r.neurons.size();i++) {
neurons.push_back(r.neurons[i]->clone());
/**
* @brief This is a function to compute iterations of network
* @param input is input of network
* @param iterations is number of iterations
* @returns output of network
*/
std::vector<float> computeOutput(const std::vector<float> &input, unsigned int iterations);
std::vector<NeuronInterface *> &getNeurons() {
return neurons;
}
}
Network& operator=(const Network&r);
virtual SimpleJSON::Type::Object serialize() const override;
/**
* @brief Virtual destructor for Network
*/
virtual ~Network() {
for(auto& a:neurons) {
delete a;
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]->setInputSize(newNeuron->id + 1);
}
return *newNeuron;
}
};
/**
* @brief This is a function to compute one iterations of network
* @param input is input of network
* @returns output of network
*/
inline virtual std::vector<float> computeOutput(const std::vector<float>& input) override {
return computeOutput(input,1);
}
/**
* @brief creates new network from joining two
* @param r is network that is connected to outputs of this network
* @returns network of constructed from two networks
*/
NeuralNetwork::Recurrent::Network connectWith(const NeuralNetwork::Recurrent::Network &r) const;
/**
* @brief This is a function to compute iterations of network
* @param input is input of network
* @param iterations is number of iterations
* @returns output of network
*/
std::vector<float> computeOutput(const std::vector<float>& input, unsigned int iterations);
static std::unique_ptr<Network> deserialize(const SimpleJSON::Type::Object &);
std::vector<NeuronInterface*>& getNeurons () {
return neurons;
}
std::size_t size() const {
return neurons.size();
};
virtual SimpleJSON::Type::Object serialize() const override;
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]->setInputSize(newNeuron->id+1);
NeuronInterface &operator[](std::size_t index) {
return *neurons[index];
}
return *newNeuron;
}
/**
* @brief creates new network from joining two
* @param r is network that is connected to outputs of this network
* @returns network of constructed from two networks
*/
NeuralNetwork::Recurrent::Network connectWith(const NeuralNetwork::Recurrent::Network &r) const;
typedef SimpleJSON::Factory<Network> Factory;
static std::unique_ptr<Network> deserialize(const SimpleJSON::Type::Object&);
protected:
std::vector<NeuronInterface *> neurons;
std::vector<float> _outputsOfNeurons;
std::size_t size() const {
return neurons.size();
};
NeuronInterface& operator[](std::size_t index) {
return *neurons[index];
}
typedef SimpleJSON::Factory<Network> Factory;
protected:
std::vector<NeuronInterface*> neurons;
std::vector<float> outputs;
SIMPLEJSON_REGISTER(NeuralNetwork::Recurrent::Network::Factory,NeuralNetwork::Recurrent::Network, deserialize)
};
}
SIMPLEJSON_REGISTER(NeuralNetwork::Recurrent::Network::Factory, NeuralNetwork::Recurrent::Network, deserialize)
};
}
}