NeuralNetworkSum.cpp 2.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/**
 * DESCRIPTION OF THE FILE
 *
 * @author Michal Kravčenko
 * @date 18.7.18 -
 */

#include "NeuralNetworkSum.h"

NeuralNetworkSum::NeuralNetworkSum(){
    this->summand = nullptr;
    this->summand_coefficient = nullptr;
}

NeuralNetworkSum::~NeuralNetworkSum() {
    if( this->summand ){
        delete this->summand;
    }
    if( this->summand_coefficient ){
        delete this->summand_coefficient;
    }
}

void NeuralNetworkSum::add_network(NeuralNetwork *net, double alpha) {
    if(!this->summand){
        this->summand = new std::vector<NeuralNetwork*>(0);
    }
    this->summand->push_back( net );

    if(!this->summand_coefficient){
        this->summand_coefficient = new std::vector<double>(0);
    }
    this->summand_coefficient->push_back( alpha );
}

Michal Kravcenko's avatar
Michal Kravcenko committed
36
void NeuralNetworkSum::eval_single(std::vector<double> &input, std::vector<double> &output, std::vector<double> *custom_weights_and_biases) {
37 38 39
    std::vector<double> mem_output(output.size());
    std::fill(output.begin(), output.end(), 0.0);

Michal Kravcenko's avatar
Michal Kravcenko committed
40 41
    for(size_t ni = 0; ni < this->summand->size(); ++ni){
        this->summand->at(ni)->eval_single(input, mem_output, custom_weights_and_biases);
42
        double alpha = this->summand_coefficient->at(ni);
Michal Kravcenko's avatar
Michal Kravcenko committed
43
        for(size_t j = 0; j < output.size(); ++j){
44 45 46 47 48 49 50
            output[j] += mem_output[j] * alpha;
        }
    }

}

size_t NeuralNetworkSum::get_n_weights(){
Michal Kravcenko's avatar
Michal Kravcenko committed
51
    //TODO insufficient solution, assumes the networks share weights
52 53 54 55
    if(this->summand){
        return this->summand->at(0)->get_n_weights();
    }

Michal Kravcenko's avatar
Michal Kravcenko committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    return 0;
}

size_t NeuralNetworkSum::get_n_biases(){
    //TODO insufficient solution, assumes the networks share weights
    if(this->summand){
        return this->summand->at(0)->get_n_biases();
    }

    return 0;
}

size_t NeuralNetworkSum::get_n_inputs() {
    //TODO insufficient solution, assumes the networks share weights
    if(this->summand){
        return this->summand->at(0)->get_n_inputs();
    }

    return 0;
}

size_t NeuralNetworkSum::get_n_neurons() {
    //TODO insufficient solution, assumes the networks share weights
    if(this->summand){
        return this->summand->at(0)->get_n_neurons();
    }

    return 0;
}

size_t NeuralNetworkSum::get_n_outputs() {
    //TODO insufficient solution, assumes the networks share weights
    if(this->summand){
        return this->summand->at(0)->get_n_outputs();
    }

92 93
    return 0;
}