iCub-main
Loading...
Searching...
No Matches
LinearGPRLearner.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2007-2011 RobotCub Consortium, European Commission FP6 Project IST-004370
3 * author: Arjan Gijsberts
4 * email: arjan.gijsberts@iit.it
5 * website: www.robotcub.org
6 * Permission is granted to copy, distribute, and/or modify this program
7 * under the terms of the GNU General Public License, version 2 or any
8 * later version published by the Free Software Foundation.
9 *
10 * A copy of the license can be found at
11 * http://www.robotcub.org/icub/license/gpl.txt
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 * Public License for more details
17 */
18
19#include <cassert>
20#include <stdexcept>
21#include <cmath>
22
23#include <iostream>
24
25#include <yarp/math/Math.h>
26
30
31using namespace yarp::math;
33using namespace iCub::learningmachine::math;
34
35
37
38namespace iCub {
39namespace learningmachine {
40
41LinearGPRLearner::LinearGPRLearner(unsigned int dom, unsigned int cod, double sigma) {
42 this->setName("LinearGPR");
43 this->sampleCount = 0;
44 // make sure to not use initialization list to constructor of base for
45 // domain and codomain size, as it will not use overloaded mutators
46 this->setDomainSize(dom);
47 // slightly inefficient to use mutators, as we are initializing multiple times
48 this->setCoDomainSize(cod);
49 this->setSigma(sigma);
50}
51
53 : IFixedSizeLearner(other), sampleCount(other.sampleCount), R(other.R),
54 B(other.B), W(other.W), sigma(other.sigma) {
55}
56
59
61 if(this == &other) return *this; // handle self initialization
62
63 this->IFixedSizeLearner::operator=(other);
64 this->sampleCount = other.sampleCount;
65
66 this->R = other.R;
67 this->B = other.B;
68 this->W = other.W;
69 this->sigma = other.sigma;
70
71 return *this;
72}
73
74void LinearGPRLearner::feedSample(const yarp::sig::Vector& input, const yarp::sig::Vector& output) {
75 this->IFixedSizeLearner::feedSample(input, output);
76
77 // update R
78 cholupdate(this->R, input);
79
80 // update B
81 this->B = this->B + outerprod(output, input);
82
83 // update W
84 cholsolve(this->R, this->B, this->W);
85
86 this->sampleCount++;
87}
88
92
93Prediction LinearGPRLearner::predict(const yarp::sig::Vector& input) {
94 this->checkDomainSize(input);
95
96 yarp::sig::Vector output = (this->W * input);
97
98 // note that all output dimensions share the same hyperparameters and input samples,
99 // the predicted variance is therefore identical
100 yarp::sig::Vector v = trsolve(this->R, input, true);
101 yarp::sig::Vector std(output.size());
102 std = this->sigma * sqrt(1. + dot(v,v));
103
104 return Prediction(output, std);
105}
106
108 this->sampleCount = 0;
109 this->R = eye(this->getDomainSize(), this->getDomainSize()) * this->sigma;
110 this->B = zeros(this->getCoDomainSize(), this->getDomainSize());
111 this->W = zeros(this->getCoDomainSize(), this->getDomainSize());
112}
113
115 std::ostringstream buffer;
116 buffer << this->IFixedSizeLearner::getInfo();
117 buffer << "Sigma: " << this->getSigma() << " | ";
118 buffer << "Sample Count: " << this->sampleCount << std::endl;
119 //for(unsigned int i = 0; i < this->machines.size(); i++) {
120 // buffer << " [" << (i + 1) << "] ";
121 // buffer << "lambda: " << this->machines[i]->getLambda();
122 // buffer << std::endl;
123 //}
124 return buffer.str();
125}
126
128 std::ostringstream buffer;
129 buffer << this->IFixedSizeLearner::getConfigHelp();
130 buffer << " sigma val Signal noise sigma" << std::endl;
131 return buffer.str();
132}
133
134void LinearGPRLearner::writeBottle(yarp::os::Bottle& bot) {
135 bot << this->R << this->B << this->W << this->sigma << this->sampleCount;
136 // make sure to call the superclass's method
138}
139
140void LinearGPRLearner::readBottle(yarp::os::Bottle& bot) {
141 // make sure to call the superclass's method
143 bot >> this->sampleCount >> this->sigma >> this->W >> this->B >> this->R;
144}
145
146void LinearGPRLearner::setDomainSize(unsigned int size) {
148 this->reset();
149}
150
151void LinearGPRLearner::setCoDomainSize(unsigned int size) {
153 this->reset();
154}
155
157 if(s > 0.0) {
158 this->sigma = s;
159 this->reset();
160 } else{
161 throw std::runtime_error("Signal noise sigma has to be larger than 0");
162 }
163}
164
166 return this->sigma;
167}
168
169
170bool LinearGPRLearner::configure(yarp::os::Searchable& config) {
171 bool success = this->IFixedSizeLearner::configure(config);
172
173 // format: set sigma val
174 if(config.find("sigma").isFloat64() || config.find("sigma").isInt32()) {
175 this->setSigma(config.find("sigma").asFloat64());
176 success = true;
177 }
178
179 return success;
180}
181
182} // learningmachine
183} // iCub
184
185
An generalized interface for a learning machine with a fixed domain and codomain size.
virtual void writeBottle(yarp::os::Bottle &bot) const
Writes a serialization of the machine into a bottle.
virtual void feedSample(const yarp::sig::Vector &input, const yarp::sig::Vector &output)
Provide the learning machine with an example of the desired mapping.
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a machine from a bottle.
virtual bool checkDomainSize(const yarp::sig::Vector &input)
Checks whether the input is of the desired dimensionality.
unsigned int getCoDomainSize() const
Returns the size (dimensionality) of the output domain (codomain).
virtual std::string getInfo()
Asks the learning machine to return a string containing information on its operation so far.
virtual void setCoDomainSize(unsigned int size)
Mutator for the codomain size.
virtual bool configure(yarp::os::Searchable &config)
Change parameters.
virtual void setDomainSize(unsigned int size)
Mutator for the domain size.
virtual std::string getConfigHelp()
Asks the learning machine to return a string containing the list of configuration options that it sup...
unsigned int getDomainSize() const
Returns the size (dimensionality) of the input domain.
void setName(const std::string &name)
Set the name of this machine learning technique.
Standard linear Bayesian regression or, equivalently, Gaussian Process Regression with a linear covar...
virtual Prediction predict(const yarp::sig::Vector &input)
Ask the learning machine to predict the output for a given input.
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a machine from a bottle.
virtual std::string getConfigHelp()
Asks the learning machine to return a string containing the list of configuration options that it sup...
virtual std::string getInfo()
Asks the learning machine to return a string containing information on its operation so far.
LinearGPRLearner(unsigned int dom=1, unsigned int cod=1, double sigma=1.0)
Constructor.
LinearGPRLearner & operator=(const LinearGPRLearner &other)
Assignment operator.
double getSigma()
Accessor for the signal noise \sigma.
void setSigma(double s)
Sets the signal noise \sigma to a specified value.
virtual bool configure(yarp::os::Searchable &config)
Change parameters.
virtual void train()
Train the learning machine on the examples that have been supplied so far.
virtual void writeBottle(yarp::os::Bottle &bot)
void setCoDomainSize(unsigned int size)
Mutator for the codomain size.
void setDomainSize(unsigned int size)
Mutator for the domain size.
virtual void feedSample(const yarp::sig::Vector &input, const yarp::sig::Vector &output)
Provide the learning machine with an example of the desired mapping.
void reset()
Forget everything and start over.
A class that represents a prediction result.
Definition Prediction.h:44
zeros(2, 2) eye(2
void cholupdate(yarp::sig::Matrix &R, const yarp::sig::Vector &x, yarp::sig::Vector &c, yarp::sig::Vector &s, yarp::sig::Matrix &Z, const yarp::sig::Vector &y, yarp::sig::Vector &rho, bool rtrans=0, bool ztrans=0)
Perform a rank-1 update to a Cholesky factor, while updating additional vectors using the used Given'...
Definition Math.cpp:136
yarp::sig::Matrix outerprod(const yarp::sig::Vector &v1, const yarp::sig::Vector &v2)
Computes the outer product of two vectors.
Definition Math.cpp:213
void cholsolve(const yarp::sig::Matrix &R, const yarp::sig::Matrix &B, yarp::sig::Matrix &X)
Solves a system A*x=b for multiple row vectors in B using a precomputed Cholesky factor R.
Definition Math.cpp:167
void trsolve(const yarp::sig::Matrix &A, const yarp::sig::Vector &b, yarp::sig::Vector &x, bool transa=false)
Solves a triangular linear system Ax=b where A is triangular.
Definition Math.cpp:230
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.