305 if(opt.check(
"help")) {
311 if(opt.check(
"port", val)) {
312 this->portPrefix = val->asString().c_str();
315 std::cout <<
"* Registering ports...";
316 this->registerAllPorts();
317 std::cout <<
"Done!" << std::endl;
320 if(opt.check(
"trainport", val)) {
321 Network::connect(this->train_out.where().getName().c_str(), val->asString().c_str());
327 if(opt.check(
"predictport", val)) {
328 Network::connect(this->predict_inout.where().getName().c_str(), val->asString().c_str());
334 if(opt.check(
"datafile", val)) {
336 this->dataset.
open(val->asString().c_str());
340 if(opt.check(
"inputs", val)) {
343 Bottle* inputs = val->asList();
344 for(
int i = 0; i < inputs->size(); i++) {
345 if(inputs->get(i).isInt32()) {
349 }
else if(val->isInt32()) {
357 if(opt.check(
"outputs", val)) {
360 Bottle* outputs = val->asList();
361 for(
int i = 0; i < outputs->size(); i++) {
362 if(outputs->get(i).isInt32()) {
366 }
else if(val->isInt32()) {
374 if(opt.check(
"frequency", val)) {
375 this->frequency = val->asInt32();
382 this->attachTerminal();
406 bool success =
false;
409 switch(
cmd.get(0).asVocab32()) {
410 case yarp::os::createVocab32(
'h',
'e',
'l',
'p'):
412 reply.add(Value::makeVocab32(
"help"));
414 reply.addString(
"Testing module configuration options");
415 reply.addString(
" help Displays this message");
416 reply.addString(
" conf Print configuration");
417 reply.addString(
" train [n] Send training samples");
418 reply.addString(
" predict [n] Send testing samples");
419 reply.addString(
" skip [n] Skip samples");
420 reply.addString(
" reset Reset dataset");
421 reply.addString(
" shoot v1 ... vn Shoot a single prediction sample");
422 reply.addString(
" open fname Opens a datafile");
423 reply.addString(
" freq f Sampling frequency in Hertz (0 for disabled)");
425 case yarp::os::createVocab32(
'c',
'o',
'n',
'f'):
432 case yarp::os::createVocab32(
's',
'k',
'i',
'p'):
436 if(
cmd.get(1).isInt32()) {
437 noSamples =
cmd.get(1).asInt32();
440 for(
int i = 0; i < noSamples; i++) {
441 std::pair<Vector,Vector> sample = this->dataset.
getNextSample();
444 reply.addString(
"Done!");
449 case yarp::os::createVocab32(
't',
'r',
'a',
'i'):
451 reply.add(Value::makeVocab32(
"help"));
454 if(
cmd.get(1).isInt32()) {
455 noSamples =
cmd.get(1).asInt32();
459 for(
int i = 0; i < noSamples; i++) {
460 std::pair<Vector,Vector> sample = this->dataset.
getNextSample();
464 if(this->frequency > 0)
465 yarp::os::Time::delay(1. / this->frequency);
471 reply.addString(
"Done!");
476 case yarp::os::createVocab32(
'p',
'r',
'e',
'd'):
478 reply.add(Value::makeVocab32(
"help"));
481 if(
cmd.get(1).isInt32()) {
482 noSamples =
cmd.get(1).asInt32();
492 double start = yarp::os::Time::now();
493 for(
int i = 0; i < noSamples; i++) {
494 std::pair<Vector,Vector> sample = this->dataset.
getNextSample();
500 Vector expected = prediction.
head;
501 if(expected.size() != sample.second.size()) {
502 std::string msg(
"incoming prediction has incorrect dimension");
503 throw std::runtime_error(msg);
505 for(
size_t j = 0; j <
error.size(); j++) {
506 double dist = sample.second[j] - expected[j];
507 error[j] += (dist * dist);
510 Vector variance = prediction.
body;
511 if(variance.size() > 0 && variance.size() != sample.second.size()) {
512 std::string msg(
"incoming predictive variance has invalid dimension");
513 throw std::runtime_error(msg);
515 for(
size_t j = 0; j < nmlp.size(); j++) {
516 double normdist = (sample.second[j] - expected[j]) / variance[j];
517 nmlp[j] += 0.5 * ((normdist * normdist) + log(
TWOPI) + 2 * log(variance[j]));
521 if(this->frequency > 0)
522 yarp::os::Time::delay(1. / this->frequency);
524 double end = yarp::os::Time::now();
525 std::string reply_str =
"Timing: " +
doubletostring(noSamples / (end - start)) +
" samples per second";
526 reply.addString(reply_str.c_str());
529 for(
size_t i = 0; i <
error.size(); i++) {
532 for(
size_t i = 0; i <
error.size(); i++) {
533 nmlp[i] = nmlp[i] / double(noSamples);
537 reply.addString(reply_str.c_str());
540 reply.addString(reply_str.c_str());
545 case yarp::os::createVocab32(
's',
'h',
'o',
'o'):
549 for(
int i = 1; i <=
cmd.size(); i++) {
550 if(
cmd.get(i).isFloat64() ||
cmd.get(i).isInt32()) {
551 input.push_back(
cmd.get(i).asFloat64());
556 reply.addString(reply_str.c_str());
561 case yarp::os::createVocab32(
'o',
'p',
'e',
'n'):
563 if(
cmd.get(1).isString()) {
564 std::cout <<
"Open..." << std::endl;
565 this->dataset.
open(
cmd.get(1).asString().c_str());
567 reply.addString((std::string(
"Opened dataset: ") +
cmd.get(1).asString().c_str()).c_str());
570 case yarp::os::createVocab32(
'r',
'e',
's',
'e'):
571 case yarp::os::createVocab32(
'r',
's',
't'):
573 this->dataset.
reset();
574 reply.addString(
"Dataset reset to beginning");
577 case yarp::os::createVocab32(
'f',
'r',
'e',
'q'):
579 if(
cmd.size() > 1 &&
cmd.get(1).isInt32()) {
581 this->frequency =
cmd.get(1).asInt32();
582 reply.addString((std::string(
"Current frequency: ") +
inttostring(this->frequency)).c_str());
587 case yarp::os::createVocab32(
's',
'e',
't'):
594 }
catch(
const std::exception&
e) {
596 std::string msg = std::string(
"Error: ") +
e.what();
597 reply.addString(msg.c_str());
600 std::string msg = std::string(
"Error. (something bad happened, but I wouldn't know what!)");
601 reply.addString(msg.c_str());