serializatioin / deserialization and tests
This commit is contained in:
@@ -26,18 +26,12 @@ OPTION(USE_SSE2 "If SSE 2 instruction set should be used." ON)
|
||||
OPTION(USE_AVX "If AVX instruction set should be used." OFF)
|
||||
OPTION(USE_FMA "If FMA instruction set should be used." OFF)
|
||||
|
||||
OPTION(DEBUG "If library should be compiled with DEBUG" OFF)
|
||||
OPTION(ENABLE_TESTS "enables tests" ON)
|
||||
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Weffc++ -Wshadow -Wstrict-aliasing -ansi -Woverloaded-virtual -Wdelete-non-virtual-dtor -Wno-unused-function")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++14")
|
||||
|
||||
if(DEBUG)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
|
||||
else()
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -mtune=native -O3")
|
||||
endif(DEBUG)
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native -mtune=native -O3")
|
||||
|
||||
if(USE_AVX)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx -DUSE_AVX")
|
||||
@@ -55,7 +49,7 @@ if(USE_SSE)
|
||||
endif(USE_SSE)
|
||||
|
||||
include_directories(./include/)
|
||||
include_directories(./lib/SimpleJSON/src/)
|
||||
include_directories(./lib/SimpleJSON/include)
|
||||
|
||||
set (LIBRARY_SOURCES
|
||||
src/sse_mathfun.cpp
|
||||
@@ -65,13 +59,13 @@ set (LIBRARY_SOURCES
|
||||
src/NeuralNetwork/Learning/PerceptronLearning.cpp
|
||||
|
||||
src/NeuralNetwork/BasisFunction/Linear.cpp
|
||||
|
||||
src/NeuralNetwork/FeedForward/Layer.cpp
|
||||
src/NeuralNetwork/FeedForward/Network.cpp
|
||||
src/NeuralNetwork/Recurrent/Network.cpp
|
||||
src/NeuralNetwork/Neuron.cpp
|
||||
|
||||
lib/SimpleJSON/src/JSON.cpp
|
||||
lib/SimpleJSON/src/JSONValue.cpp
|
||||
src/NeuralNetwork/Neuron.cpp
|
||||
src/NeuralNetwork/IMPL.cpp
|
||||
)
|
||||
|
||||
add_library(NeuralNetwork STATIC ${LIBRARY_SOURCES})
|
||||
@@ -88,7 +82,7 @@ add_subdirectory (tests)
|
||||
|
||||
enable_testing()
|
||||
|
||||
add_test(activation tests/backpropagation)
|
||||
add_test(activation tests/activation)
|
||||
set_property(TEST activation PROPERTY LABELS unit)
|
||||
|
||||
add_test(backpropagation tests/backpropagation)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <SimpleJSON/SerializableObject.h>
|
||||
#include <SimpleJSON/Factory.h>
|
||||
|
||||
#include <math.h>
|
||||
#include <vector>
|
||||
|
||||
#include <string>
|
||||
|
||||
#define NEURAL_NETWORK_REGISTER_BASIS_FUNCTION(name,function) SIMPLEJSON_REGISTER(NeuralNetwork::BasisFunction::Factory,name,function)
|
||||
#define NEURAL_NETWORK_REGISTER_BASIS_FUNCTION_FINISH(name,function) SIMPLEJSON_REGISTER_FINISH(NeuralNetwork::BasisFunction::Factory,name,function)
|
||||
|
||||
namespace NeuralNetwork {
|
||||
namespace BasisFunction {
|
||||
class BasisFunction {
|
||||
class BasisFunction : public SimpleJSON::SerializableObject {
|
||||
public:
|
||||
virtual ~BasisFunction() {}
|
||||
virtual float operator()(const std::vector<float>& weights, const std::vector<float>& input) const =0;
|
||||
@@ -15,13 +21,9 @@ namespace BasisFunction {
|
||||
/**
|
||||
* @brief Function returns clone of object
|
||||
*/
|
||||
virtual BasisFunction* clone() const = 0;
|
||||
|
||||
/**
|
||||
* @brief This is a virtual function for storing Basis function
|
||||
* @returns json describing function
|
||||
*/
|
||||
virtual std::string stringify() const =0;
|
||||
virtual std::unique_ptr<BasisFunction> clone() const = 0;
|
||||
};
|
||||
|
||||
typedef SimpleJSON::Factory<BasisFunction> Factory;
|
||||
}
|
||||
}
|
||||
@@ -22,14 +22,18 @@ namespace BasisFunction {
|
||||
|
||||
virtual float operator()(const std::vector<float>& weights, const std::vector<float>& input) const override;
|
||||
|
||||
virtual BasisFunction* clone() const override {
|
||||
return new Linear();
|
||||
virtual std::unique_ptr<BasisFunction> clone() const override {
|
||||
return std::unique_ptr<BasisFunction>(new Linear());
|
||||
}
|
||||
|
||||
virtual std::string stringify() const override {
|
||||
return "{ \"class\": \"NeuralNetwork::BasisFunction::Linear\" }";
|
||||
virtual SimpleJSON::Type::Object serialize() const override {
|
||||
return {{"class", "NeuralNetwork::BasisFunction::Linear"}};
|
||||
}
|
||||
|
||||
static std::unique_ptr<Linear> deserialize(const SimpleJSON::Type::Object &) {
|
||||
return std::unique_ptr<Linear>(new Linear());
|
||||
}
|
||||
NEURAL_NETWORK_REGISTER_BASIS_FUNCTION(NeuralNetwork::BasisFunction::Linear, deserialize)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -21,14 +21,19 @@ namespace BasisFunction {
|
||||
return product;
|
||||
}
|
||||
|
||||
virtual Product* clone() const override {
|
||||
return new Product();
|
||||
virtual std::unique_ptr<BasisFunction> clone() const override {
|
||||
return std::unique_ptr<BasisFunction>(new Product());
|
||||
}
|
||||
|
||||
virtual std::string stringify() const override {
|
||||
return "{ \"class\": \"NeuralNetwork::BasisFunction::Product\" }";
|
||||
virtual SimpleJSON::Type::Object serialize() const override {
|
||||
return {{"class", "NeuralNetwork::BasisFunction::Product"}};
|
||||
}
|
||||
|
||||
static std::unique_ptr<Product> deserialize(const SimpleJSON::Type::Object &) {
|
||||
return std::unique_ptr<Product>(new Product());
|
||||
}
|
||||
|
||||
NEURAL_NETWORK_REGISTER_BASIS_FUNCTION(NeuralNetwork::BasisFunction::Product, deserialize)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
#ifndef __BASIS_RADIAL_H_
|
||||
#define __BASIS_RADIAL_H_
|
||||
#pragma once
|
||||
|
||||
#include "./BasisFunction.h"
|
||||
|
||||
@@ -20,14 +19,19 @@ namespace BasisFunction
|
||||
return sqrt(sum);
|
||||
}
|
||||
|
||||
virtual BasisFunction* clone() const override {
|
||||
return new Radial();
|
||||
virtual std::unique_ptr<BasisFunction> clone() const override {
|
||||
return std::unique_ptr<BasisFunction>(new Radial());
|
||||
}
|
||||
|
||||
virtual std::string stringify() const override {
|
||||
return "{ \"class\": \"NeuralNetwork::BasisFunction::Radial\" }";
|
||||
virtual SimpleJSON::Type::Object serialize() const override {
|
||||
return {{"class", "NeuralNetwork::BasisFunction::Radial"}};
|
||||
}
|
||||
|
||||
static std::unique_ptr<Radial> deserialize(const SimpleJSON::Type::Object &) {
|
||||
return std::unique_ptr<Radial>(new Radial());
|
||||
}
|
||||
|
||||
NEURAL_NETWORK_REGISTER_BASIS_FUNCTION(NeuralNetwork::BasisFunction::Radial, deserialize)
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -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)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "Neuron.h"
|
||||
|
||||
#include <SimpleJSON/SerializableObject.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
#include "Neuron.h"
|
||||
|
||||
#include "Stringifiable.h"
|
||||
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#define NEURAL_NETWORK_INIT() const static bool ______TMP= NeuralNetwork::Network::loaded()
|
||||
|
||||
namespace NeuralNetwork
|
||||
{
|
||||
@@ -17,13 +16,15 @@ namespace NeuralNetwork
|
||||
* @author Tomas Cernik (Tom.Cernik@gmail.com)
|
||||
* @brief Abstract model of simple Network
|
||||
*/
|
||||
class Network : public Stringifiable
|
||||
class Network : public SimpleJSON::SerializableObject
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor for Network
|
||||
*/
|
||||
inline Network() {};
|
||||
inline Network() {
|
||||
loaded();
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Virtual destructor for Network
|
||||
@@ -49,5 +50,8 @@ namespace NeuralNetwork
|
||||
* @brief Number of threads used by network
|
||||
*/
|
||||
unsigned threads=1;
|
||||
|
||||
public:
|
||||
static bool loaded();
|
||||
};
|
||||
}
|
||||
@@ -1,22 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <NeuralNetwork/ActivationFunction/Sigmoid.h>
|
||||
#include <NeuralNetwork/BasisFunction/Linear.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <sstream>
|
||||
#include <limits>
|
||||
|
||||
#include <NeuralNetwork/ActivationFunction/Sigmoid.h>
|
||||
#include <NeuralNetwork/BasisFunction/Linear.h>
|
||||
|
||||
namespace NeuralNetwork
|
||||
{
|
||||
/**
|
||||
* @author Tomas Cernik (Tom.Cernik@gmail.com)
|
||||
* @brief Abstract class of neuron. All Neuron classes should derive from this on
|
||||
*/
|
||||
class NeuronInterface
|
||||
{
|
||||
class NeuronInterface : public SimpleJSON::SerializableObject {
|
||||
public:
|
||||
NeuronInterface(const unsigned long &_id=0): id(_id), weights(1),_output(1),
|
||||
_value(0) {
|
||||
@@ -33,12 +31,6 @@ namespace NeuralNetwork
|
||||
*/
|
||||
virtual ~NeuronInterface() {};
|
||||
|
||||
/**
|
||||
* @brief This is a virtual function for storing network
|
||||
* @returns json describing network and it's state
|
||||
*/
|
||||
virtual std::string stringify(const std::string &prefix="") const =0;
|
||||
|
||||
/**
|
||||
* @brief getter for neuron weight
|
||||
* @param &neuron is neuron it's weight is returned
|
||||
@@ -118,6 +110,8 @@ namespace NeuralNetwork
|
||||
* @brief id is identificator if neuron
|
||||
*/
|
||||
const unsigned long id;
|
||||
|
||||
typedef SimpleJSON::Factory<NeuronInterface> Factory;
|
||||
protected:
|
||||
std::vector<float> weights;
|
||||
float _output;
|
||||
@@ -128,8 +122,7 @@ namespace NeuralNetwork
|
||||
* @author Tomas Cernik (Tom.Cernik@gmail.com)
|
||||
* @brief Class of FeedForward neuron.
|
||||
*/
|
||||
class Neuron: public NeuronInterface
|
||||
{
|
||||
class Neuron: public NeuronInterface {
|
||||
public:
|
||||
Neuron(unsigned long _id=0, const ActivationFunction::ActivationFunction &activationFunction=ActivationFunction::Sigmoid(-4.9)):
|
||||
NeuronInterface(_id), basis(new BasisFunction::Linear),
|
||||
@@ -137,7 +130,7 @@ namespace NeuralNetwork
|
||||
_output=0.0;
|
||||
}
|
||||
|
||||
Neuron(const Neuron &r): NeuronInterface(r), basis(r.basis->clone()), activation(r.activation->clone()) {
|
||||
Neuron(const Neuron &r): NeuronInterface(r), basis(r.basis->clone().release()), activation(r.activation->clone()) {
|
||||
}
|
||||
|
||||
virtual ~Neuron() {
|
||||
@@ -147,8 +140,6 @@ namespace NeuralNetwork
|
||||
|
||||
Neuron operator=(const Neuron&) = delete;
|
||||
|
||||
virtual std::string stringify(const std::string &prefix="") const override;
|
||||
|
||||
float operator()(const std::vector<float>& inputs) {
|
||||
//compute value
|
||||
_value=basis->operator()(weights,inputs);
|
||||
@@ -174,7 +165,7 @@ namespace NeuralNetwork
|
||||
|
||||
virtual void setBasisFunction(const BasisFunction::BasisFunction& basisFunction) override {
|
||||
delete basis;
|
||||
basis=basisFunction.clone();
|
||||
basis=basisFunction.clone().release();
|
||||
|
||||
}
|
||||
|
||||
@@ -182,10 +173,16 @@ namespace NeuralNetwork
|
||||
delete activation;
|
||||
activation=activationFunction.clone();
|
||||
}
|
||||
|
||||
virtual SimpleJSON::Type::Object serialize() const override;
|
||||
|
||||
static std::unique_ptr<Neuron> deserialize(const SimpleJSON::Type::Object &obj);
|
||||
protected:
|
||||
|
||||
BasisFunction::BasisFunction *basis;
|
||||
ActivationFunction::ActivationFunction *activation;
|
||||
|
||||
SIMPLEJSON_REGISTER(NeuralNetwork::NeuronInterface::Factory, NeuralNetwork::Neuron,NeuralNetwork::Neuron::deserialize)
|
||||
};
|
||||
|
||||
class BiasNeuron: public NeuronInterface {
|
||||
@@ -203,8 +200,6 @@ namespace NeuralNetwork
|
||||
std::string text;
|
||||
};
|
||||
|
||||
virtual std::string stringify(const std::string& prefix = "") const override { return prefix+"{ \"class\" : \"NeuralNetwork::BiasNeuron\" }"; }
|
||||
|
||||
virtual float operator()(const std::vector< float >&) override { return 1.0; }
|
||||
|
||||
virtual BiasNeuron* clone() const { return new BiasNeuron(); }
|
||||
@@ -226,6 +221,15 @@ namespace NeuralNetwork
|
||||
throw usageException("activation function");
|
||||
}
|
||||
|
||||
virtual SimpleJSON::Type::Object serialize() const override {
|
||||
return {{"class", "NeuralNetwork::BiasNeuron"}};
|
||||
}
|
||||
|
||||
static std::unique_ptr<BiasNeuron> deserialize(const SimpleJSON::Type::Object &) {
|
||||
return std::unique_ptr<BiasNeuron>(new BiasNeuron());
|
||||
}
|
||||
|
||||
SIMPLEJSON_REGISTER(NeuralNetwork::NeuronInterface::Factory, NeuralNetwork::BiasNeuron,NeuralNetwork::BiasNeuron::deserialize)
|
||||
};
|
||||
|
||||
class InputNeuron: public NeuronInterface {
|
||||
@@ -247,8 +251,6 @@ namespace NeuralNetwork
|
||||
|
||||
}
|
||||
|
||||
virtual std::string stringify(const std::string& prefix = "") const override { return prefix+"{ \"class\" : \"NeuralNetwork::InputNeuron\", \"id\": "+std::to_string(id)+" }"; }
|
||||
|
||||
virtual float operator()(const std::vector< float >&) override { return 1.0; }
|
||||
|
||||
virtual InputNeuron* clone() const { return new InputNeuron(id); }
|
||||
@@ -269,5 +271,15 @@ namespace NeuralNetwork
|
||||
virtual void setActivationFunction(const ActivationFunction::ActivationFunction &) override {
|
||||
throw usageException("activation function");
|
||||
}
|
||||
|
||||
virtual SimpleJSON::Type::Object serialize() const override {
|
||||
return {{"class", "NeuralNetwork::InputNeuron"}, {"id", id}};
|
||||
}
|
||||
|
||||
static std::unique_ptr<NeuronInterface> deserialize(const SimpleJSON::Type::Object &obj) {
|
||||
return std::unique_ptr<NeuronInterface>(new InputNeuron(obj["id"].as<int>()));
|
||||
}
|
||||
|
||||
SIMPLEJSON_REGISTER(NeuralNetwork::NeuronInterface::Factory, NeuralNetwork::InputNeuron,NeuralNetwork::InputNeuron::deserialize)
|
||||
};
|
||||
}
|
||||
@@ -40,6 +40,15 @@ namespace Recurrent {
|
||||
}
|
||||
};
|
||||
|
||||
Network(const Network &r) :inputSize(r.inputSize), outputSize(r.outputSize), neurons(0), outputs(r.outputs) {
|
||||
neurons.push_back(new NeuralNetwork::BiasNeuron());
|
||||
for(std::size_t i=1;i<r.neurons.size();i++) {
|
||||
neurons.push_back(r.neurons[i]->clone());
|
||||
}
|
||||
}
|
||||
|
||||
Network& operator=(const Network&r);
|
||||
|
||||
/**
|
||||
* @brief Virtual destructor for Network
|
||||
*/
|
||||
@@ -70,9 +79,7 @@ namespace Recurrent {
|
||||
return neurons;
|
||||
}
|
||||
|
||||
using NeuralNetwork::Network::stringify;
|
||||
|
||||
void stringify(std::ostream& out) const override;
|
||||
virtual SimpleJSON::Type::Object serialize() const override;
|
||||
|
||||
NeuronInterface& addNeuron() {
|
||||
neurons.push_back(new Neuron(neurons.size()));
|
||||
@@ -90,12 +97,17 @@ namespace Recurrent {
|
||||
*/
|
||||
NeuralNetwork::Recurrent::Network connectWith(const NeuralNetwork::Recurrent::Network &r) const;
|
||||
|
||||
static std::unique_ptr<Network> deserialize(const SimpleJSON::Type::Object&);
|
||||
|
||||
typedef SimpleJSON::Factory<Network> Factory;
|
||||
protected:
|
||||
size_t inputSize=0;
|
||||
size_t outputSize=0;
|
||||
|
||||
std::vector<NeuronInterface*> neurons;
|
||||
std::vector<float> outputs;
|
||||
|
||||
SIMPLEJSON_REGISTER(NeuralNetwork::Recurrent::Network::Factory,NeuralNetwork::Recurrent::Network, deserialize)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace NeuralNetwork {
|
||||
class Stringifiable {
|
||||
public:
|
||||
|
||||
virtual ~Stringifiable() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This is a virtual function for class
|
||||
*/
|
||||
virtual void stringify(std::ostream& out) const =0;
|
||||
|
||||
virtual std::string stringify() final {
|
||||
std::ostringstream s;
|
||||
stringify(s);
|
||||
return s.str();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
inline static std::ostream& operator<<(std::ostream& o, const Stringifiable& n) {
|
||||
n.stringify(o);
|
||||
return o;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
namespace Array {
|
||||
|
||||
/**
|
||||
* @author Tomas Cernik (Tom.Cernik@gmail.com)
|
||||
* @brief Template of array for simple usage
|
||||
*/
|
||||
|
||||
template <typename T>
|
||||
class Array {
|
||||
public:
|
||||
|
||||
Array(unsigned long size=0):arr(size==0? nullptr: new T[size]),_size(0) {
|
||||
|
||||
}
|
||||
|
||||
Array (const Array& r):arr(r.arr),_size(r._size) {
|
||||
}
|
||||
|
||||
~Array() {
|
||||
}
|
||||
|
||||
inline Array& operator=(const Array& r) {
|
||||
arr=r.arr;
|
||||
_size=r._size;
|
||||
}
|
||||
|
||||
inline void resize(unsigned long size) {
|
||||
if(arr==nullptr) {
|
||||
arr=new T[size];
|
||||
_size=size;
|
||||
}else {
|
||||
T* tmp=new T[size];
|
||||
for(unsigned long i=0;i<_size;i++) {
|
||||
tmp[i]=arr[i];
|
||||
}
|
||||
delete[] arr;
|
||||
arr=tmp;
|
||||
}
|
||||
}
|
||||
|
||||
inline void free() {
|
||||
delete[] arr;
|
||||
arr=nullptr;
|
||||
_size=0;
|
||||
}
|
||||
|
||||
inline const T& operator[](unsigned long i) const {
|
||||
return arr[i];
|
||||
}
|
||||
|
||||
inline T& operator[](unsigned long i) {
|
||||
return arr[i];
|
||||
}
|
||||
|
||||
unsigned long size() const {
|
||||
return _size;
|
||||
}
|
||||
|
||||
protected:
|
||||
T* arr;
|
||||
unsigned long _size;
|
||||
private:
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class DynamicArray: public Array<T> {
|
||||
public:
|
||||
DynamicArray(unsigned long size=0,float _scaleFactor=1):Array<T>(size),scaleFactor(_scaleFactor) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
float scaleFactor;
|
||||
private:
|
||||
};
|
||||
}
|
||||
Submodule lib/SimpleJSON updated: 62e1e8b57e...2eeb3e0b84
@@ -1,5 +1,7 @@
|
||||
#include <NeuralNetwork/FeedForward/Layer.h>
|
||||
|
||||
SIMPLEJSON_REGISTER_FINISH(NeuralNetwork::FeedForward::Layer::Factory, NeuralNetwork::FeedForward::Layer,NeuralNetwork::FeedForward::Layer::deserialize)
|
||||
|
||||
void NeuralNetwork::FeedForward::Layer::solve(const std::vector<float> &input, std::vector<float> &output) {
|
||||
output.resize(neurons.size());
|
||||
|
||||
@@ -8,17 +10,22 @@ void NeuralNetwork::FeedForward::Layer::solve(const std::vector<float> &input, s
|
||||
}
|
||||
}
|
||||
|
||||
void NeuralNetwork::FeedForward::Layer::stringify(std::ostream &out) const {
|
||||
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;
|
||||
SimpleJSON::Type::Object NeuralNetwork::FeedForward::Layer::serialize() const {
|
||||
std::vector<SimpleJSON::Value> neuronsSerialized;
|
||||
for(std::size_t i=0;i<neurons.size();i++) {
|
||||
neuronsSerialized.push_back(neurons[i]->serialize());
|
||||
}
|
||||
out << "]";
|
||||
out << "}";
|
||||
return {{"class", "NeuralNetwork::FeedForward::Layer"},
|
||||
{"neurons" , neuronsSerialized}
|
||||
};
|
||||
}
|
||||
|
||||
std::unique_ptr<NeuralNetwork::FeedForward::Layer> NeuralNetwork::FeedForward::Layer::deserialize(const SimpleJSON::Type::Object &obj) {
|
||||
NeuralNetwork::FeedForward::Layer *layer= new NeuralNetwork::FeedForward::Layer();
|
||||
for(auto& neuron: obj["neurons"].as<SimpleJSON::Type::Array>()) {
|
||||
layer->neurons.push_back(NeuralNetwork::NeuronInterface::Factory::deserialize(neuron.as<SimpleJSON::Type::Object>()).release());
|
||||
}
|
||||
return std::unique_ptr<Layer>(layer);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <NeuralNetwork/FeedForward/Network.h>
|
||||
|
||||
SIMPLEJSON_REGISTER_FINISH(NeuralNetwork::FeedForward::Network::Factory, NeuralNetwork::FeedForward::Network,NeuralNetwork::FeedForward::Network::deserialize)
|
||||
|
||||
std::vector<float> NeuralNetwork::FeedForward::Network::computeOutput(const std::vector<float>& input) {
|
||||
std::vector<float> partialInput(input.size()+1);
|
||||
std::vector<float> partialOutput;
|
||||
@@ -30,4 +32,18 @@ void NeuralNetwork::FeedForward::Network::randomizeWeights() {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<NeuralNetwork::FeedForward::Network> NeuralNetwork::FeedForward::Network::deserialize(const SimpleJSON::Type::Object &obj) {
|
||||
NeuralNetwork::FeedForward::Network* network= new NeuralNetwork::FeedForward::Network();
|
||||
for(auto layers:network->layers) {
|
||||
delete layers;
|
||||
}
|
||||
network->layers.clear();
|
||||
|
||||
for(auto& layerObject: obj["layers"].as<SimpleJSON::Type::Array>()) {
|
||||
network->layers.push_back(NeuralNetwork::FeedForward::Layer::Factory::deserialize(layerObject.as<SimpleJSON::Type::Object>()).release());
|
||||
}
|
||||
return std::unique_ptr<Network>(network);
|
||||
}
|
||||
|
||||
|
||||
24
src/NeuralNetwork/IMPL.cpp
Normal file
24
src/NeuralNetwork/IMPL.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <NeuralNetwork/ActivationFunction/Heaviside.h>
|
||||
#include <NeuralNetwork/ActivationFunction/Linear.h>
|
||||
#include <NeuralNetwork/ActivationFunction/HyperbolicTangent.h>
|
||||
#include <NeuralNetwork/ActivationFunction/Sigmoid.h>
|
||||
|
||||
NEURAL_NETWORK_REGISTER_ACTIVATION_FUNCTION_FINISH(NeuralNetwork::ActivationFunction::Heaviside, Heaviside::deserialize)
|
||||
NEURAL_NETWORK_REGISTER_ACTIVATION_FUNCTION_FINISH(NeuralNetwork::ActivationFunction::HyperbolicTangent, HyperbolicTangent::deserialize)
|
||||
NEURAL_NETWORK_REGISTER_ACTIVATION_FUNCTION_FINISH(NeuralNetwork::ActivationFunction::Linear, Linear::deserialize)
|
||||
NEURAL_NETWORK_REGISTER_ACTIVATION_FUNCTION_FINISH(NeuralNetwork::ActivationFunction::Sigmoid, Sigmoid::deserialize)
|
||||
|
||||
#include <NeuralNetwork/BasisFunction/Linear.h>
|
||||
#include <NeuralNetwork/BasisFunction/Product.h>
|
||||
#include <NeuralNetwork/BasisFunction/Radial.h>
|
||||
|
||||
NEURAL_NETWORK_REGISTER_BASIS_FUNCTION_FINISH(NeuralNetwork::BasisFunction::Linear, deserialize)
|
||||
NEURAL_NETWORK_REGISTER_BASIS_FUNCTION_FINISH(NeuralNetwork::BasisFunction::Product, deserialize)
|
||||
NEURAL_NETWORK_REGISTER_BASIS_FUNCTION_FINISH(NeuralNetwork::BasisFunction::Radial, deserialize)
|
||||
|
||||
|
||||
#include <NeuralNetwork/Network.h>
|
||||
|
||||
bool NeuralNetwork::Network::loaded() {
|
||||
return true;
|
||||
}
|
||||
@@ -1,27 +1,35 @@
|
||||
#include <NeuralNetwork/Neuron.h>
|
||||
|
||||
std::string NeuralNetwork::Neuron::stringify(const std::string &prefix) const {
|
||||
std::ostringstream out;
|
||||
out.precision(std::numeric_limits<float>::digits10+1);
|
||||
SIMPLEJSON_REGISTER_FINISH(NeuralNetwork::NeuronInterface::Factory, NeuralNetwork::Neuron, NeuralNetwork::Neuron::deserialize)
|
||||
SIMPLEJSON_REGISTER_FINISH(NeuralNetwork::NeuronInterface::Factory, NeuralNetwork::BiasNeuron, NeuralNetwork::BiasNeuron::deserialize)
|
||||
SIMPLEJSON_REGISTER_FINISH(NeuralNetwork::NeuronInterface::Factory, NeuralNetwork::InputNeuron, NeuralNetwork::InputNeuron::deserialize)
|
||||
|
||||
out << prefix << "{\n";
|
||||
out << prefix << "\t\"class\": \"NeuralNetwork::Neuron\",\n";
|
||||
out << prefix << "\t\"id\": " << id << ",\n";
|
||||
out << prefix << "\t\"output\": " << output() << ",\n";
|
||||
out << prefix << "\t\"value\": " << value() << ",\n";
|
||||
out << prefix << "\t\"activationFunction\": " << activation->stringify() <<",\n";
|
||||
out << prefix << "\t\"basisFunction\": " << basis->stringify() <<",\n";
|
||||
out << prefix << "\t\"weights\": {";
|
||||
bool first=true;
|
||||
for(std::size_t j=0;j<weights.size();j++) {
|
||||
if(weights[j]!= 0.0) {
|
||||
if(!first)
|
||||
out << ", ";
|
||||
first=false;
|
||||
out << "\"" << j << "\": " << weights[j];
|
||||
}
|
||||
SimpleJSON::Type::Object NeuralNetwork::Neuron::serialize() const{
|
||||
return {
|
||||
{"class", "NeuralNetwork::Neuron"},
|
||||
{"id", id},
|
||||
{"output", output()},
|
||||
{"value", value()},
|
||||
{"activationFunction", *activation},
|
||||
{"basisFunction", *basis},
|
||||
{"weights", weights}
|
||||
};
|
||||
}
|
||||
|
||||
std::unique_ptr<NeuralNetwork::Neuron> NeuralNetwork::Neuron::deserialize(const SimpleJSON::Type::Object &obj) {
|
||||
Neuron *neuron = new Neuron(obj["id"].as<int>());
|
||||
neuron->_output=obj["output"].as<double>();
|
||||
neuron->_value=obj["value"].as<double>();
|
||||
delete neuron->activation;
|
||||
delete neuron->basis;
|
||||
neuron->activation=NeuralNetwork::ActivationFunction::Factory::deserialize(obj["activationFunction"].as<SimpleJSON::Type::Object>()).release();
|
||||
neuron->basis=NeuralNetwork::BasisFunction::Factory::deserialize(obj["basisFunction"].as<SimpleJSON::Type::Object>()).release();
|
||||
const SimpleJSON::Type::Array& weights=obj["weights"].as<SimpleJSON::Type::Array>();
|
||||
neuron->weights.resize(weights.size());
|
||||
for(std::size_t i=0;i<weights.size();i++) {
|
||||
neuron->weights[i]=weights[i].as<double>();
|
||||
}
|
||||
out << "}\n";
|
||||
out << prefix << "}";
|
||||
return out.str();
|
||||
}
|
||||
return std::unique_ptr<NeuralNetwork::Neuron>(neuron);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
#include <NeuralNetwork/Recurrent/Network.h>
|
||||
#include <iostream>
|
||||
|
||||
SIMPLEJSON_REGISTER_FINISH(NeuralNetwork::Recurrent::Network::Factory, NeuralNetwork::Recurrent::Network, NeuralNetwork::Recurrent::Network::deserialize)
|
||||
|
||||
std::vector<float> NeuralNetwork::Recurrent::Network::computeOutput(const std::vector<float>& input, unsigned int iterations) {
|
||||
|
||||
assert(input.size() == inputSize);
|
||||
@@ -36,29 +40,56 @@ std::vector<float> NeuralNetwork::Recurrent::Network::computeOutput(const std::v
|
||||
return ret;
|
||||
}
|
||||
|
||||
void NeuralNetwork::Recurrent::Network::stringify(std::ostream& out) const {
|
||||
out <<std::setprecision(std::numeric_limits<float>::digits10+1);
|
||||
out << "{\n";
|
||||
out << "\t\"class\":\"NeuralNetwork::Recurrent::Network\",\n";
|
||||
out << "\t\"size\":" << neurons.size() << ",\n";
|
||||
out << "\t\"inputs\":" << inputSize << ",\n";
|
||||
out << "\t\"outputs\":" << outputSize << ",\n";
|
||||
|
||||
out << "\t\"neurons\":[\n";
|
||||
|
||||
for(size_t i=0;i<neurons.size();i++) {
|
||||
if(i!=0)
|
||||
out << ",\n";
|
||||
out << neurons[i]->stringify("\t\t");
|
||||
}
|
||||
out << "\n\t]\n";
|
||||
|
||||
out <<"}";
|
||||
}
|
||||
|
||||
NeuralNetwork::Recurrent::Network NeuralNetwork::Recurrent::Network::connectWith(const NeuralNetwork::Recurrent::Network &r) const {
|
||||
|
||||
}
|
||||
|
||||
NeuralNetwork::Recurrent::Network& NeuralNetwork::Recurrent::Network::operator=(const NeuralNetwork::Recurrent::Network&r) {
|
||||
inputSize=r.inputSize;
|
||||
outputSize=r.outputSize;
|
||||
outputs=r.outputs;
|
||||
|
||||
for(std::size_t i=1;i<neurons.size();i++) {
|
||||
delete neurons[i];
|
||||
}
|
||||
|
||||
neurons.resize(1);
|
||||
|
||||
for(std::size_t i=1;i<neurons.size();i++) {
|
||||
neurons.push_back(r.neurons[i]->clone());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
SimpleJSON::Type::Object NeuralNetwork::Recurrent::Network::serialize() const {
|
||||
std::vector<SimpleJSON::Value> neuronsSerialized;
|
||||
for(auto &neuron: neurons) {
|
||||
neuronsSerialized.push_back(neuron->serialize());
|
||||
}
|
||||
return {
|
||||
{"class", "NeuralNetwork::Recurrent::Network"},
|
||||
{"inputSize", inputSize},
|
||||
{"outputSize", outputSize},
|
||||
{"outputs", outputs},
|
||||
{"neurons", neuronsSerialized}
|
||||
};
|
||||
}
|
||||
|
||||
std::unique_ptr<NeuralNetwork::Recurrent::Network> NeuralNetwork::Recurrent::Network::deserialize(const SimpleJSON::Type::Object &obj) {
|
||||
const int inputSize=obj["inputSize"].as<int>();
|
||||
const int outputSize=obj["outputSize"].as<int>();
|
||||
NeuralNetwork::Recurrent::Network *net= new NeuralNetwork::Recurrent::Network(inputSize,outputSize,0) ;
|
||||
for(auto &a: net->neurons) {
|
||||
delete a;
|
||||
}
|
||||
net->neurons.clear();
|
||||
for(const auto& neuronObj: obj["neurons"].as<SimpleJSON::Type::Array>()) {
|
||||
NeuronInterface* neuron=Neuron::Neuron::Factory::deserialize(neuronObj.as<SimpleJSON::Type::Object>()).release();
|
||||
net->neurons.push_back(neuron);
|
||||
}
|
||||
return std::unique_ptr<Network>(net);
|
||||
}
|
||||
|
||||
/*
|
||||
NeuralNetwork::Recurrent::Network NeuralNetwork::Recurrent::Network::connectWith(const NeuralNetwork::Recurrent::Network &r) const {
|
||||
if(outputSize!=r.inputSize) {
|
||||
|
||||
@@ -3,15 +3,17 @@
|
||||
#include <NeuralNetwork/ActivationFunction/HyperbolicTangent.h>
|
||||
#include <NeuralNetwork/ActivationFunction/Linear.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <NeuralNetwork/Network.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
|
||||
union {
|
||||
__m128 v; // SSE 4 x float vector
|
||||
float a[4]; // scalar array of 4 floats
|
||||
} U;
|
||||
|
||||
NEURAL_NETWORK_INIT();
|
||||
|
||||
int main() {
|
||||
{
|
||||
NeuralNetwork::ActivationFunction::Heaviside h(1.0);
|
||||
@@ -68,5 +70,34 @@ int main() {
|
||||
assert(s(1.0) < 0.7001);
|
||||
}
|
||||
|
||||
{
|
||||
NeuralNetwork::ActivationFunction::Linear l(2.5);
|
||||
const std::string tmp = l.serialize().serialize();
|
||||
NeuralNetwork::ActivationFunction::ActivationFunction* deserialized = NeuralNetwork::ActivationFunction::Factory::deserialize(l.serialize()).release();
|
||||
assert(tmp == deserialized->serialize().serialize());
|
||||
delete deserialized;
|
||||
}
|
||||
{
|
||||
NeuralNetwork::ActivationFunction::Heaviside l(2.5);
|
||||
const std::string tmp = l.serialize().serialize();
|
||||
NeuralNetwork::ActivationFunction::ActivationFunction* deserialized = NeuralNetwork::ActivationFunction::Factory::deserialize(l.serialize()).release();
|
||||
assert(tmp == deserialized->serialize().serialize());
|
||||
delete deserialized;
|
||||
}
|
||||
{
|
||||
NeuralNetwork::ActivationFunction::HyperbolicTangent l(2.5);
|
||||
const std::string tmp = l.serialize().serialize();
|
||||
NeuralNetwork::ActivationFunction::ActivationFunction* deserialized = NeuralNetwork::ActivationFunction::Factory::deserialize(l.serialize()).release();
|
||||
assert(tmp == deserialized->serialize().serialize());
|
||||
delete deserialized;
|
||||
}
|
||||
{
|
||||
NeuralNetwork::ActivationFunction::Sigmoid l(2.5);
|
||||
const std::string tmp = l.serialize().serialize();
|
||||
NeuralNetwork::ActivationFunction::ActivationFunction* deserialized = NeuralNetwork::ActivationFunction::Factory::deserialize(l.serialize()).release();
|
||||
assert(tmp == deserialized->serialize().serialize());
|
||||
delete deserialized;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,10 +1,15 @@
|
||||
#include <NeuralNetwork/BasisFunction/Linear.h>
|
||||
#include <NeuralNetwork/BasisFunction/Product.h>
|
||||
#include <NeuralNetwork/BasisFunction/Radial.h>
|
||||
|
||||
#include <NeuralNetwork/Network.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
|
||||
NEURAL_NETWORK_INIT();
|
||||
|
||||
int main() {
|
||||
{
|
||||
NeuralNetwork::BasisFunction::Linear l;
|
||||
@@ -38,4 +43,28 @@ int main() {
|
||||
assert(l(w,i) > 0.05999);
|
||||
assert(l(w,i) < 0.06001);
|
||||
}
|
||||
|
||||
{
|
||||
NeuralNetwork::BasisFunction::Linear l;
|
||||
std::string tmp = l.serialize().serialize();
|
||||
NeuralNetwork::BasisFunction::BasisFunction *deserialized =NeuralNetwork::BasisFunction::Factory::deserialize(l.serialize()).release();
|
||||
assert(tmp==deserialized->serialize().serialize());
|
||||
delete deserialized;
|
||||
}
|
||||
|
||||
{
|
||||
NeuralNetwork::BasisFunction::Product l;
|
||||
std::string tmp = l.serialize().serialize();
|
||||
NeuralNetwork::BasisFunction::BasisFunction *deserialized =NeuralNetwork::BasisFunction::Factory::deserialize(l.serialize()).release();
|
||||
assert(tmp==deserialized->serialize().serialize());
|
||||
delete deserialized;
|
||||
}
|
||||
|
||||
{
|
||||
NeuralNetwork::BasisFunction::Radial l;
|
||||
std::string tmp = l.serialize().serialize();
|
||||
NeuralNetwork::BasisFunction::BasisFunction *deserialized =NeuralNetwork::BasisFunction::Factory::deserialize(l.serialize()).release();
|
||||
assert(tmp==deserialized->serialize().serialize());
|
||||
delete deserialized;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::string serialized;
|
||||
{ // XOR problem
|
||||
NeuralNetwork::FeedForward::Network n(2);
|
||||
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
|
||||
@@ -42,6 +43,31 @@ int main() {
|
||||
std::vector<float> ret =n.computeOutput({0,0});
|
||||
assert(ret[0] < 0.5);
|
||||
}
|
||||
serialized = n.serialize().serialize();
|
||||
}
|
||||
{
|
||||
NeuralNetwork::FeedForward::Network *deserialized=NeuralNetwork::FeedForward::Network::Factory::deserialize(serialized).release();
|
||||
|
||||
{
|
||||
std::vector<float> ret =deserialized->computeOutput({1,1});
|
||||
assert(ret[0] < 0.5);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<float> ret =deserialized->computeOutput({0,1});
|
||||
assert(ret[0] > 0.5);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<float> ret =deserialized->computeOutput({1,0});
|
||||
assert(ret[0] > 0.5);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<float> ret =deserialized->computeOutput({0,0});
|
||||
assert(ret[0] < 0.5);
|
||||
}
|
||||
delete deserialized;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,4 +17,16 @@ int main() {
|
||||
float res= a.computeOutput({1,0.7})[0];
|
||||
assert(res > solutions[i]*0.999 && res < solutions[i]*1.001);
|
||||
}
|
||||
|
||||
std::string str = a.stringify();
|
||||
std::cout << str << std::endl;;
|
||||
|
||||
//deserialize and check it!
|
||||
NeuralNetwork::Recurrent::Network *deserialized = (NeuralNetwork::Recurrent::Network::Factory::deserialize(str).release());
|
||||
for(size_t i=0;i<solutions.size();i++) {
|
||||
float res= a.computeOutput({1,0.7})[0];
|
||||
float resDeserialized= deserialized->computeOutput({1,0.7})[0];
|
||||
assert(fabs(resDeserialized-res) < 0.01);
|
||||
}
|
||||
delete deserialized;
|
||||
}
|
||||
Reference in New Issue
Block a user