<commit_before>/* 
 * File:   BackpropeagationLearner.cpp
 * Author: janvojt
 * 
 * Created on July 13, 2014, 12:10 AM
 */

#include "BackpropagationLearner.h"

#include <cstring>
#include <string>
#include <stdexcept>
#include <limits>

#include "../log/LoggerFactory.h"
#include "log4cpp/Category.hh"

BackpropagationLearner::BackpropagationLearner(Network *network) {
    this->network = network;
    learningRate = 1;
    epochCounter = 0;
    epochLimit = 1000000;
    targetMse = .0001;
    errorTotal = std::numeric_limits<double>::infinity();
    useBias = network->getConfiguration()->getBias();
    noLayers = network->getConfiguration()->getLayers();
}

BackpropagationLearner::BackpropagationLearner(const BackpropagationLearner &orig) {
}

BackpropagationLearner::~BackpropagationLearner() {
    if (errorComputer != NULL) delete errorComputer;
}

void BackpropagationLearner::train(LabeledDataset *dataset) {
    double mse = 0;
    LOG()->info("Started training with limits of %d epochs and target MSE of %f.", epochLimit, targetMse);
    do {
        epochCounter++;
        LOG()->debug("Starting epoch %d.", epochCounter);
        dataset->reset();
        int datasetSize = 0;
        mse = 0;
        while (dataset->hasNext()) {
            datasetSize++;
            double *pattern = dataset->next();
            double *expOutput = pattern + dataset->getInputDimension();
        LOG()->debug("Starting forward phase for dataset %d in epoch %d.", datasetSize, epochCounter);
            doForwardPhase(pattern);
        LOG()->debug("Starting backward phase for dataset %d in epoch %d.", datasetSize, epochCounter);
            doBackwardPhase(expOutput);
            mse += errorComputer->compute(network, expOutput);
        }
        mse = mse / datasetSize;
        LOG()->debug("Finished epoch %d with MSE: %f.", epochCounter, mse);
    } while (mse > targetMse && epochCounter < epochLimit);
    
    if (mse <= targetMse) {
        LOG()->info("Training successful after %d epochs with MSE of %f.", epochCounter, mse);
    } else {
        LOG()->info("Training interrupted after %d epochs with MSE of %f.", epochCounter, mse);
    }
}

void BackpropagationLearner::doForwardPhase(double *input) {
    network->setInput(input);
    network->run();
}

void BackpropagationLearner::doBackwardPhase(double *expectedOutput) {
    computeOutputGradients(expectedOutput);
    computeWeightDifferentials();
    adjustWeights();
    if (network->getConfiguration()->getBias()) {
        adjustBias();
    }<commit_msg>Changed logging level for MSE progress to INFO.<commit_after>/* 
 * File:   BackpropeagationLearner.cpp
 * Author: janvojt
 * 
 * Created on July 13, 2014, 12:10 AM
 */

#include "BackpropagationLearner.h"

#include <cstring>
#include <string>
#include <stdexcept>
#include <limits>

#include "../log/LoggerFactory.h"
#include "log4cpp/Category.hh"

BackpropagationLearner::BackpropagationLearner(Network *network) {
    this->network = network;
    learningRate = 1;
    epochCounter = 0;
    epochLimit = 1000000;
    targetMse = .0001;
    errorTotal = std::numeric_limits<double>::infinity();
    useBias = network->getConfiguration()->getBias();
    noLayers = network->getConfiguration()->getLayers();
}

BackpropagationLearner::BackpropagationLearner(const BackpropagationLearner &orig) {
}

BackpropagationLearner::~BackpropagationLearner() {
    if (errorComputer != NULL) delete errorComputer;
}

void BackpropagationLearner::train(LabeledDataset *dataset) {
    double mse = 0;
    LOG()->info("Started training with limits of %d epochs and target MSE of %f.", epochLimit, targetMse);
    do {
        epochCounter++;
        LOG()->debug("Starting epoch %d.", epochCounter);
        dataset->reset();
        int datasetSize = 0;
        mse = 0;
        while (dataset->hasNext()) {
            datasetSize++;
            double *pattern = dataset->next();
            double *expOutput = pattern + dataset->getInputDimension();
        LOG()->debug("Starting forward phase for dataset %d in epoch %d.", datasetSize, epochCounter);
            doForwardPhase(pattern);
        LOG()->debug("Starting backward phase for dataset %d in epoch %d.", datasetSize, epochCounter);
            doBackwardPhase(expOutput);
            mse += errorComputer->compute(network, expOutput);
        }
        mse = mse / datasetSize;
        LOG()->info("Finished epoch %d with MSE: %f.", epochCounter, mse);
    } while (mse > targetMse && epochCounter < epochLimit);
    
    if (mse <= targetMse) {
        LOG()->info("Training successful after %d epochs with MSE of %f.", epochCounter, mse);
    } else {
        LOG()->info("Training interrupted after %d epochs with MSE of %f.", epochCounter, mse);
    }
}

void BackpropagationLearner::doForwardPhase(double *input) {
    network->setInput(input);
    network->run();
}

void BackpropagationLearner::doBackwardPhase(double *expectedOutput) {
    computeOutputGradients(expectedOutput);
    computeWeightDifferentials();
    adjustWeights();
    if (network->getConfiguration()->getBias()) {
        adjustBias();
    }
{'fraction_non_alphanumeric': 0.13313782991202347, 'fraction_numerical': 0.010557184750733138, 'mean_word_length': 3.491659350307287, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5202076', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1467, 'n_words': 363}