Files
NeuralNetworkLib/include/NeuralNetwork/FeedForward/Layer.h
2016-10-31 15:03:27 +01:00

108 lines
2.5 KiB
C++

#pragma once
#include "../Neuron.h"
#include <SimpleJSON/SerializableObject.h>
#include <cstddef>
#include <vector>
namespace NeuralNetwork {
namespace FeedForward {
/**
* @author Tomas Cernik (Tom.Cernik@gmail.com)
* @brief Class for Layer of FeedForward network
*/
class Layer : public SimpleJSON::SerializableObject {
public:
Layer(std::size_t size, const ActivationFunction::ActivationFunction &activationFunction):neurons() {
neurons.push_back(new BiasNeuron);
for(std::size_t i=0;i<size;i++) {
neurons.push_back(new Neuron(neurons.size(),activationFunction));
}
}
Layer(const Layer&r):neurons() {
*this=r;
}
Layer& operator=(const Layer &r) {
for(auto &neuron:neurons) {
delete neuron;
}
neurons.clear();
for(auto &neuron:r.neurons) {
neurons.push_back(neuron->clone());
}
return *this;
}
~Layer() {
for(auto &neuron:neurons) {
delete neuron;
}
};
/**
* @brief Function adds new neuron
* @returns Newly added neuron
*/
Neuron& addNeuron(const ActivationFunction::ActivationFunction &activationFunction = ActivationFunction::Sigmoid()) {
auto neuron = new Neuron(neurons.size(),activationFunction);
neurons.push_back(neuron);
return *neuron;
}
/**
* @brief This is a virtual function for selecting neuron
* @param neuron is position in layer
* @returns Specific neuron
*/
NeuronInterface& operator[](const std::size_t& neuron) {
return *neurons[neuron];
}
/**
* @brief This is a virtual function for selecting neuron
* @param neuron is position in layer
* @returns Specific neuron
*/
const NeuronInterface& operator[](const std::size_t& neuron) const {
return *neurons[neuron];
}
void solve(const std::vector<float> &input, std::vector<float> &output);
/**
* @returns Size of layer
*/
std::size_t size() const {
return neurons.size();
}
void setInputSize(std::size_t size) {
for(auto& neuron:neurons) {
neuron->setInputSize(size);
}
}
virtual SimpleJSON::Type::Object serialize() const override;
static std::unique_ptr<Layer> deserialize(const SimpleJSON::Type::Object&);
typedef SimpleJSON::Factory<Layer> Factory;
protected:
std::vector<NeuronInterface*> neurons;
private:
Layer():neurons() {
}
SIMPLEJSON_REGISTER(NeuralNetwork::FeedForward::Layer::Factory, NeuralNetwork::FeedForward::Layer,NeuralNetwork::FeedForward::Layer)
};
}
}