From bd9276fcebd268375bf06a53fb278bb1ab306d30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Zbyszy=C5=84ski?= <m.zbyszynski@gold.ac.uk> Date: Thu, 18 May 2017 14:42:52 +0200 Subject: [PATCH] adding machine learning class --- src/machineLearning/machineLearning.cpp | 42 ++++++++++++++++ src/machineLearning/machineLearning.h | 64 +++++++++++++++++++++++++ src/rapidmix.h | 6 +++ 3 files changed, 112 insertions(+) create mode 100644 src/machineLearning/machineLearning.cpp create mode 100644 src/machineLearning/machineLearning.h diff --git a/src/machineLearning/machineLearning.cpp b/src/machineLearning/machineLearning.cpp new file mode 100644 index 0000000..3b4951d --- /dev/null +++ b/src/machineLearning/machineLearning.cpp @@ -0,0 +1,42 @@ +/* + * machineLearning.h + * Created by Michael Zbyszynski on 10 Jan 2016 + * Copyright © 2017 Goldsmiths. All rights reserved. + */ + +#include "machineLearning.h" + +RAPIDMIX_BEGIN_NAMESPACE + +void trainingData2rapidLib (const trainingData &newTrainingData, std::vector<trainingExample> &trainingSet) { + for (int h = 0; h < newTrainingData.trainingSet.size(); ++h) { //Go through every phrase + for (int i = 0; i < newTrainingData.trainingSet[h].elements.size(); ++i) { //...and every element + trainingExample tempExample; + tempExample.input = newTrainingData.trainingSet[h].elements[i].input; + if (newTrainingData.trainingSet[h].elements[i].output.size() > 0) { + tempExample.output = newTrainingData.trainingSet[h].elements[i].output; + } else { + std::unordered_map<std::string, int>::const_iterator mappedLabel = newTrainingData.labels.find(newTrainingData.trainingSet[h].label); + tempExample.output.push_back(double(mappedLabel->second)); + } + trainingSet.push_back(tempExample); + } + } +}; + +template<> +bool machineLearning<classification>::train(const trainingData &newTrainingData) { + std::vector<trainingExample> trainingSet; + trainingData2rapidLib(newTrainingData, trainingSet); + return classification::train(trainingSet); +} + +template<> +bool machineLearning<regression>::train(const trainingData &newTrainingData) { + std::vector<trainingExample> trainingSet; + trainingData2rapidLib(newTrainingData, trainingSet); + return regression::train(trainingSet); +} + + +RAPIDMIX_END_NAMESPACE diff --git a/src/machineLearning/machineLearning.h b/src/machineLearning/machineLearning.h new file mode 100644 index 0000000..6f810f7 --- /dev/null +++ b/src/machineLearning/machineLearning.h @@ -0,0 +1,64 @@ +/* + * machineLearning.h + * Created by Michael Zbyszynski on 10 Jan 2016 + * Copyright © 2017 Goldsmiths. All rights reserved. + */ + +#ifndef machineLearning_h +#define machineLearning_h + +#include "rapidMix.h" +#include "trainingData.h" + +////////// Include all of the machine learning algorithms here +#include "classification.h" +#include "regression.h" +#include "rapidXmmTools.h" +#include "gvf.h" + + + +RAPIDMIX_BEGIN_NAMESPACE + +//* Host class for machine learning algorithms */ +template <typename MachineLearningModule> +class machineLearning : public MachineLearningModule { +public: + + //* Constructors */ + machineLearning() : MachineLearningModule() {}; + + template<class T> + machineLearning(T type) : MachineLearningModule(type) {}; + + //* this function becomes specialized in the implementation */ + bool train(const trainingData &newTrainingData); + + // Could overload this, or specialize, or both + std::vector<double> run(const std::vector<double> &inputVector) { + return MachineLearningModule::process(inputVector); + } + + bool reset() { + return MachineLearningModule::reset(); + } + +private: + MachineLearningModule module; +}; + +////////// typedefs for calling different algorithms +typedef machineLearning<classification> staticClassification; +typedef machineLearning<regression> staticRegression; + +typedef xmmToolConfig xmmConfig; +typedef machineLearning<xmmGmmTool> xmmStaticClassification; +typedef machineLearning<xmmGmrTool> xmmStaticRegression; +typedef machineLearning<xmmHmmTool> xmmTemporalClassification; +typedef machineLearning<xmmHmrTool> xmmTemporalRegression; + +typedef machineLearning<GVF> gvf; + +RAPIDMIX_END_NAMESPACE + +#endif diff --git a/src/rapidmix.h b/src/rapidmix.h index 3138488..9b493b1 100644 --- a/src/rapidmix.h +++ b/src/rapidmix.h @@ -1,3 +1,9 @@ +/* + * rapidmix.h + * Created by Michael Zbyszynski on 12 Jan 2017 + * Copyright © 2017 Goldsmiths. All rights reserved. + */ + ///////////////////////////////////////////////////////////////////////////// // ______ _ _ ___ ____ ___ ______ _____ // // | ___ \ (_) | | | \/ (_) / _ \ | ___ \_ _| // -- GitLab