12#include <yarp/sig/Vector.h>
13#include <yarp/os/Property.h>
14#include <yarp/math/Math.h>
15#include <yarp/math/Rand.h>
17#define MIN(a, b) ((a < b) ? a : b)
21#define NOISE_MIN -0.05
26using namespace yarp::os;
27using namespace yarp::sig;
28using namespace yarp::math;
34 double a = std::exp(-10 *
x1);
35 double b = std::exp(-50 *
x2);
36 double c = 1.25 * std::exp(-5 * (
x1 +
x2));
37 return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
41 return std::sin(
x1 +
x2);
45 for(
size_t i = 0; i <
MIN(v1.size(), v2.size()); i++) {
46 v2[i] = v1[i] * v2[i];
52 for(
size_t i = 0; i < v.size(); i++) {
53 ret[i] = (d == 0.) ? v[i] : v[i] / d;
60 std::pair<Vector, Vector> sample;
61 sample.first.resize(2);
62 sample.second.resize(2);
63 sample.first[0] = Rand::scalar(-1, +1);
64 sample.first[1] = Rand::scalar(-1, +1);
65 sample.second[0] =
sin2d(sample.first[0], sample.first[1]);
66 sample.second[1] =
cross(sample.first[0], sample.first[1]);
83int main(
int argc,
char** argv) {
89 std::cout <<
"LearningMachine library example (indirect)" << std::endl;
94 Property
p(
"(dom 250) (cod 2) (lambda 0.5)");
96 std::cout <<
"Learner:" << std::endl << rls->
getInfo() << std::endl;
100 p.fromString(
"(dom 2) (cod 250) (gamma 16.0)",
true);
102 std::cout <<
"Transformer:" << std::endl << rf->
getInfo() << std::endl;
115 Vector noisyOutput = sample.second + Rand::vector(noise_min, noise_max);
118 Vector transInput = rf->
transform(sample.first);
125 trainMSE = trainMSE + diff;
131 std::cout <<
"Train MSE: " << trainMSE.toString() << std::endl;
135 for(
int i = 0; i <
NO_TEST; i++) {
140 Vector transInput = rf->
transform(sample.first);
147 testMSE = testMSE + diff;
150 std::cout <<
"Test MSE: " << testMSE.toString() << std::endl;
A generalized interface for a learning machine for offline and online learning machines (e....
virtual bool configure(yarp::os::Searchable &config)
Change parameters.
virtual Prediction predict(const yarp::sig::Vector &input)=0
Ask the learning machine to predict the output for a given input.
virtual std::string getInfo()
Asks the learning machine to return a string containing information on its operation so far.
virtual void feedSample(const yarp::sig::Vector &input, const yarp::sig::Vector &output)=0
Provide the learning machine with an example of the desired mapping.
A class that represents a prediction result.
yarp::sig::Vector getPrediction()
Accessor for the expected value of the prediction.
Recursive Regularized Least Squares (a.k.a.
Implementation of Random Feature preprocessing.
double cross(double x1, double x2)
Vector elementDiv(const Vector &v, double d)
void elementProd(const Vector &v1, Vector &v2)
double sin2d(double x1, double x2)
std::pair< Vector, Vector > createSample()