16 #include <yarp/sig/Vector.h>
17 #include <yarp/os/Property.h>
18 #include <yarp/math/Math.h>
19 #include <yarp/math/Rand.h>
20 #include <yarp/os/SystemClock.h>
22 #define MIN(a, b) ((a < b) ? a : b)
26 #define NOISE_MIN -0.05
27 #define NOISE_MAX 0.05
31 using namespace yarp::os;
32 using namespace yarp::sig;
33 using namespace yarp::math;
42 return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
46 return std::sin(
x1 +
x2);
50 for(
size_t i = 0; i <
MIN(v1.size(), v2.size()); i++) {
51 v2[i] = v1[i] * v2[i];
57 for(
size_t i = 0; i < v.size(); i++) {
58 ret[i] = (d == 0.) ? v[i] : v[i] / d;
65 std::pair<Vector, Vector> sample;
66 sample.first.resize(2);
67 sample.second.resize(2);
68 sample.first[0] = Rand::scalar(-1, +1);
69 sample.first[1] = Rand::scalar(-1, +1);
70 sample.second[0] =
sin2d(sample.first[0], sample.first[1]);
71 sample.second[1] =
cross(sample.first[0], sample.first[1]);
96 double trtrtime = 0.0;
97 double trtetime = 0.0;
98 double mctrtime = 0.0;
99 double mctetime = 0.0;
101 if(
argc > 1) sscanf(argv[1],
"%d", &nrf);
108 std::cout <<
"LearningMachine library example (portable)" << std::endl;
111 std::string name(
"RLS");
114 p.put(
"dom", Value(nrf));
115 p.put(
"cod", Value(2));
116 p.put(
"lambda", Value(0.5));
123 p.put(
"dom", Value(2));
124 p.put(
"cod", Value(nrf));
125 p.put(
"gamma", Value(16.0));
127 std::cout <<
"Transformer:" << std::endl << tp.
getWrapped().
getInfo() << std::endl;
141 Vector noisyOutput = sample.second + Rand::vector(noise_min, noise_max);
144 tic = yarp::os::SystemClock::nowSystem();
146 trtrtime += (yarp::os::SystemClock::nowSystem() - tic);
149 tic = yarp::os::SystemClock::nowSystem();
154 mctrtime += (yarp::os::SystemClock::nowSystem() - tic);
158 trainMSE = trainMSE + diff;
161 std::cout <<
"Train MSE: " << trainMSE.toString() << std::endl;
162 std::cout <<
"Train Transformer Time per Sample: " << (trtrtime /
NO_TRAIN) << std::endl;
163 std::cout <<
"Train Machine Time per Sample: " << (mctrtime /
NO_TRAIN) << std::endl;
164 std::cout <<
"Combined Time per Sample: " << ((trtrtime + mctrtime) /
NO_TRAIN) << std::endl;
166 std::cout << std::endl;
167 std::cout <<
"Saving machine portable to file 'mp.txt'...";
169 std::cout << ((
ok) ?
"ok!" :
"failed :(") << std::endl;
171 std::cout <<
"Saving transformer portable to file 'tp.txt'...";
173 std::cout << ((
ok) ?
"ok!" :
"failed :(") << std::endl;
175 std::cout <<
"Loading machine portable from file 'mp.txt'...";
177 std::cout << ((
ok) ?
"ok!" :
"failed :(") << std::endl;
179 std::cout <<
"Loading transformer portable from file 'tp.txt'...";
181 std::cout << ((
ok) ?
"ok!" :
"failed :(") << std::endl;
182 std::cout << std::endl;
186 for(
int i = 0; i <
NO_TEST; i++) {
191 tic = yarp::os::SystemClock::nowSystem();
193 trtetime += (yarp::os::SystemClock::nowSystem() - tic);
196 tic = yarp::os::SystemClock::nowSystem();
198 mctetime += (yarp::os::SystemClock::nowSystem() - tic);
202 testMSE = testMSE + diff;
205 std::cout <<
"Test MSE: " << testMSE.toString() << std::endl;
206 std::cout <<
"Test Transformer Time per Sample: " << (trtetime /
NO_TEST) << std::endl;
207 std::cout <<
"Test Machine Time per Sample: " << (mctetime /
NO_TEST) << std::endl;
208 std::cout <<
"Combined Time per Sample: " << ((trtetime + mctetime) /
NO_TEST) << std::endl;
virtual bool configure(yarp::os::Searchable &config)
Change parameters.
virtual Prediction predict(const yarp::sig::Vector &input)=0
Ask the learning machine to predict the output for a given input.
virtual std::string getInfo()
Asks the learning machine to return a string containing information on its operation so far.
virtual void feedSample(const yarp::sig::Vector &input, const yarp::sig::Vector &output)=0
Provide the learning machine with an example of the desired mapping.
T & getWrapped() const
The accessor for the wrapped object.
bool writeToFile(std::string filename)
Writes a wrapped object to a file.
bool readFromFile(std::string filename)
Reads a wrapped object from a file.
A class that represents a prediction result.
yarp::sig::Vector getPrediction()
Accessor for the expected value of the prediction.
PortableT< IMachineLearner > MachinePortable
A portable wrapper around an IMachineLearner.
int main(int argc, char **argv)
double cross(double x1, double x2)
Vector elementDiv(const Vector &v, double d)
std::pair< Vector, Vector > createSample()
void elementProd(const Vector &v1, Vector &v2)
double sin2d(double x1, double x2)
void registerTransformers()