iCub-main
Loading...
Searching...
No Matches
SparseSpectrumFeature.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 <sstream>
22#include <algorithm>
23#include <cmath>
24
25#include <yarp/math/Math.h>
26#include <yarp/math/Rand.h>
27
31
32//#define TWOPI 6.2831853071795862
33
34using namespace yarp::math;
35using namespace iCub::learningmachine::math;
37
38namespace iCub {
39namespace learningmachine {
40
41SparseSpectrumFeature::SparseSpectrumFeature(unsigned int dom, unsigned int cod, double sigma,
42 yarp::sig::Vector ell) {
43 this->setName("SparseSpectrumFeature");
44 // ell has to be initialized *prior* to anything that could reset this instance
45 this->setEll(ell);
46 this->setDomainSize(dom);
47 this->setCoDomainSize(cod);
48 // will initiate reset automatically
49 this->setSigma(sigma);
50}
51
52yarp::sig::Vector SparseSpectrumFeature::transform(const yarp::sig::Vector& input) {
53 yarp::sig::Vector output = this->IFixedSizeTransformer::transform(input);
54
55 yarp::sig::Vector inputW = (this->W * input);
56 int nproj = this->getCoDomainSize() >> 1;
57 double factor = this->sigma / sqrt((double)nproj);
58 for(int i = 0; i < nproj; i++) {
59 output(i) = cos(inputW(i)) * factor;
60 output(i+nproj) = sin(inputW(i)) * factor;
61 }
62 return output;
63}
64
65void SparseSpectrumFeature::setDomainSize(unsigned int size) {
66 // call method in base class
68 // rebuild projection matrix
69 this->reset();
70}
71
73 if((size & 0x1) == 0) {
74 // call method in base class
76 // rebuild projection matrix
77 this->reset();
78 } else {
79 throw std::runtime_error("Size of codomain of Sparse Spectrum Features has to be even");
80 }
81}
82
85
86 // create pseudo random number generators
87 yarp::math::RandnScalar prng_normal;
88
89 // create new projection matrix
90 this->W = yarp::sig::Matrix((int) this->getCoDomainSize() / 2, (int) this->getDomainSize());
91
92 // fill projection matrix with random and scaled data
93 for(int r = 0; r < this->W.rows(); r++) {
94 for(int c = 0; c < this->W.cols(); c++) {
95 this->W(r, c) = prng_normal.get() / this->ell(c);
96 }
97 }
98}
99
100void SparseSpectrumFeature::writeBottle(yarp::os::Bottle& bot) {
101 bot << this->getSigma() << this->ell << this->W;
102
103 // make sure to call the superclass's method
105}
106
107void SparseSpectrumFeature::readBottle(yarp::os::Bottle& bot) {
108 // make sure to call the superclass's method
110
111 // directly write to sigma to prevent resetting W
112 bot >> this->W >> this->ell >> this->sigma;
113 this->setSigma(sigma);
114}
115
116void SparseSpectrumFeature::setEll(yarp::sig::Vector& ell) {
117 yarp::sig::Vector ls = yarp::sig::Vector(this->getDomainSize());
118 ls = 1.;
119 for(unsigned int i = 0; (int)i < std::min(ell.size(), ls.size()); i++) {
120 ls(i) = ell(i);
121 }
122 this->ell = ls;
123 // rebuild projection matrix
124 this->reset();
125}
126
128 std::ostringstream buffer;
129 buffer << this->IFixedSizeTransformer::getInfo();
130 buffer << " sigma: " << this->sigma << " | ";
131 buffer << " ell: " << this->ell.toString();
132 return buffer.str();
133}
134
136 std::ostringstream buffer;
137 buffer << this->IFixedSizeTransformer::getConfigHelp();
138 buffer << " sigma val Set sigma parameter" << std::endl;
139 buffer << " ell (list) Set lambda parameter" << std::endl;
140 return buffer.str();
141}
142
143bool SparseSpectrumFeature::configure(yarp::os::Searchable &config) {
144 bool success = this->IFixedSizeTransformer::configure(config);
145
146 // format: set sigma val
147 if(config.find("sigma").isFloat64() || config.find("sigma").isInt32()) {
148 this->setSigma(config.find("sigma").asFloat64());
149 success = true;
150 }
151
152 // format: set ell val | set ell (val ... val)
153 if(config.find("ell").isFloat64() || config.find("ell").isInt32()) {
154 yarp::sig::Vector ls = yarp::sig::Vector(this->getDomainSize());
155 ls = config.find("ell").asFloat64();
156 this->setEll(ls);
157 success = true;
158 } else if(config.find("ell").isList()) {
159 yarp::os::Bottle* b = config.find("ell").asList();
160 assert(b != (yarp::os::Bottle*) 0x0);
161 yarp::sig::Vector ls(0);
162 for(int i = 0; i < b->size(); i++) {
163 if(b->get(i).isFloat64() || b->get(i).isInt32()) {
164 ls.push_back(b->get(i).asFloat64());
165 }
166 }
167 this->setEll(ls);
168 success = true;
169 }
170 return success;
171}
172
173
174} // learningmachine
175} // iCub
176
virtual std::string getConfigHelp()
Asks the transformer to return a string containing the list of configuration options that it supports...
virtual yarp::sig::Vector transform(const yarp::sig::Vector &input)
Transforms an input vector.
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a transformer from a bottle.
virtual void setDomainSize(unsigned int size)
Mutator for the domain size.
unsigned int getCoDomainSize() const
Returns the size (dimensionality) of the output domain (codomain).
virtual void writeBottle(yarp::os::Bottle &bot) const
Writes a serialization of the transformer into a bottle.
virtual bool configure(yarp::os::Searchable &config)
virtual void setCoDomainSize(unsigned int size)
Mutator for the codomain size.
virtual std::string getInfo()
Asks the transformer to return a string containing statistics on its operation so far.
unsigned int getDomainSize() const
Returns the size (dimensionality) of the input domain.
virtual void reset()
Forget everything and start over.
void setName(std::string name)
Set the name of this transformer.
yarp::sig::Vector ell
Characteristic length-scales ell, analoguous to same parameter in asymmetric RBF kernel.
virtual std::string getConfigHelp()
Asks the transformer to return a string containing the list of configuration options that it supports...
virtual void reset()
Forget everything and start over.
virtual double getSigma()
Accessor for the sigma parameter.
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a transformer from a bottle.
virtual void setSigma(double s)
Mutator for the sigma parameter.
virtual void setDomainSize(unsigned int size)
Mutator for the domain size.
virtual bool configure(yarp::os::Searchable &config)
virtual yarp::sig::Vector transform(const yarp::sig::Vector &input)
Transforms an input vector.
virtual std::string getInfo()
Asks the transformer to return a string containing statistics on its operation so far.
virtual void setEll(yarp::sig::Vector &ell)
Mutator for the characteristic length-scales parameter ell.
virtual void setCoDomainSize(unsigned int size)
Mutator for the codomain size.
SparseSpectrumFeature(unsigned int dom=1, unsigned int cod=2, double sigma=1., yarp::sig::Vector ell=yarp::sig::Vector(0))
Constructor.
yarp::sig::Matrix W
Projection matrix W.
virtual void writeBottle(yarp::os::Bottle &bot)
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.