regressionTutorial.cpp
Go to the documentation of this file.
1#include <shark/Data/Csv.h>
6
7#include <iostream>
8
9using namespace shark;
10using namespace std;
11
12
13//loads a pair of files
14RegressionDataset loadData(const std::string& dataFile,const std::string& labelFile){
15 //we first load two separate data files for the training inputs and the labels of the data point
16 Data<RealVector> inputs;
17 Data<RealVector> labels;
18 try {
19 importCSV(inputs, dataFile, ' ');
20 importCSV(labels, labelFile, ' ');
21 } catch (...) {
22 cerr << "Unable to open file " << dataFile << " and/or " << labelFile << ". Check paths!" << endl;
23 exit(EXIT_FAILURE);
24 }
25 //now we create a complete dataset which represents pairs of inputs and labels
26 RegressionDataset data(inputs, labels);
27 return data;
28}
29
30int main(){
31 //load some data set and split a test set from the dataset. The first 80% of data points are training data.
32 RegressionDataset data = loadData("data/regressionInputs.csv","data/regressionLabels.csv");
33 RegressionDataset test = splitAtElement(data,static_cast<std::size_t>(0.8*data.numberOfElements()));
34
35 //a linear model with as many in and outputs as the data has
37
38 //the squared loss can be used to calculate the mean squared error of the data and the model
39 //the ErrorFunction brings model, loss and data together and so automates evaluation
40 SquaredLoss<> loss;
41 ErrorFunction<> errorFunction(data, &model,&loss);
42
43 CG<> optimizer;
44 errorFunction.init();
45 optimizer.init(errorFunction);
46 for(int i = 0; i != 100; ++i)
47 {
48 optimizer.step(errorFunction);
49 }
50 //copy solution parameters into model
51 model.setParameterVector(optimizer.solution().point);
52
53 //save training error
54 double trainingError = optimizer.solution().value;
55
56 //evaluate test error
57 Data<RealVector> predictions = model(test.inputs());
58 double testError = loss.eval(test.labels(),predictions);
59
60 //print the results
61 cout << "RESULTS: " << endl;
62 cout << "======== \n" << endl;
63 cout << "training error " << trainingError << endl;
64 cout << "test error: " << testError << endl;
65}