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

ENH: Serialization implementation started

parent a3c47005
......@@ -9,3 +9,4 @@ compilers.env
src/funit.tmp
*.swp
/Release/
.idea/*
cmake_minimum_required(VERSION 3.0)
message ("Before enable language")
enable_language(Fortran)
if (WIN32)
message ("cmake for " ${CMAKE_Fortran_COMPILER})
set (CMAKE_FORTRAN_COMPILER ${CMAKE_Fortran_COMPILER})
project(4Neuro)
else ()
project(4Neuro)
endif ()
message ("Start cmakeList")
project(4neuro)
#message ("Before enable language")
##enable_language(Fortran)
#if (WIN32)
# message ("cmake for " ${CMAKE_Fortran_COMPILER})
# set (CMAKE_FORTRAN_COMPILER ${CMAKE_Fortran_COMPILER})
# project(4Neuro)
#else ()
# project(4Neuro)
#endif ()
#message ("Start cmakeList")
#-------------------------------#
# Default installation location #
......@@ -32,13 +33,13 @@ endif (NOT CMAKE_BUILD_TYPE)
#--------------------------------#
# Setting C++ compiler flags #
#--------------------------------#
set(standard "-std=c++17")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall" )
#--------------------#
# 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
if (WITH_TIME_PROFILING)
......@@ -52,15 +53,10 @@ if (WIN32)
endif()
# 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 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})
message ("LIB_DIR: " ${LIB_DIR})
if (WIN32)
message ("Windows")
else ()
message ("Not Windows")
endif ()
......@@ -3,4 +3,4 @@ add_library(neuron SHARED Neuron/Neuron.cpp Neuron/Neuron.h Neuron/NeuronBinary.
add_executable(test_cases main.cpp)
target_link_libraries(test_cases neuron)
\ No newline at end of file
target_link_libraries(test_cases neuron boost_serialization)
\ No newline at end of file
......@@ -112,3 +112,9 @@ std::vector<Connection*>* Neuron::get_connections_in() {
std::vector<Connection*>* Neuron::get_connections_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 @@
#ifndef NEURON_H_
#define NEURON_H_
#include <boost/serialization/base_object.hpp>
#include <vector>
#include "../NetConnection/Connection.h"
......@@ -20,6 +20,7 @@ class Connection;
* Abstract class representing a general neuron
*/
class Neuron{
friend class boost::serialization::access;
protected:
/**
......@@ -62,6 +63,17 @@ protected:
*/
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:
/**
......
......@@ -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 @@
* Binary neuron class - uses unit-step as the activation function
*/
class NeuronBinary:public Neuron {
friend class boost::serialization::access;
private:
template<class Archive>
void serialize(Archive & ar, const unsigned int version);
public:
......@@ -32,8 +35,6 @@ public:
*/
void activate( ) override;
};
#endif //INC_4NEURO_NEURONBINARY_H
......@@ -2,10 +2,12 @@
// Created by fluffymoo on 11.6.18.
//
#include <boost/serialization/base_object.hpp>
#include "NeuronLinear.h"
NeuronLinear::NeuronLinear(double a, double b) {
this->n_activation_function_parameters = 2;
this->activation_function_parameters = new double[2];
this->activation_function_parameters[0] = a;
......@@ -44,3 +46,8 @@ double NeuronLinear::activation_function_get_derivative( ) {
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 @@
* 'x' being the neuron's potential
*/
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:
/**
......
#include <cstdio>
/**
* DESCRIPTION OF THE FILE
*
......@@ -7,12 +5,14 @@
* @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 "Neuron/NeuronLinear.h"
#include "NetConnection/Connection.h"
#include "NetConnection/ConnectionWeightIdentity.h"
//TODO prepsat tak, aby neuronova sit managovala destruktory vsech potrebnych objektu (kvuli serializaci)
......@@ -42,7 +42,7 @@ void test1(){
printf("eval: %f, %f\n", a, 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};
net.add_connection_general(idx1, idx2, &weight_function, weight_indices, weight_values, 2);
/////////////////////////END OF COMPLEX EDGE WEIGHT//////////////////////////////
......@@ -58,11 +58,46 @@ void test1(){
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){
test1();
printf("hello biatch\n");
test2();
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