23 #include <yarp/math/Math.h>
24 #include <yarp/math/SVD.h>
29 using namespace yarp::math;
33 namespace learningmachine {
35 double RBFKernel::evaluate(
const yarp::sig::Vector& v1,
const yarp::sig::Vector& v2) {
36 assert(v1.size() == v2.size());
40 for(
size_t i = 0; i < v1.size(); i++) {
42 result += diff * diff;
44 result *= -1 * this->gamma;
49 LSSVMLearner::LSSVMLearner(
unsigned int dom,
unsigned int cod,
double c) {
50 this->setName(
"LSSVM");
54 this->setDomainSize(dom);
55 this->setCoDomainSize(cod);
61 alphas(other.alphas), bias(other.bias), LOO(other.LOO), C(other.C),
71 if(
this == &other)
return *
this;
73 this->IFixedSizeLearner::operator=(other);
74 this->inputs = other.inputs;
75 this->outputs = other.outputs;
76 this->alphas = other.alphas;
77 this->bias = other.bias;
78 this->LOO = other.LOO;
81 this->kernel =
new RBFKernel(*other.kernel);
90 this->inputs.push_back(input);
91 this->outputs.push_back(output);
95 assert(this->inputs.size() == this->outputs.size());
98 if(inputs.size() == 0) {
103 yarp::sig::Matrix K(inputs.size() + 1, inputs.size() + 1);
104 for(
int r = 0; r < K.rows() - 1; r++) {
106 for(
int c = 0; c <= r; c++) {
107 K(r, c) = K(c, r) = this->kernel->
evaluate(this->inputs[r], this->inputs[c]);
108 if(r == c) K(r, c) += (1.0 / this->C);
111 for(
int i = 0; i < K.rows() - 1; i++) {
112 K(i, K.cols() - 1) = K(K.rows() - 1, i) = 1.;
114 K(K.rows() - 1, K.cols() - 1) = 0.;
117 yarp::sig::Matrix Kinv = luinv(K);
120 yarp::sig::Matrix Y =
zeros(this->outputs.size() + 1, this->getCoDomainSize());
121 for(
int r = 0; r < Y.rows() - 1; r++) {
122 for(
int c = 0; c < Y.cols(); c++) {
123 Y(r, c) = this->outputs[r](c);
127 yarp::sig::Matrix result = Kinv * Y;
128 this->alphas = result.submatrix(0, result.rows() - 2, 0, result.cols() - 1);
129 this->bias = result.getRow(result.rows() - 1);
135 yarp::sig::Vector alphas_i = this->alphas.getCol(i);
136 for(
size_t j = 0; j < alphas_i.size(); j++) {
137 double err = alphas_i(j) / Kinv(j, j);
138 this->LOO(i) += err * err;
140 this->LOO(i) /= alphas_i.size();
148 if(this->inputs.size() == 0) {
153 yarp::sig::Vector k(this->inputs.size());
154 for(
size_t i = 0; i < k.size(); i++) {
155 k(i) = this->kernel->
evaluate(this->inputs[i], input);
158 return Prediction((this->alphas.transposed() * k) + this->bias);
162 this->inputs.clear();
163 this->outputs.clear();
164 this->alphas = yarp::sig::Matrix();
174 std::ostringstream buffer;
176 buffer <<
"C: " << this->
getC() <<
" | ";
177 buffer <<
"Collected Samples: " << this->inputs.size() <<
" | ";
178 buffer <<
"Training Samples: " << this->alphas.rows() <<
" | ";
179 buffer <<
"Kernel: " << this->kernel->
getInfo() << std::endl;
180 buffer <<
"LOO: " << this->LOO.toString() << std::endl;
185 std::ostringstream buffer;
188 buffer <<
" c val Tradeoff parameter C" << std::endl;
195 bot << this->kernel->
getGamma() << this->
getC() << this->bias
199 for(
unsigned int i = 0; i < this->inputs.size(); i++) {
201 bot.addFloat64(this->inputs[i](d));
204 bot.addInt32(this->inputs.size());
207 for(
unsigned int i = 0; i < this->outputs.size(); i++) {
209 bot.addFloat64(this->outputs[i](d));
212 bot.addInt32(this->outputs.size());
223 this->outputs.resize(bot.pop().asInt32());
224 for(
int i = this->outputs.size() - 1; i >= 0; i--) {
227 this->outputs[i](d) = bot.pop().asFloat64();
232 this->inputs.resize(bot.pop().asInt32());
233 for(
int i = this->inputs.size() - 1; i >= 0; i--) {
236 this->inputs[i](d) = bot.pop().asFloat64();
242 bot >> this->alphas >> this->bias >> c >> gamma;
260 if(config.find(
"c").isFloat64() || config.find(
"c").isInt32()) {
261 double val = config.find(
"c").asFloat64();
263 this->
setC(config.find(
"c").asFloat64());
268 success |= this->kernel->
configure(config);
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.
This is basic implementation of the LSSVM algorithms.
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 writeBottle(yarp::os::Bottle &bot)
virtual double getC()
Accessor for the regularization parameter C.
void setCoDomainSize(unsigned int size)
Mutator for the codomain size.
void reset()
Forget everything and start over.
virtual ~LSSVMLearner()
Destructor.
void setDomainSize(unsigned int size)
Mutator for the domain size.
virtual void setC(double C)
Mutator for the regularization parameter C.
LSSVMLearner * clone()
Asks the learning machine to return a clone of its type.
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 void readBottle(yarp::os::Bottle &bot)
Unserializes a machine from a bottle.
LSSVMLearner(unsigned int dom=1, unsigned int cod=1, double c=1.0)
Constructor.
virtual std::string getInfo()
Asks the learning machine to return a string containing information on its operation so far.
virtual LSSVMLearner & operator=(const LSSVMLearner &other)
Assignment operator.
virtual std::string getConfigHelp()
Asks the learning machine to return a string containing the list of configuration options that it sup...
virtual bool configure(yarp::os::Searchable &config)
Change parameters.
A class that represents a prediction result.
virtual std::string getConfigHelp()
virtual std::string getInfo()
virtual void setGamma(double g)
virtual double evaluate(const yarp::sig::Vector &v1, const yarp::sig::Vector &v2)
virtual bool configure(yarp::os::Searchable &config)
virtual double getGamma()
PortablePair< Vector, Vector > Prediction
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.