25 #include <yarp/math/Math.h>
26 #include <yarp/math/Rand.h>
34 using namespace yarp::math;
39 namespace learningmachine {
41 SparseSpectrumFeature::SparseSpectrumFeature(
unsigned int dom,
unsigned int cod,
double sigma,
42 yarp::sig::Vector ell) {
43 this->setName(
"SparseSpectrumFeature");
46 this->setDomainSize(dom);
47 this->setCoDomainSize(cod);
49 this->setSigma(sigma);
52 yarp::sig::Vector SparseSpectrumFeature::transform(
const yarp::sig::Vector& input) {
53 yarp::sig::Vector output = this->IFixedSizeTransformer::transform(input);
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;
65 void SparseSpectrumFeature::setDomainSize(
unsigned int size) {
67 this->IFixedSizeTransformer::setDomainSize(size);
72 void SparseSpectrumFeature::setCoDomainSize(
unsigned int size) {
73 if((size & 0x1) == 0) {
75 this->IFixedSizeTransformer::setCoDomainSize(size);
79 throw std::runtime_error(
"Size of codomain of Sparse Spectrum Features has to be even");
83 void SparseSpectrumFeature::reset() {
84 this->IFixedSizeTransformer::reset();
87 yarp::math::RandnScalar prng_normal;
90 this->W = yarp::sig::Matrix((
int) this->getCoDomainSize() / 2, (
int) this->getDomainSize());
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);
100 void SparseSpectrumFeature::writeBottle(yarp::os::Bottle& bot) {
101 bot << this->getSigma() << this->ell << this->W;
104 this->IFixedSizeTransformer::writeBottle(bot);
107 void SparseSpectrumFeature::readBottle(yarp::os::Bottle& bot) {
109 this->IFixedSizeTransformer::readBottle(bot);
112 bot >> this->W >> this->ell >> this->sigma;
113 this->setSigma(sigma);
116 void SparseSpectrumFeature::setEll(yarp::sig::Vector& ell) {
117 yarp::sig::Vector ls = yarp::sig::Vector(this->getDomainSize());
119 for(
unsigned int i = 0; (int)i <
std::min(ell.size(), ls.size()); i++) {
127 std::string SparseSpectrumFeature::getInfo() {
128 std::ostringstream buffer;
129 buffer << this->IFixedSizeTransformer::getInfo();
130 buffer <<
" sigma: " << this->sigma <<
" | ";
131 buffer <<
" ell: " << this->ell.toString();
135 std::string SparseSpectrumFeature::getConfigHelp() {
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;
143 bool SparseSpectrumFeature::configure(yarp::os::Searchable &config) {
144 bool success = this->IFixedSizeTransformer::configure(config);
147 if(config.find(
"sigma").isFloat64() || config.find(
"sigma").isInt32()) {
148 this->setSigma(config.find(
"sigma").asFloat64());
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();
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());
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.