diff --git a/set_env_n_cores.bat b/set_env_n_cores.bat new file mode 100644 index 0000000000000000000000000000000000000000..214359c2cdcb4f42a2b80420fe0612513ebc8a1e --- /dev/null +++ b/set_env_n_cores.bat @@ -0,0 +1 @@ +set N_CORES=3 \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index de3c43c6dd97a8548060b180aa84299950af33bd..d9d2f75caef032c147e9fb4a2639d9d0a09fe4c2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -83,10 +83,14 @@ IF("${BUILD_LIB}" STREQUAL "yes") ${LIB_TYPE} Neuron/Neuron.cpp + Neuron/NeuronBiased.cpp Neuron/NeuronBinary.cpp Neuron/NeuronConstant.cpp Neuron/NeuronLinear.cpp Neuron/NeuronLogistic.cpp + Neuron/NeuronRectifier.cpp + Neuron/NeuronFilter.cpp + Neuron/NeuronBinaryBiased.cpp Network/NeuralNetwork.cpp Network/NeuralNetworkSum.cpp NetConnection/ConnectionFunctionGeneral.cpp diff --git a/src/Neuron/NeuronBiased.h b/src/Neuron/NeuronBiased.h index 6525f71b148952e463ee3b952a53a3a7a34873e9..ef69a7b06ef884f4ba721cec61991701842edbf5 100644 --- a/src/Neuron/NeuronBiased.h +++ b/src/Neuron/NeuronBiased.h @@ -13,7 +13,7 @@ namespace lib4neuro { class NeuronBiased : public NeuronDifferentiable { - private: + protected: double bias; diff --git a/src/Neuron/NeuronBinaryBiased.cpp b/src/Neuron/NeuronBinaryBiased.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b299c4f1f5e0cb8ef4370d17e6f9ea4622c9851c --- /dev/null +++ b/src/Neuron/NeuronBinaryBiased.cpp @@ -0,0 +1,44 @@ + +#include <boost/serialization/export.hpp> + +#include "NeuronSerialization.h" +#include "NeuronBinaryBiasedSerialization.h" +#include "NeuronConstant.h" + +BOOST_CLASS_EXPORT_IMPLEMENT(lib4neuro::NeuronBinaryBiased) + +namespace lib4neuro { + NeuronBinaryBiased::NeuronBinaryBiased(double b) { + this->bias = b; + } + + double NeuronBinaryBiased::activate(double x, + double b) { + + if (x >= this->bias) { + this->activation_val = 1.0; + } else { + this->activation_val = 0.0; + } + + return this->activation_val; + } + + double NeuronBinaryBiased::activation_function_eval_derivative_bias(double x, + double b) { + // f'(0) = 0 for the purposes of training + return 0.0; + } + + double NeuronBinaryBiased::activation_function_eval_derivative(double x, + double b) { + // f'(0) = 0 for the purposes of training + return 0.0; + } + + Neuron* NeuronBinaryBiased::get_derivative() { + NeuronConstant* output = new NeuronConstant(0.0); + return output; + } + +} \ No newline at end of file diff --git a/src/Neuron/NeuronBinaryBiased.h b/src/Neuron/NeuronBinaryBiased.h new file mode 100644 index 0000000000000000000000000000000000000000..7926523443208db001602d891f4ec41f5dd23664 --- /dev/null +++ b/src/Neuron/NeuronBinaryBiased.h @@ -0,0 +1,56 @@ +/** + * DESCRIPTION OF THE CLASS + * + * @author Martin Beseda + * @author Martin Mrovec + * @author Michal KravÄŤenko + * @date 2017 - 2019 + */ + +#ifndef INC_4NEURO_NEURONBINARYBIASED_H +#define INC_4NEURO_NEURONBINARYBIASED_H + +#include "NeuronBiased.h" + +namespace lib4neuro { + +/** + * BinaryBiased neuron class - uses unit-step as the activation function + */ + class NeuronBinaryBiased : public NeuronBiased { + + public: + + /** + * Struct used to access private properties from + * the serialization function + */ + struct access; + + /** + * Default constructor for the BinaryBiased Neuron + * @param[in] threshold Denotes when the neuron is activated + * When neuron potential exceeds 'threshold' value it becomes excited + */ + LIB4NEURO_API explicit NeuronBinaryBiased(double b = 0.0); + + + /** + * Performs the activation function and stores the result into the 'state' property + */ + LIB4NEURO_API double activate(double x, + double b) override; + + LIB4NEURO_API double activation_function_eval_derivative_bias(double x, + double b) override; + + LIB4NEURO_API double activation_function_eval_derivative(double x, + double b) override; + + LIB4NEURO_API Neuron* get_derivative() override; + + }; + +} + +#endif //INC_4NEURO_NEURONBINARYBIASED_H diff --git a/src/Neuron/NeuronBinaryBiasedSerialization.h b/src/Neuron/NeuronBinaryBiasedSerialization.h new file mode 100644 index 0000000000000000000000000000000000000000..0af0d5823c2e0a2922307571eabddd6894c33414 --- /dev/null +++ b/src/Neuron/NeuronBinaryBiasedSerialization.h @@ -0,0 +1,49 @@ + +#ifndef LIB4NEURO_NEURON_BINARYBIASED_SERIALIZATION_H +#define LIB4NEURO_NEURON_BINARYBIASED_SERIALIZATION_H + +#include <boost/serialization/base_object.hpp> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> +#include <boost/serialization/export.hpp> + +#include "NeuronSerialization.h" +#include "NeuronBinaryBiased.h" + +BOOST_CLASS_EXPORT_KEY(lib4neuro::NeuronBinaryBiased); + +namespace lib4neuro { + struct NeuronBinaryBiased::access { + template<class Archive> + static void serialize(Archive& ar, + lib4neuro::NeuronBinaryBiased& n, + const unsigned int version) { + ar & boost::serialization::base_object<Neuron>(n); + } + }; +} + +namespace boost { + namespace serialization { + + /** + * Serialization function + * @tparam Archive Boost library template + * @param ar Boost parameter - filled automatically during serialization! + * @param n NeuronBinaryBiased instance + * @param version Boost parameter - filled automatically during serialization! + */ + template<class Archive> + void serialize(Archive& ar, + lib4neuro::NeuronBinaryBiased& n, + const unsigned int version) { + lib4neuro::NeuronBinaryBiased::access::serialize(ar, + n, + version); + } + + } // namespace serialization +} // namespace boost + + +#endif //LIB4NEURO_NEURON_BINARYBIASED_SERIALIZATION_H diff --git a/src/Neuron/NeuronBinarySerialization.h b/src/Neuron/NeuronBinarySerialization.h index 3506f03903ecf774129a66aa15b598032bec8341..b16a3e4658638a4c8e8d1d02de76731d88ce613d 100644 --- a/src/Neuron/NeuronBinarySerialization.h +++ b/src/Neuron/NeuronBinarySerialization.h @@ -11,15 +11,17 @@ #include "NeuronBinary.h" BOOST_CLASS_EXPORT_KEY(lib4neuro::NeuronBinary); - -struct lib4neuro::NeuronBinary::access { - template<class Archive> - static void serialize(Archive& ar, - lib4neuro::NeuronBinary& n, - const unsigned int version) { - ar & boost::serialization::base_object<lib4neuro::Neuron>(n); - } -}; + +namespace lib4neuro { + struct NeuronBinary::access { + template<class Archive> + static void serialize(Archive& ar, + lib4neuro::NeuronBinary& n, + const unsigned int version) { + ar & boost::serialization::base_object<Neuron>(n); + } + }; +} namespace boost { namespace serialization { diff --git a/src/Neuron/NeuronFilter.cpp b/src/Neuron/NeuronFilter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b70edcc32a0134c089fe8d62d8da2def3cbc3b3f --- /dev/null +++ b/src/Neuron/NeuronFilter.cpp @@ -0,0 +1,41 @@ + +#include <boost/serialization/export.hpp> + +#include "NeuronRectifier.h" +#include "NeuronBinary.h" +#include "Neuron.h" +#include "NeuronSerialization.h" +#include "NeuronFilterSerialization.h" + +BOOST_CLASS_EXPORT_IMPLEMENT(lib4neuro::NeuronFilter); + +namespace lib4neuro { + NeuronFilter::NeuronFilter(double b) { + this->bias = b; + } + + double NeuronFilter::activate(double x, + double b) { + this->activation_val = (0 < x + this->bias) ? x : 0.0; + return this->activation_val; + } + + double NeuronFilter::activation_function_eval_derivative_bias(double x, + double b) { + // f'(0) = 0 for the purposes of training + return 0.0; + } + + double NeuronFilter::activation_function_eval_derivative(double x, + double b) { + // f'(0) = 0 for the purposes of training + return ((x + this->bias) > 0) ? 1.0 : 0.0; + } + + Neuron* NeuronFilter::get_derivative() { + NeuronBinary* output = new NeuronBinary(); + return output; + } + + +} \ No newline at end of file diff --git a/src/Neuron/NeuronFilter.h b/src/Neuron/NeuronFilter.h new file mode 100644 index 0000000000000000000000000000000000000000..a58c72fd32e0477e7999990ddb242040618ab53b --- /dev/null +++ b/src/Neuron/NeuronFilter.h @@ -0,0 +1,33 @@ + +#include "Neuron.h" +#include "NeuronBiased.h" + +#ifndef LIB4NEURO_NEURONFILTER_H +#define LIB4NEURO_NEURONFILTER_H + +namespace lib4neuro { + + /** + * Filter linear unit neuron class - uses activation function in the form f(x) = max(0, x), + * 'x' being the neuron's potential + */ + class NeuronFilter : public NeuronBiased { + public: + struct access; + + LIB4NEURO_API explicit NeuronFilter(double b = 0.0); + + LIB4NEURO_API double activate(double x, + double b) override; + + LIB4NEURO_API double activation_function_eval_derivative_bias(double x, + double b) override; + + LIB4NEURO_API double activation_function_eval_derivative(double x, + double b) override; + + LIB4NEURO_API Neuron* get_derivative() override; + }; +} + +#endif //LIB4NEURO_NEURONFilter_H diff --git a/src/Neuron/NeuronFilterSerialization.h b/src/Neuron/NeuronFilterSerialization.h new file mode 100644 index 0000000000000000000000000000000000000000..073fbfe272b897c20a0cce3a7ad3bbc597b5787f --- /dev/null +++ b/src/Neuron/NeuronFilterSerialization.h @@ -0,0 +1,48 @@ + +#ifndef LIB4NEURO_NEURONFILTERSERIALIZATION_H +#define LIB4NEURO_NEURONFILTERSERIALIZATION_H + +#include <boost/serialization/base_object.hpp> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> +#include <boost/serialization/export.hpp> + +#include "NeuronFilter.h" +#include "NeuronSerialization.h" + +BOOST_CLASS_EXPORT_KEY(lib4neuro::NeuronFilter); + +namespace lib4neuro { + struct NeuronFilter::access { + template<class Archive> + static void serialize(Archive& ar, + NeuronFilter& n, + const unsigned int version) { + ar & boost::serialization::base_object<Neuron>(n); + } + }; +} + +namespace boost { + namespace serialization { + + /** + * Serialization function + * @tparam Archive Boost library template + * @param ar Boost parameter - filled automatically during serialization! + * @param n NeuronFilter instance + * @param version Boost parameter - filled automatically during serialization! + */ + template<class Archive> + void serialize(Archive& ar, + lib4neuro::NeuronFilter& n, + const unsigned int version) { + lib4neuro::NeuronFilter::access::serialize(ar, + n, + version); + } + + } // namespace serialization +} // namespace boost + +#endif //LIB4NEURO_NEURONFILTERSERIALIZATION_H diff --git a/src/Neuron/NeuronRectifier.cpp b/src/Neuron/NeuronRectifier.cpp index ef64823e5402f253249249704001c0ebc4bc3afd..c6cce775d2379e54da644f4c283d34c6176de51f 100644 --- a/src/Neuron/NeuronRectifier.cpp +++ b/src/Neuron/NeuronRectifier.cpp @@ -2,6 +2,8 @@ #include <boost/serialization/export.hpp> #include "NeuronRectifier.h" +#include "NeuronBinary.h" +#include "NeuronSerialization.h" #include "NeuronRectifierSerialization.h" BOOST_CLASS_EXPORT_IMPLEMENT(lib4neuro::NeuronRectifier); @@ -11,24 +13,25 @@ namespace lib4neuro { double NeuronRectifier::activate(double x, double b) { - this->activation_val = std::max(0, - x + b); + this->activation_val = (0 < x + b) ? x + b : 0.0; + return this->activation_val; } - double NeuronRectifier::activation_function_eval_derivative(double x, - double b) { + double NeuronRectifier::activation_function_eval_derivative_bias(double x, + double b) { // f'(0) = 0 for the purposes of training - return ((x + b) > 0) ? 1 : 0; + return ((x + b) > 0) ? 1.0 : 0.0; } double NeuronRectifier::activation_function_eval_derivative(double x, double b) { // f'(0) = 0 for the purposes of training - return ((x + b) > 0) ? 1 : 0; + return ((x + b) > 0) ? 1.0 : 0.0; } Neuron* NeuronRectifier::get_derivative() { - NeuronConstant* output = new NeuronConstant(); + NeuronBinary* output = new NeuronBinary(); + return output; } diff --git a/src/Neuron/NeuronRectifier.h b/src/Neuron/NeuronRectifier.h index f7014de2eb0f046e3a12b25e3dfbde2eb3daef06..8f8bf2d57a21e79d3674a35f5c91326267a819ef 100644 --- a/src/Neuron/NeuronRectifier.h +++ b/src/Neuron/NeuronRectifier.h @@ -1,8 +1,8 @@ #include "Neuron.h" -#ifndef LIB4NEURO_NEURONRECTIFIER_H -#define LIB4NEURO_NEURONRECTIFIER_H +#ifndef INC_4NEURO_NEURONRECTIFIER_H +#define INC_4NEURO_NEURONRECTIFIER_H namespace lib4neuro { @@ -10,7 +10,7 @@ namespace lib4neuro { * Rectifier linear unit neuron class - uses activation function in the form f(x) = max(0, x), * 'x' being the neuron's potential */ - class NeuronRectifier : NeuronDifferentiable { + class NeuronRectifier : public NeuronDifferentiable { public: struct access; @@ -29,4 +29,4 @@ namespace lib4neuro { }; } -#endif //LIB4NEURO_NEURONRECTIFIER_H +#endif //INC_4NEURO_NEURONRECTIFIER_H diff --git a/src/Neuron/NeuronRectifierSerialization.h b/src/Neuron/NeuronRectifierSerialization.h index 929dc7af78804d86ade12137a3b196c39dadaf9a..3f6f2c396018f002c2626563647f700753f5891c 100644 --- a/src/Neuron/NeuronRectifierSerialization.h +++ b/src/Neuron/NeuronRectifierSerialization.h @@ -16,7 +16,7 @@ namespace lib4neuro { struct NeuronRectifier::access { template<class Archive> static void serialize(Archive& ar, - Neuronrectifier& n, + NeuronRectifier& n, const unsigned int version) { ar & boost::serialization::base_object<Neuron>(n); }