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  optimizer.init(errorFunction);
45  for(int i = 0; i != 100; ++i)
46  {
47  optimizer.step(errorFunction);
48  }
49 
50  //save training error
51  double trainingError = optimizer.solution().value;
52 
53  //evaluate test error
54  model.setParameterVector(optimizer.solution().point);
55  Data<RealVector> predictions = model(test.inputs());
56  double testError = loss.eval(test.labels(),predictions);
57 
58  //print the results
59  cout << "RESULTS: " << endl;
60  cout << "======== \n" << endl;
61  cout << "training error " << trainingError << endl;
62  cout << "test error: " << testError << endl;
63 }