92 lines
1.9 KiB
C++
92 lines
1.9 KiB
C++
#pragma once
|
|
|
|
#include <cstddef>
|
|
#include <vector>
|
|
|
|
#include "../Neuron.h"
|
|
#include "../Stringifiable.h"
|
|
|
|
namespace NeuralNetwork {
|
|
namespace FeedForward {
|
|
|
|
/**
|
|
* @author Tomas Cernik (Tom.Cernik@gmail.com)
|
|
* @brief Class for Layer of FeedForward network
|
|
*/
|
|
class Layer : public Stringifiable {
|
|
|
|
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 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];
|
|
}
|
|
|
|
void solve(const std::vector<float> &input, std::vector<float> &output);
|
|
|
|
/**
|
|
* @returns Size of layer
|
|
*/
|
|
std::size_t size() {
|
|
return neurons.size();
|
|
}
|
|
|
|
void setInputSize(std::size_t size) {
|
|
for(auto& neuron:neurons) {
|
|
neuron->setInputSize(size);
|
|
}
|
|
}
|
|
|
|
using Stringifiable::stringify;
|
|
virtual void stringify(std::ostream& out) const override {
|
|
out << "{" << std::endl;
|
|
out << "\t \"class\": \"NeuralNetwork::FeedForward::Layer\"," << std::endl;
|
|
out << "\t \"neurons\": [" << std::endl;
|
|
bool first=true;
|
|
for(auto &neuron: neurons) {
|
|
if(!first)
|
|
out << ", ";
|
|
out << neuron->stringify();
|
|
first=false;
|
|
}
|
|
out << "]";
|
|
out << "}";
|
|
}
|
|
protected:
|
|
std::vector<NeuronInterface*> neurons;
|
|
};
|
|
}
|
|
} |