Skip to content
Snippets Groups Projects
ErrorFunctions.cpp 2.97 KiB
Newer Older
  • Learn to ignore specific revisions
  • //
    // Created by martin on 7/15/18.
    //
    
    
    #include <vector>
    
    
    #include "ErrorFunctions.h"
    
    David Vojtek's avatar
    David Vojtek committed
    namespace lib4neuro {
    
    David Vojtek's avatar
    David Vojtek committed
        size_t ErrorFunction::get_dimension() {
            return this->dimension;
        }
    
    David Vojtek's avatar
    David Vojtek committed
        MSE::MSE(NeuralNetwork *net, DataSet *ds) {
            this->net = net;
            this->ds = ds;
            this->dimension = net->get_n_weights() + net->get_n_biases();
        }
    
    David Vojtek's avatar
    David Vojtek committed
        double MSE::eval(std::vector<double> *weights) {
            unsigned int dim_out = this->ds->get_output_dim();
    
    David Vojtek's avatar
    David Vojtek committed
            size_t n_elements = this->ds->get_n_elements();
            double error = 0.0, val;
    
    David Vojtek's avatar
    David Vojtek committed
            std::vector<std::pair<std::vector<double>, std::vector<double>>> *data = this->ds->get_data();
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
    //    //TODO instead use something smarter
    //    this->net->copy_weights(weights);
    
    David Vojtek's avatar
    David Vojtek committed
            std::vector<double> output(dim_out);
    
    David Vojtek's avatar
    David Vojtek committed
            for (unsigned int i = 0; i < n_elements; ++i) {  // Iterate through every element in the test set
    
    David Vojtek's avatar
    David Vojtek committed
                this->net->eval_single(data->at(i).first, output,
                                       weights);  // Compute the net output and store it into 'output' variable
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
    
    //        printf("errors: ");
    
    David Vojtek's avatar
    David Vojtek committed
                for (unsigned int j = 0; j < dim_out; ++j) {  // Compute difference for every element of the output vector
    
    David Vojtek's avatar
    David Vojtek committed
                    val = output[j] - data->at(i).second[j];
                    error += val * val;
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
    
    //            printf("%f, ", val * val);
    
    David Vojtek's avatar
    David Vojtek committed
                }
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
    //        printf("\n");
    
    David Vojtek's avatar
    David Vojtek committed
            }
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
    //    printf("n_elements: %d\n", n_elements);
    
    David Vojtek's avatar
    David Vojtek committed
            return error / n_elements;
    
    David Vojtek's avatar
    David Vojtek committed
        ErrorSum::ErrorSum() {
            this->summand = nullptr;
            this->summand_coefficient = nullptr;
            this->dimension = 0;
        }
    
    David Vojtek's avatar
    David Vojtek committed
        ErrorSum::~ErrorSum() {
            if (this->summand) {
                delete this->summand;
            }
            if (this->summand_coefficient) {
                delete this->summand_coefficient;
            }
    
    David Vojtek's avatar
    David Vojtek committed
        double ErrorSum::eval(std::vector<double> *weights) {
            double output = 0.0;
    
    David Vojtek's avatar
    David Vojtek committed
            for (unsigned int i = 0; i < this->summand->size(); ++i) {
                output += this->summand->at(i)->eval(weights) * this->summand_coefficient->at(i);
            }
    
    David Vojtek's avatar
    David Vojtek committed
            return output;
    
    David Vojtek's avatar
    David Vojtek committed
        void ErrorSum::add_error_function(ErrorFunction *F, double alpha) {
            if (!this->summand) {
                this->summand = new std::vector<ErrorFunction *>(0);
            }
            this->summand->push_back(F);
    
            if (!this->summand_coefficient) {
                this->summand_coefficient = new std::vector<double>(0);
            }
            this->summand_coefficient->push_back(alpha);
    
            if (F->get_dimension() > this->dimension) {
                this->dimension = F->get_dimension();
            }
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        }
    
    David Vojtek's avatar
    David Vojtek committed
        size_t ErrorSum::get_dimension() {
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
    //    if(!this->dimension) {
    //        size_t max = 0;
    //        for(auto e : *this->summand) {
    //            if(e->get_dimension() > max) {
    //                max = e->get_dimension();
    //            }
    //        };
    //
    //        this->dimension = max;
    //    }
    
    David Vojtek's avatar
    David Vojtek committed
            return this->dimension;
        }