Skip to content
Snippets Groups Projects
main.cpp 2.91 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * DESCRIPTION OF THE FILE
     *
     * @author Michal Kravčenko
     * @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)
    
    /**
     * Test of simple neural network
     * Network should evaluate the function f(x) = x + 1
     */
    void test1(){
        std::vector<double> in(1);
        std::vector<double> out(1);
    
    
        NeuralNetwork net;
        NeuronLinear* u1 = new NeuronLinear(1.0, 1.0); //f(x) = x + 1.0
        NeuronLinear* u2 = new NeuronLinear(0.0, 1.0); //f(x) = x
        int idx1 = net.add_neuron(u1);
        int idx2 = net.add_neuron(u2);
    ////////////////////// SIMPLE EDGE WEIGHT ////////////////////////////////////////
    //    net.add_connection_simple(idx1, idx2, -1, 1.0);
    ////////////////////// END SIMPLE EDGE WEIGHT ////////////////////////////////////////
    
    /////////////////////////BEGIN OF COMPLEX EDGE WEIGHT//////////////////////////////
    
       std::function<double(double *, int*, int)> weight_function = [](double * weight_array, int * index_array, int n_params){
    
            double a = weight_array[index_array[0]];
            double b = weight_array[index_array[1]];
    //        printf("eval: %f, %f\n",  a, b);
    
        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//////////////////////////////
    
        for(int i = 0; i < 20; ++i){
            in[0] = 0.05 * i;
            net.eval_single(in, out);
    
            printf("x = %3.2f, f(x) = %3.2f, expected output = %3.2f\n", in[0], out[0], in[0] + 1.0);
        }
    
    
    
    /**
     * 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");