Commit 2f1f086c authored by Martin Beseda's avatar Martin Beseda

ENH: Serialization implementation started

parent a3c47005
...@@ -9,3 +9,4 @@ compilers.env ...@@ -9,3 +9,4 @@ compilers.env
src/funit.tmp src/funit.tmp
*.swp *.swp
/Release/ /Release/
.idea/*
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
project(4neuro)
message ("Before enable language")
enable_language(Fortran) #message ("Before enable language")
if (WIN32) ##enable_language(Fortran)
message ("cmake for " ${CMAKE_Fortran_COMPILER}) #if (WIN32)
set (CMAKE_FORTRAN_COMPILER ${CMAKE_Fortran_COMPILER}) # message ("cmake for " ${CMAKE_Fortran_COMPILER})
project(4Neuro) # set (CMAKE_FORTRAN_COMPILER ${CMAKE_Fortran_COMPILER})
else () # project(4Neuro)
project(4Neuro) #else ()
endif () # project(4Neuro)
message ("Start cmakeList") #endif ()
#message ("Start cmakeList")
#-------------------------------# #-------------------------------#
# Default installation location # # Default installation location #
...@@ -32,13 +33,13 @@ endif (NOT CMAKE_BUILD_TYPE) ...@@ -32,13 +33,13 @@ endif (NOT CMAKE_BUILD_TYPE)
#--------------------------------# #--------------------------------#
# Setting C++ compiler flags # # Setting C++ compiler flags #
#--------------------------------# #--------------------------------#
set(CMAKE_CXX_STANDARD 17)
set(standard "-std=c++17") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall" )
#--------------------# #--------------------#
# Automatic settings # # Automatic settings #
#--------------------# #--------------------#
get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME) #get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)
# Processing user variables # Processing user variables
if (WITH_TIME_PROFILING) if (WITH_TIME_PROFILING)
...@@ -52,15 +53,10 @@ if (WIN32) ...@@ -52,15 +53,10 @@ if (WIN32)
endif() endif()
# Write compiler variables to the file - to pass them to test script # Write compiler variables to the file - to pass them to test script
file(WRITE compilers.env "export FC=${CMAKE_Fortran_COMPILER}\n") #file(WRITE compilers.env "export FC=${CMAKE_Fortran_COMPILER}\n")
file(APPEND compilers.env "export CXX=${CMAKE_CXX_COMPILER}\n") file(APPEND compilers.env "export CXX=${CMAKE_CXX_COMPILER}\n")
file(APPEND compilers.env "export CC=${CMAKE_C_COMPILER}\n") file(APPEND compilers.env "export CC=${CMAKE_C_COMPILER}\n")
# Fortran compiler detection
message ("Fortran compiler name: " ${Fortran_COMPILER_NAME})
message ("CMAKE_Fortran_COMPILER full path: " ${CMAKE_Fortran_COMPILER})
message ("Fortran compiler: " ${Fortran_COMPILER_NAME})
message ("Build type:" ${CMAKE_BUILD_TYPE})
...@@ -84,11 +80,8 @@ message ("BUILD_DIR:" ${BUILD_DIR}) ...@@ -84,11 +80,8 @@ message ("BUILD_DIR:" ${BUILD_DIR})
message ("LIB_DIR: " ${LIB_DIR}) message ("LIB_DIR: " ${LIB_DIR})
if (WIN32) if (WIN32)
message ("Windows") message ("Windows")
else () else ()
message ("Not Windows") message ("Not Windows")
endif () endif ()
...@@ -3,4 +3,4 @@ add_library(neuron SHARED Neuron/Neuron.cpp Neuron/Neuron.h Neuron/NeuronBinary. ...@@ -3,4 +3,4 @@ add_library(neuron SHARED Neuron/Neuron.cpp Neuron/Neuron.h Neuron/NeuronBinary.
add_executable(test_cases main.cpp) add_executable(test_cases main.cpp)
target_link_libraries(test_cases neuron) target_link_libraries(test_cases neuron boost_serialization)
\ No newline at end of file \ No newline at end of file
...@@ -112,3 +112,9 @@ std::vector<Connection*>* Neuron::get_connections_in() { ...@@ -112,3 +112,9 @@ std::vector<Connection*>* Neuron::get_connections_in() {
std::vector<Connection*>* Neuron::get_connections_out() { std::vector<Connection*>* Neuron::get_connections_out() {
return this->edges_out; return this->edges_out;
} }
//template<class Archive>
//void Neuron::serialize(Archive & ar, const unsigned int version) {
// ar << this->potential;
// ar << this->state;
//}
\ No newline at end of file
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#ifndef NEURON_H_ #ifndef NEURON_H_
#define NEURON_H_ #define NEURON_H_
#include <boost/serialization/base_object.hpp>
#include <vector> #include <vector>
#include "../NetConnection/Connection.h" #include "../NetConnection/Connection.h"
...@@ -20,6 +20,7 @@ class Connection; ...@@ -20,6 +20,7 @@ class Connection;
* Abstract class representing a general neuron * Abstract class representing a general neuron
*/ */
class Neuron{ class Neuron{
friend class boost::serialization::access;
protected: protected:
/** /**
...@@ -62,6 +63,17 @@ protected: ...@@ -62,6 +63,17 @@ protected:
*/ */
std::vector<Connection*> *edges_out = nullptr; std::vector<Connection*> *edges_out = nullptr;
template<class Archive>
void serialize(Archive & ar, const unsigned int version){
//TODO separate implementation to Neuron.cpp!
ar & this->potential;
ar & this->state;
for(unsigned short int i = 0; i < this->n_activation_function_parameters; i++) {
ar & this->activation_function_parameters[i];
}
};
public: public:
/** /**
......
...@@ -25,3 +25,7 @@ void NeuronBinary::activate( ) { ...@@ -25,3 +25,7 @@ void NeuronBinary::activate( ) {
} }
} }
template<class Archive>
void NeuronBinary::serialize(Archive & ar, const unsigned int version) {
ar & boost::serialization::base_object<Neuron>(*this);
}
...@@ -16,7 +16,10 @@ ...@@ -16,7 +16,10 @@
* Binary neuron class - uses unit-step as the activation function * Binary neuron class - uses unit-step as the activation function
*/ */
class NeuronBinary:public Neuron { class NeuronBinary:public Neuron {
friend class boost::serialization::access;
private: private:
template<class Archive>
void serialize(Archive & ar, const unsigned int version);
public: public:
...@@ -32,8 +35,6 @@ public: ...@@ -32,8 +35,6 @@ public:
*/ */
void activate( ) override; void activate( ) override;
}; };
#endif //INC_4NEURO_NEURONBINARY_H #endif //INC_4NEURO_NEURONBINARY_H
...@@ -2,10 +2,12 @@ ...@@ -2,10 +2,12 @@
// Created by fluffymoo on 11.6.18. // Created by fluffymoo on 11.6.18.
// //
#include <boost/serialization/base_object.hpp>
#include "NeuronLinear.h" #include "NeuronLinear.h"
NeuronLinear::NeuronLinear(double a, double b) { NeuronLinear::NeuronLinear(double a, double b) {
this->n_activation_function_parameters = 2;
this->activation_function_parameters = new double[2]; this->activation_function_parameters = new double[2];
this->activation_function_parameters[0] = a; this->activation_function_parameters[0] = a;
...@@ -44,3 +46,8 @@ double NeuronLinear::activation_function_get_derivative( ) { ...@@ -44,3 +46,8 @@ double NeuronLinear::activation_function_get_derivative( ) {
return b; return b;
} }
//template<class Archive>
//void NeuronLinear::serialize(Archive & ar, const unsigned int version) {
// ar & boost::serialization::base_object<Neuron>(*this);
//}
...@@ -17,6 +17,15 @@ ...@@ -17,6 +17,15 @@
* 'x' being the neuron's potential * 'x' being the neuron's potential
*/ */
class NeuronLinear:public Neuron { class NeuronLinear:public Neuron {
friend class boost::serialization::access;
protected:
template<class Archive>
void serialize(Archive & ar, const unsigned int version){
//TODO separate implementation to NeuronLinear.cpp!
ar & boost::serialization::base_object<Neuron>(*this);
};
public: public:
/** /**
......
#include <cstdio>
/** /**
* DESCRIPTION OF THE FILE * DESCRIPTION OF THE FILE
* *
...@@ -7,12 +5,14 @@ ...@@ -7,12 +5,14 @@
* @date 14.6.18 - * @date 14.6.18 -
*/ */
#include <iostream>
#include <cstdio>
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include "Network/NeuralNetwork.h" #include "Network/NeuralNetwork.h"
#include "Neuron/NeuronLinear.h" #include "Neuron/NeuronLinear.h"
#include "NetConnection/Connection.h" #include "NetConnection/Connection.h"
#include "NetConnection/ConnectionWeightIdentity.h" #include "NetConnection/ConnectionWeightIdentity.h"
//TODO prepsat tak, aby neuronova sit managovala destruktory vsech potrebnych objektu (kvuli serializaci) //TODO prepsat tak, aby neuronova sit managovala destruktory vsech potrebnych objektu (kvuli serializaci)
...@@ -42,7 +42,7 @@ void test1(){ ...@@ -42,7 +42,7 @@ void test1(){
printf("eval: %f, %f\n", a, b); printf("eval: %f, %f\n", a, b);
return (a + 0.0 * b); return (a + 0.0 * b);
}; };
int weight_indices [2]= {0, -1}; int weight_indices [2] = {0, -1};
double weight_values [2] = {1.0, 5.0}; double weight_values [2] = {1.0, 5.0};
net.add_connection_general(idx1, idx2, &weight_function, weight_indices, weight_values, 2); net.add_connection_general(idx1, idx2, &weight_function, weight_indices, weight_values, 2);
/////////////////////////END OF COMPLEX EDGE WEIGHT////////////////////////////// /////////////////////////END OF COMPLEX EDGE WEIGHT//////////////////////////////
...@@ -58,11 +58,46 @@ void test1(){ ...@@ -58,11 +58,46 @@ void test1(){
delete u2; delete u2;
} }
/**
* Test of the binary serialization
*/
void test2() {
NeuronLinear n(2, 3);
std::cout << n.get_potential() << " "
<< n.get_state() << " "
<< n.activation_function_get_parameter(0) << " "
<< n.activation_function_get_parameter(1) << std::endl;
std::ofstream ofs("stored_neuron.4n");
{
boost::archive::text_oarchive oa(ofs);
oa << n;
ofs.close();
}
NeuronLinear n2;
{
std::ifstream ifs("stored_neuron.4n");
boost::archive::text_iarchive ia(ifs);
ia >> n2;
ifs.close();
}
std::cout << n2.get_potential() << " "
<< n2.get_state() << " "
<< n2.activation_function_get_parameter(0) << " "
<< n2.activation_function_get_parameter(1) << std::endl;
}
int main(int argc, char** argv){ int main(int argc, char** argv){
test1(); test1();
printf("hello biatch\n"); printf("hello biatch\n");
test2();
return 0; return 0;
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment