iCub-main
Loading...
Searching...
No Matches
RandomFeature.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 <sstream>
21#include <cmath>
22
23#include <yarp/math/Math.h>
24#include <yarp/math/Rand.h>
25
29
30#define TWOPI 6.2831853071795862
31
32using namespace yarp::math;
34using namespace iCub::learningmachine::math;
35
36namespace iCub {
37namespace learningmachine {
38
39RandomFeature::RandomFeature(unsigned int dom, unsigned int cod, double gamma) {
40 this->setName("RandomFeature");
41 this->setDomainSize(dom);
42 this->setCoDomainSize(cod);
43 // will initiate reset automatically
44 this->setGamma(gamma);
45}
46
47yarp::sig::Vector RandomFeature::transform(const yarp::sig::Vector& input) {
48 yarp::sig::Vector output = this->IFixedSizeTransformer::transform(input);
49
50 // python: x_f = numpy.cos(numpy.dot(self.W, x) + self.bias) / math.sqrt(self.nproj)
51 output = cosvec((this->W * input) + this->b) * (1. / std::sqrt((double) this->getCoDomainSize()));
52 return output;
53}
54
55void RandomFeature::setDomainSize(unsigned int size) {
56 // call method in base class
58 // rebuild projection matrix
59 this->reset();
60}
61
62void RandomFeature::setCoDomainSize(unsigned int size) {
63 // call method in base class
65 // rebuild projection matrix
66 this->reset();
67}
68
71
72 // create pseudo random number generators
73 yarp::math::RandnScalar prng_normal;
74 yarp::math::RandScalar prng_uniform;
75
76 // create new projection matrix
77 this->W = sqrt(2 * this->gamma) * random(this->getCoDomainSize(), this->getDomainSize(), prng_normal);
78
79 this->b = TWOPI * random(this->getCoDomainSize(), prng_uniform);
80}
81
82void RandomFeature::writeBottle(yarp::os::Bottle& bot) const {
83 bot << this->getGamma() << b << W;
84 // make sure to call the superclass's method
86}
87
88void RandomFeature::readBottle(yarp::os::Bottle& bot) {
89 // make sure to call the superclass's method
91 // do _not_ use public accessor, as it resets the matrix
92 bot >> W >> this->b >> this->gamma;
93}
94
95
96
98 std::ostringstream buffer;
99 buffer << this->IFixedSizeTransformer::getInfo();
100 buffer << " gamma: " << this->gamma;
101 return buffer.str();
102}
103
105 std::ostringstream buffer;
106 buffer << this->IFixedSizeTransformer::getConfigHelp();
107 buffer << " gamma val Set gamma parameter" << std::endl;
108 return buffer.str();
109}
110
111bool RandomFeature::configure(yarp::os::Searchable &config) {
112 bool success = this->IFixedSizeTransformer::configure(config);
113
114 // format: set gamma val
115 if(config.find("gamma").isFloat64() || config.find("gamma").isInt32()) {
116 this->setGamma(config.find("gamma").asFloat64());
117 success = true;
118 }
119 return success;
120}
121
122
123} // learningmachine
124} // iCub
125
#define TWOPI
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.
RandomFeature(unsigned int dom=1, unsigned int cod=1, double gamma=1.)
Constructor.
double gamma
Gamma parameter, analoguous to same parameter in RBF kernel.
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a transformer from a bottle.
virtual bool configure(yarp::os::Searchable &config)
virtual std::string getConfigHelp()
Asks the transformer to return a string containing the list of configuration options that it supports...
yarp::sig::Matrix W
Projection matrix W.
virtual yarp::sig::Vector transform(const yarp::sig::Vector &input)
Transforms an input vector.
yarp::sig::Vector b
Bias vector b.
virtual void reset()
Forget everything and start over.
virtual void setCoDomainSize(unsigned int size)
Mutator for the codomain size.
virtual void setDomainSize(unsigned int size)
Mutator for the domain size.
virtual std::string getInfo()
Asks the transformer to return a string containing statistics on its operation so far.
virtual void setGamma(double g)
Mutator for the gamma parameter.
virtual void writeBottle(yarp::os::Bottle &bot) const
Writes a serialization of the transformer into a bottle.
virtual double getGamma() const
Accessor for the gamma parameter.
yarp::sig::Vector & cosvec(yarp::sig::Vector &v)
Computes the cosine of a vector element-wise inplace.
Definition Math.cpp:339
yarp::sig::Vector random(int length, yarp::math::RandScalar &prng)
Returns a random vector with given dimensionality.
Definition Math.cpp:281
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.