serializatioin / deserialization and tests
This commit is contained in:
@@ -1,6 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <SimpleJSON/SerializableObject.h>
|
||||
#include <SimpleJSON/Factory.h>
|
||||
|
||||
#define NEURAL_NETWORK_REGISTER_ACTIVATION_FUNCTION(name,function) SIMPLEJSON_REGISTER(NeuralNetwork::ActivationFunction::Factory,name,function)
|
||||
/*public: \
|
||||
static const class __FACT_REGISTER_ {\
|
||||
public: \
|
||||
__FACT_REGISTER_() {\
|
||||
NeuralNetwork::ActivationFunction::Factory::registerCreator( #name ,function);\
|
||||
}\
|
||||
} __FACT_REGISTER;
|
||||
*/
|
||||
#define NEURAL_NETWORK_REGISTER_ACTIVATION_FUNCTION_FINISH(name,function) SIMPLEJSON_REGISTER_FINISH(NeuralNetwork::ActivationFunction::Factory,name,function)
|
||||
|
||||
namespace NeuralNetwork {
|
||||
namespace ActivationFunction {
|
||||
@@ -9,7 +21,7 @@ namespace ActivationFunction {
|
||||
* @author Tomas Cernik (Tom.Cernik@gmail.com)
|
||||
* @brief Abstract class of activation function
|
||||
*/
|
||||
class ActivationFunction {
|
||||
class ActivationFunction : public SimpleJSON::SerializableObject {
|
||||
public:
|
||||
|
||||
virtual ~ActivationFunction() {}
|
||||
@@ -41,11 +53,10 @@ namespace ActivationFunction {
|
||||
*/
|
||||
virtual ActivationFunction* clone() const = 0;
|
||||
|
||||
/**
|
||||
* @brief This is a virtual function for storing Activation function
|
||||
* @returns json describing function
|
||||
*/
|
||||
virtual std::string stringify() const =0;
|
||||
const static bool FORCE_REGISTER;
|
||||
};
|
||||
|
||||
typedef SimpleJSON::Factory<ActivationFunction> Factory;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -16,12 +16,18 @@ namespace ActivationFunction {
|
||||
return new Heaviside(lambda);
|
||||
}
|
||||
|
||||
virtual std::string stringify() const override {
|
||||
return "{ \"class\": \"NeuralNetwork::ActivationFunction::Heaviside\", \"lamba\" : "+std::to_string(lambda)+"}";
|
||||
virtual SimpleJSON::Type::Object serialize() const override {
|
||||
return {{"class", "NeuralNetwork::ActivationFunction::Heaviside"}, {"lambda", lambda}};
|
||||
}
|
||||
|
||||
static std::unique_ptr<Heaviside> deserialize(const SimpleJSON::Type::Object &obj) {
|
||||
return std::unique_ptr<Heaviside>(new Heaviside(obj["lambda"].as<double>()));
|
||||
}
|
||||
|
||||
protected:
|
||||
float lambda;
|
||||
|
||||
NEURAL_NETWORK_REGISTER_ACTIVATION_FUNCTION(NeuralNetwork::ActivationFunction::Heaviside, Heaviside::deserialize)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -13,17 +13,24 @@ namespace ActivationFunction {
|
||||
|
||||
inline virtual float derivatedOutput(const float &,const float &output) const override { return lambda*(1-output*output); }
|
||||
|
||||
inline virtual float operator()(const float &x) const override { return tanh(lambda*x); };
|
||||
inline virtual float operator()(const float &x) const override { return tanh(lambda*x); }
|
||||
;
|
||||
virtual ActivationFunction* clone() const override {
|
||||
return new HyperbolicTangent(lambda);
|
||||
}
|
||||
|
||||
virtual std::string stringify() const override {
|
||||
return "{ \"class\": \"NeuralNetwork::ActivationFunction::HyperbolicTangent\", \"lamba\" : "+std::to_string(lambda)+"}";
|
||||
virtual SimpleJSON::Type::Object serialize() const override {
|
||||
return {{"class", "NeuralNetwork::ActivationFunction::HyperbolicTangent"}, {"lambda", lambda}};
|
||||
}
|
||||
|
||||
static std::unique_ptr<HyperbolicTangent> deserialize(const SimpleJSON::Type::Object &obj) {
|
||||
return std::unique_ptr<HyperbolicTangent>(new HyperbolicTangent(obj["lambda"].as<double>()));
|
||||
}
|
||||
|
||||
protected:
|
||||
float lambda;
|
||||
|
||||
NEURAL_NETWORK_REGISTER_ACTIVATION_FUNCTION(NeuralNetwork::ActivationFunction::HyperbolicTangent, HyperbolicTangent::deserialize)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include "./ActivationFunction.h"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace NeuralNetwork {
|
||||
namespace ActivationFunction {
|
||||
|
||||
@@ -17,12 +19,17 @@ namespace ActivationFunction {
|
||||
return new Linear(lambda);
|
||||
}
|
||||
|
||||
virtual std::string stringify() const override {
|
||||
return "{ \"class\": \"NeuralNetwork::ActivationFunction::Linear\", \"lamba\" : "+std::to_string(lambda)+"}";
|
||||
virtual SimpleJSON::Type::Object serialize() const override {
|
||||
return {{"class", "NeuralNetwork::ActivationFunction::Linear"}, {"lambda", lambda}};
|
||||
}
|
||||
|
||||
static std::unique_ptr<Linear> deserialize(const SimpleJSON::Type::Object &obj) {
|
||||
return std::unique_ptr<Linear>(new Linear(obj["lambda"].as<double>()));
|
||||
}
|
||||
|
||||
protected:
|
||||
float lambda;
|
||||
NEURAL_NETWORK_REGISTER_ACTIVATION_FUNCTION(NeuralNetwork::ActivationFunction::Linear, Linear::deserialize)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -29,11 +29,18 @@ namespace ActivationFunction {
|
||||
return new Sigmoid(lambda);
|
||||
}
|
||||
|
||||
virtual std::string stringify() const override {
|
||||
return "{ \"class\": \"NeuralNetwork::ActivationFunction::Sigmoid\", \"lamba\" : "+std::to_string(lambda)+"}";
|
||||
virtual SimpleJSON::Type::Object serialize() const override {
|
||||
return {{"class", "NeuralNetwork::ActivationFunction::Sigmoid"}, {"lambda", lambda}};
|
||||
}
|
||||
|
||||
static std::unique_ptr<Sigmoid> deserialize(const SimpleJSON::Type::Object &obj) {
|
||||
return std::unique_ptr<Sigmoid>(new Sigmoid(obj["lambda"].as<double>()));
|
||||
}
|
||||
|
||||
protected:
|
||||
float lambda;
|
||||
|
||||
NEURAL_NETWORK_REGISTER_ACTIVATION_FUNCTION(NeuralNetwork::ActivationFunction::Sigmoid, Sigmoid::deserialize)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,7 @@ namespace ActivationFunction {
|
||||
*/
|
||||
class StreamingActivationFunction : public ActivationFunction {
|
||||
public:
|
||||
|
||||
virtual float operator()(const float &x) const=0;
|
||||
using ActivationFunction::operator();
|
||||
|
||||
/**
|
||||
* @brief Returns value of four outputs
|
||||
|
||||
Reference in New Issue
Block a user