iCub-main
Loading...
Searching...
No Matches
RLSLearner.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 <sstream>
20#include <stdexcept>
21#include <cmath>
22
23#include <yarp/math/Math.h>
24
28
29using namespace yarp::math;
31using namespace iCub::learningmachine::math;
32
33namespace iCub {
34namespace learningmachine {
35
36
37RLSLearner::RLSLearner(unsigned int dom, unsigned int cod, double lambda) {
38 this->setName("RLS");
39 this->sampleCount = 0;
40 // make sure to not use initialization list to constructor of base for
41 // domain and codomain size, as it will not use overloaded mutators
42 this->setDomainSize(dom);
43 // slightly inefficient to use mutators, as we are initializing multiple times
44 this->setCoDomainSize(cod);
45 this->setLambda(lambda);
46}
47
49 : IFixedSizeLearner(other), sampleCount(other.sampleCount), R(other.R),
50 B(other.B), W(other.W), lambda(other.lambda) {
51}
52
55
57 if(this == &other) return *this; // handle self initialization
58
59 this->IFixedSizeLearner::operator=(other);
60 this->sampleCount = other.sampleCount;
61
62 this->R = other.R;
63 this->B = other.B;
64 this->W = other.W;
65 this->lambda = other.lambda;
66
67 return *this;
68}
69
70void RLSLearner::feedSample(const yarp::sig::Vector& input, const yarp::sig::Vector& output) {
71 this->IFixedSizeLearner::feedSample(input, output);
72
73 // update R
74 cholupdate(this->R, input);
75
76 // update B
77 this->B = this->B + outerprod(output, input);
78
79 // update W
80 cholsolve(this->R, this->B, this->W);
81
82 this->sampleCount++;
83}
84
86
87}
88
89Prediction RLSLearner::predict(const yarp::sig::Vector& input) {
90 this->checkDomainSize(input);
91
92 yarp::sig::Vector output = (this->W * input);
93
94 return Prediction(output);
95}
96
98 this->sampleCount = 0;
99 this->R = eye(this->getDomainSize(), this->getDomainSize()) * sqrt(this->lambda);
100 this->B = zeros(this->getCoDomainSize(), this->getDomainSize());
101 this->W = zeros(this->getCoDomainSize(), this->getDomainSize());
102}
103
104std::string RLSLearner::getInfo() {
105 std::ostringstream buffer;
106 buffer << this->IFixedSizeLearner::getInfo();
107 buffer << "Lambda: " << this->getLambda() << " | ";
108 buffer << "Sample Count: " << this->sampleCount << std::endl;
109 //for(unsigned int i = 0; i < this->machines.size(); i++) {
110 // buffer << " [" << (i + 1) << "] ";
111 // buffer << "lambda: " << this->machines[i]->getLambda();
112 // buffer << std::endl;
113 //}
114 return buffer.str();
115}
116
118 std::ostringstream buffer;
119 buffer << this->IFixedSizeLearner::getConfigHelp();
120 buffer << " lambda val Regularization parameter lambda" << std::endl;
121 return buffer.str();
122}
123
124void RLSLearner::writeBottle(yarp::os::Bottle& bot) {
125 bot << this->R << this->B << this->W << this->lambda << this->sampleCount;
126 // make sure to call the superclass's method
128}
129
130void RLSLearner::readBottle(yarp::os::Bottle& bot) {
131 // make sure to call the superclass's method
133 bot >> this->sampleCount >> this->lambda >> this->W >> this->B >> this->R;
134}
135
136void RLSLearner::setDomainSize(unsigned int size) {
138 this->reset();
139}
140
141void RLSLearner::setCoDomainSize(unsigned int size) {
143 this->reset();
144}
145
146void RLSLearner::setLambda(double l) {
147 if(l > 0.0) {
148 this->lambda = l;
149 this->reset();
150 } else{
151 throw std::runtime_error("Regularization parameter lamdba has to be larger than 0");
152 }
153}
154
156 return this->lambda;
157}
158
159
160bool RLSLearner::configure(yarp::os::Searchable& config) {
161 bool success = this->IFixedSizeLearner::configure(config);
162
163 // format: set lambda val
164 if(config.find("lambda").isFloat64() || config.find("lambda").isInt32()) {
165 this->setLambda(config.find("lambda").asFloat64());
166 success = true;
167 }
168
169 return success;
170}
171
172} // learningmachine
173} // iCub
174
175
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.
A class that represents a prediction result.
Definition Prediction.h:44
Recursive Regularized Least Squares (a.k.a.
Definition RLSLearner.h:46
void setCoDomainSize(unsigned int size)
Mutator for the codomain size.
void reset()
Forget everything and start over.
virtual std::string getInfo()
Asks the learning machine to return a string containing information on its operation so far.
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a machine from a bottle.
virtual Prediction predict(const yarp::sig::Vector &input)
Ask the learning machine to predict the output for a given input.
virtual std::string getConfigHelp()
Asks the learning machine to return a string containing the list of configuration options that it sup...
void setDomainSize(unsigned int size)
Mutator for the domain size.
virtual void writeBottle(yarp::os::Bottle &bot)
virtual void train()
Train the learning machine on the examples that have been supplied so far.
virtual ~RLSLearner()
Destructor.
void setLambda(double l)
Sets the regularization parameter \lambda to a specified value.
RLSLearner & operator=(const RLSLearner &other)
Assignment operator.
RLSLearner(unsigned int dom=1, unsigned int cod=1, double lambda=1.0)
Constructor.
double getLambda()
Accessor for the regularization parameter \lambda.
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 bool configure(yarp::os::Searchable &config)
Change parameters.
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
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.