23 #include <yarp/math/Math.h>
24 #include <yarp/math/Rand.h>
30 #define TWOPI 6.2831853071795862
32 using namespace yarp::math;
37 namespace learningmachine {
39 RandomFeature::RandomFeature(
unsigned int dom,
unsigned int cod,
double gamma) {
40 this->setName(
"RandomFeature");
41 this->setDomainSize(dom);
42 this->setCoDomainSize(cod);
44 this->setGamma(gamma);
47 yarp::sig::Vector RandomFeature::transform(
const yarp::sig::Vector& input) {
48 yarp::sig::Vector output = this->IFixedSizeTransformer::transform(input);
51 output =
cosvec((this->W * input) + this->b) * (1. / std::sqrt((
double) this->getCoDomainSize()));
55 void RandomFeature::setDomainSize(
unsigned int size) {
57 this->IFixedSizeTransformer::setDomainSize(size);
62 void RandomFeature::setCoDomainSize(
unsigned int size) {
64 this->IFixedSizeTransformer::setCoDomainSize(size);
69 void RandomFeature::reset() {
70 this->IFixedSizeTransformer::reset();
73 yarp::math::RandnScalar prng_normal;
74 yarp::math::RandScalar prng_uniform;
77 this->W = sqrt(2 * this->gamma) *
random(this->getCoDomainSize(), this->getDomainSize(), prng_normal);
79 this->b =
TWOPI *
random(this->getCoDomainSize(), prng_uniform);
82 void RandomFeature::writeBottle(yarp::os::Bottle& bot)
const {
83 bot << this->getGamma() << b << W;
85 this->IFixedSizeTransformer::writeBottle(bot);
88 void RandomFeature::readBottle(yarp::os::Bottle& bot) {
90 this->IFixedSizeTransformer::readBottle(bot);
92 bot >> W >> this->b >> this->gamma;
97 std::string RandomFeature::getInfo() {
98 std::ostringstream buffer;
99 buffer << this->IFixedSizeTransformer::getInfo();
100 buffer <<
" gamma: " << this->gamma;
104 std::string RandomFeature::getConfigHelp() {
105 std::ostringstream buffer;
106 buffer << this->IFixedSizeTransformer::getConfigHelp();
107 buffer <<
" gamma val Set gamma parameter" << std::endl;
111 bool RandomFeature::configure(yarp::os::Searchable &config) {
112 bool success = this->IFixedSizeTransformer::configure(config);
115 if(config.find(
"gamma").isFloat64() || config.find(
"gamma").isInt32()) {
116 this->setGamma(config.find(
"gamma").asFloat64());
yarp::sig::Vector & cosvec(yarp::sig::Vector &v)
Computes the cosine of a vector element-wise inplace.
yarp::sig::Vector random(int length, yarp::math::RandScalar &prng)
Returns a random vector with given dimensionality.
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.