serializatioin / deserialization and tests
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Neuron.h"
|
||||
|
||||
#include <SimpleJSON/SerializableObject.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
#include "../Neuron.h"
|
||||
#include "../Stringifiable.h"
|
||||
|
||||
namespace NeuralNetwork {
|
||||
namespace FeedForward {
|
||||
|
||||
@@ -13,9 +14,10 @@ namespace FeedForward {
|
||||
* @author Tomas Cernik (Tom.Cernik@gmail.com)
|
||||
* @brief Class for Layer of FeedForward network
|
||||
*/
|
||||
class Layer : public Stringifiable {
|
||||
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++) {
|
||||
@@ -70,11 +72,18 @@ namespace FeedForward {
|
||||
}
|
||||
}
|
||||
|
||||
using Stringifiable::stringify;
|
||||
virtual void stringify(std::ostream& out) const override;
|
||||
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)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,8 @@
|
||||
#include "Layer.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <limits>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace NeuralNetwork {
|
||||
namespace FeedForward {
|
||||
|
||||
@@ -63,24 +58,29 @@ 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;
|
||||
virtual SimpleJSON::Type::Object serialize() const override {
|
||||
std::vector<SimpleJSON::Value> layersSerialized;
|
||||
for(std::size_t i=0;i<layers.size();i++) {
|
||||
layersSerialized.push_back(layers[i]->serialize());
|
||||
}
|
||||
out << "]";
|
||||
out << "}";
|
||||
return {
|
||||
{"class", "NeuralNetwork::FeedForward::Network"},
|
||||
{"layers", layersSerialized },
|
||||
};
|
||||
}
|
||||
|
||||
static std::unique_ptr<Network> deserialize(const SimpleJSON::Type::Object&);
|
||||
|
||||
typedef SimpleJSON::Factory<Network> Factory;
|
||||
|
||||
protected:
|
||||
std::vector<Layer*> layers;
|
||||
|
||||
private:
|
||||
inline Network():NeuralNetwork::Network(),layers() {
|
||||
};
|
||||
|
||||
SIMPLEJSON_REGISTER(NeuralNetwork::FeedForward::Network::Factory, NeuralNetwork::FeedForward::Network,NeuralNetwork::FeedForward::Network::deserialize)
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user