iCub-main
lmlibdirect.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2009 Arjan Gijsberts
3  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
4  *
5  * Example code how to use the learningMachine library in a direct manner.
6  */
7 
8 #include <iostream>
9 #include <cmath>
12 #include <yarp/sig/Vector.h>
13 #include <yarp/math/Rand.h>
14 
15 #define INPUT_MIN -10
16 #define INPUT_MAX 10
17 #define NO_TRAIN 400
18 #define NO_TEST 400
19 
20 using namespace iCub::learningmachine;
21 using namespace yarp::sig;
22 
23 std::pair<Vector, Vector> createSample(double min_in, double max_in) {
24  std::pair<Vector, Vector> sample;
25  sample.first.resize(1);
26  sample.second.resize(1);
27  double input = yarp::math::Rand::scalar(min_in, max_in);
28  sample.first[0] = input;
29  sample.second[0] = std::sin(input);
30  return sample;
31 }
32 
33 /*
34  * This example shows how LearningMachine classes can be used in a direct manner
35  * in your code. Please see all direct/indirect/portable examples to have an
36  * idea which method suits your application best.
37  *
38  * Please keep in mind that the purpose is to demonstrate how to interface with
39  * the learningMachine library. The synthetic data used in this example is
40  * utterly useless.
41  */
42 
43 int main(int argc, char** argv) {
44  std::cout << "LearningMachine library example (direct)" << std::endl;
45 
46  // one dimensional input, one dimensional output, c = 1.0
47  LSSVMLearner lssvm = LSSVMLearner(1, 1, 2.);
48  lssvm.getKernel()->setGamma(32.);
49 
50  // normalizer that scales [-10,10] -> [-1,1]
51  // IScalers only do R^1 -> R^1, for higher dimensions use ScaleTransformer
53 
54  // create and feed training samples
55  for(int i = 0; i < NO_TRAIN; i++) {
56  std::pair<Vector, Vector> sample = createSample(INPUT_MIN, INPUT_MAX);
57  sample.first[0] = scaler.transform(sample.first[0]);
58  lssvm.feedSample(sample.first, sample.second);
59  }
60 
61  // train the machine on the data (it's a batch machine!)
62  lssvm.train();
63 
64  // feed test samples
65  double MSE = 0.;
66  for(int i = 0; i < NO_TEST; i++) {
67  std::pair<Vector, Vector> sample = createSample(INPUT_MIN, INPUT_MAX);
68  sample.first[0] = scaler.transform(sample.first[0]);
69  Prediction prediction = lssvm.predict(sample.first);
70  double diff = sample.second[0] - prediction.getPrediction()[0];
71  MSE += diff * diff;
72  }
73  MSE /= NO_TEST;
74  std::cout << "MSE on test data after " << NO_TEST << " samples: " << MSE << std::endl;
75 }
76 
77 
A class that implements preprocessing based on a fixed range of outputs to a fixed range of outputs.
virtual double transform(double val)
Transforms a single sample value according to the state of the scaler.
Definition: IScaler.cpp:30
This is basic implementation of the LSSVM algorithms.
Definition: LSSVMLearner.h:120
virtual void feedSample(const yarp::sig::Vector &input, const yarp::sig::Vector &output)
Provide the learning machine with an example of the desired mapping.
Prediction predict(const yarp::sig::Vector &input)
Ask the learning machine to predict the output for a given input.
virtual void train()
Train the learning machine on the examples that have been supplied so far.
virtual RBFKernel * getKernel()
Accessor for the kernel.
Definition: LSSVMLearner.h:266
A class that represents a prediction result.
Definition: Prediction.h:44
yarp::sig::Vector getPrediction()
Accessor for the expected value of the prediction.
Definition: Prediction.h:106
virtual void setGamma(double g)
Definition: LSSVMLearner.h:70
#define INPUT_MAX
Definition: lmlibdirect.cpp:16
#define NO_TEST
Definition: lmlibdirect.cpp:18
int main(int argc, char **argv)
Definition: lmlibdirect.cpp:43
#define INPUT_MIN
Definition: lmlibdirect.cpp:15
#define NO_TRAIN
Definition: lmlibdirect.cpp:17
std::pair< Vector, Vector > createSample(double min_in, double max_in)
Definition: lmlibdirect.cpp:23