regressionTutorial.cpp
Go to the documentation of this file.
1 #include <shark/Data/Csv.h>
6 
7 #include <iostream>
8 
9 using namespace shark;
10 using namespace std;
11 
12 
13 //loads a pair of files
14 RegressionDataset 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 
30 int 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
36  LinearModel<> model(inputDimension(data), labelDimension(data));
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 
51  //save training error
52  double trainingError = optimizer.solution().value;
53 
54  //evaluate test error
55  model.setParameterVector(optimizer.solution().point);
56  Data<RealVector> predictions = model(test.inputs());
57  double testError = loss.eval(test.labels(),predictions);
58 
59  //print the results
60  cout << "RESULTS: " << endl;
61  cout << "======== \n" << endl;
62  cout << "training error " << trainingError << endl;
63  cout << "test error: " << testError << endl;
64 }