Neuron change
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "../Neuron.h"
|
||||
#include "../Stringifiable.h"
|
||||
|
||||
namespace NeuralNetwork {
|
||||
namespace FeedForward {
|
||||
@@ -12,19 +13,46 @@ namespace FeedForward {
|
||||
* @author Tomas Cernik (Tom.Cernik@gmail.com)
|
||||
* @brief Class for Layer of FeedForward network
|
||||
*/
|
||||
class Layer
|
||||
{
|
||||
public:
|
||||
class Layer : public Stringifiable {
|
||||
|
||||
~Layer() {};
|
||||
public:
|
||||
Layer(std::size_t size = 0):neurons() {
|
||||
neurons.push_back(new BiasNeuron);
|
||||
for(std::size_t i=0;i<size;i++) {
|
||||
neurons.push_back(new Neuron(neurons.size()));
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
Neuron& operator[](const std::size_t& neuron) {
|
||||
return neurons[neuron];
|
||||
NeuronInterface& operator[](const std::size_t& neuron) {
|
||||
return *neurons[neuron];
|
||||
}
|
||||
|
||||
void solve(const std::vector<float> &input, std::vector<float> &output);
|
||||
@@ -36,8 +64,29 @@ namespace FeedForward {
|
||||
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<Neuron> neurons;
|
||||
std::vector<NeuronInterface*> neurons;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -25,9 +25,23 @@ namespace FeedForward {
|
||||
* @param _outputSize is size of output from network
|
||||
* @param hiddenUnits is number of hiddenUnits to be created
|
||||
*/
|
||||
inline Network(size_t _inputSize):NeuralNetwork::Network() {
|
||||
inline Network(size_t _inputSize):NeuralNetwork::Network(),layers() {
|
||||
appendLayer(_inputSize);
|
||||
};
|
||||
|
||||
Layer& appendLayer(std::size_t size=1) {
|
||||
layers.push_back(Layer(size));
|
||||
|
||||
if(layers.size() > 1)
|
||||
layers.back().setInputSize(layers[layers.size()-2].size());
|
||||
|
||||
return layers.back();
|
||||
}
|
||||
|
||||
Layer& operator[](const std::size_t &id) {
|
||||
return layers[id];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Virtual destructor for Network
|
||||
*/
|
||||
@@ -44,6 +58,19 @@ namespace FeedForward {
|
||||
using NeuralNetwork::Network::stringify;
|
||||
|
||||
void stringify(std::ostream& out) const override {
|
||||
out << "{" << std::endl;
|
||||
out << "\t \"class\": \"NeuralNetwork::FeedForward::Network\"," << std::endl;
|
||||
out << "\t \"layers\": [" << std::endl;
|
||||
bool first=true;
|
||||
for(auto &layer:layers) {
|
||||
if(!first) {
|
||||
out << ",";
|
||||
}
|
||||
out << layer;
|
||||
first=false;
|
||||
}
|
||||
out << "]";
|
||||
out << "}";
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
Reference in New Issue
Block a user