iCub-main
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 
32 using namespace yarp::math;
34 using namespace iCub::learningmachine::math;
35 
36 namespace iCub {
37 namespace learningmachine {
38 
39 RandomFeature::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 
47 yarp::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 
55 void RandomFeature::setDomainSize(unsigned int size) {
56  // call method in base class
57  this->IFixedSizeTransformer::setDomainSize(size);
58  // rebuild projection matrix
59  this->reset();
60 }
61 
62 void RandomFeature::setCoDomainSize(unsigned int size) {
63  // call method in base class
64  this->IFixedSizeTransformer::setCoDomainSize(size);
65  // rebuild projection matrix
66  this->reset();
67 }
68 
69 void RandomFeature::reset() {
70  this->IFixedSizeTransformer::reset();
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 
82 void RandomFeature::writeBottle(yarp::os::Bottle& bot) const {
83  bot << this->getGamma() << b << W;
84  // make sure to call the superclass's method
85  this->IFixedSizeTransformer::writeBottle(bot);
86 }
87 
88 void RandomFeature::readBottle(yarp::os::Bottle& bot) {
89  // make sure to call the superclass's method
90  this->IFixedSizeTransformer::readBottle(bot);
91  // do _not_ use public accessor, as it resets the matrix
92  bot >> W >> this->b >> this->gamma;
93 }
94 
95 
96 
97 std::string RandomFeature::getInfo() {
98  std::ostringstream buffer;
99  buffer << this->IFixedSizeTransformer::getInfo();
100  buffer << " gamma: " << this->gamma;
101  return buffer.str();
102 }
103 
104 std::string RandomFeature::getConfigHelp() {
105  std::ostringstream buffer;
106  buffer << this->IFixedSizeTransformer::getConfigHelp();
107  buffer << " gamma val Set gamma parameter" << std::endl;
108  return buffer.str();
109 }
110 
111 bool 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
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.