diff --git a/src/machineLearning/machineLearning.cpp b/src/machineLearning/machineLearning.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b4951d44290230f25a99f8f1818441d8c6d3f4a --- /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 0000000000000000000000000000000000000000..6f810f7ad268a57f466872d3b29352c75701ea40 --- /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 3138488f3f8b09d9c1a56bae019aa498e5d314ff..9b493b174e5569fb5000489866786f28b7417b53 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. + */ + ///////////////////////////////////////////////////////////////////////////// // ______ _ _ ___ ____ ___ ______ _____ // // | ___ \ (_) | | | \/ (_) / _ \ | ___ \_ _| //