#pragma once #include "../Network.h" #include #include #include #include namespace NeuralNetwork { namespace Recurrent { /** * @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), _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 < 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()); } } 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; } } /** * @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 computeOutput(const std::vector &input) override { return computeOutput(input, 1); } /** * @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 computeOutput(const std::vector &input, unsigned int iterations); std::vector &getNeurons() { return neurons; } 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); } 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; static std::unique_ptr deserialize(const SimpleJSON::Type::Object &); std::size_t size() const { return neurons.size(); }; NeuronInterface &operator[](std::size_t index) { return *neurons[index]; } typedef SimpleJSON::Factory Factory; protected: std::vector neurons; std::vector _outputsOfNeurons; SIMPLEJSON_REGISTER(NeuralNetwork::Recurrent::Network::Factory, NeuralNetwork::Recurrent::Network, deserialize) }; } }