307 if(opt.check(
"help")) {
313 if(opt.check(
"port", val)) {
314 this->portPrefix = val->asString().c_str();
317 std::cout <<
"* Registering ports...";
318 this->registerAllPorts();
319 std::cout <<
"Done!" << std::endl;
322 if(opt.check(
"trainport", val)) {
323 Network::connect(this->train_out.where().getName().c_str(), val->asString().c_str());
329 if(opt.check(
"predictport", val)) {
330 Network::connect(this->predict_inout.where().getName().c_str(), val->asString().c_str());
336 if(opt.check(
"datafile", val)) {
338 this->dataset.
open(val->asString().c_str());
342 if(opt.check(
"inputs", val)) {
345 Bottle* inputs = val->asList();
346 for(
int i = 0; i < inputs->size(); i++) {
347 if(inputs->get(i).isInt32()) {
351 }
else if(val->isInt32()) {
359 if(opt.check(
"outputs", val)) {
362 Bottle* outputs = val->asList();
363 for(
int i = 0; i < outputs->size(); i++) {
364 if(outputs->get(i).isInt32()) {
368 }
else if(val->isInt32()) {
376 if(opt.check(
"frequency", val)) {
377 this->frequency = val->asInt32();
384 this->attachTerminal();
408 bool success =
false;
411 switch(
cmd.get(0).asVocab32()) {
412 case yarp::os::createVocab32(
'h',
'e',
'l',
'p'):
414 reply.add(Value::makeVocab32(
"help"));
416 reply.addString(
"Testing module configuration options");
417 reply.addString(
" help Displays this message");
418 reply.addString(
" conf Print configuration");
419 reply.addString(
" train [n] Send training samples");
420 reply.addString(
" predict [n] Send testing samples");
421 reply.addString(
" skip [n] Skip samples");
422 reply.addString(
" reset Reset dataset");
423 reply.addString(
" shoot v1 ... vn Shoot a single prediction sample");
424 reply.addString(
" open fname Opens a datafile");
425 reply.addString(
" freq f Sampling frequency in Hertz (0 for disabled)");
427 case yarp::os::createVocab32(
'c',
'o',
'n',
'f'):
434 case yarp::os::createVocab32(
's',
'k',
'i',
'p'):
438 if(
cmd.get(1).isInt32()) {
439 noSamples =
cmd.get(1).asInt32();
442 for(
int i = 0; i < noSamples; i++) {
443 std::pair<Vector,Vector> sample = this->dataset.
getNextSample();
446 reply.addString(
"Done!");
451 case yarp::os::createVocab32(
't',
'r',
'a',
'i'):
453 reply.add(Value::makeVocab32(
"help"));
456 if(
cmd.get(1).isInt32()) {
457 noSamples =
cmd.get(1).asInt32();
461 for(
int i = 0; i < noSamples; i++) {
462 std::pair<Vector,Vector> sample = this->dataset.
getNextSample();
466 if(this->frequency > 0)
467 yarp::os::Time::delay(1. / this->frequency);
473 reply.addString(
"Done!");
478 case yarp::os::createVocab32(
'p',
'r',
'e',
'd'):
480 reply.add(Value::makeVocab32(
"help"));
483 if(
cmd.get(1).isInt32()) {
484 noSamples =
cmd.get(1).asInt32();
494 double start = yarp::os::Time::now();
495 for(
int i = 0; i < noSamples; i++) {
496 std::pair<Vector,Vector> sample = this->dataset.
getNextSample();
502 Vector expected = prediction.head;
503 if(expected.size() != sample.second.size()) {
504 std::string msg(
"incoming prediction has incorrect dimension");
505 throw std::runtime_error(msg);
507 for(
size_t j = 0; j <
error.size(); j++) {
508 double dist = sample.second[j] - expected[j];
509 error[j] += (dist * dist);
512 Vector variance = prediction.body;
513 if(variance.size() > 0 && variance.size() != sample.second.size()) {
514 std::string msg(
"incoming predictive variance has invalid dimension");
515 throw std::runtime_error(msg);
517 for(
size_t j = 0; j < nmlp.size(); j++) {
518 double normdist = (sample.second[j] - expected[j]) / variance[j];
519 nmlp[j] += 0.5 * ((normdist * normdist) + log(
TWOPI) + 2 * log(variance[j]));
523 if(this->frequency > 0)
524 yarp::os::Time::delay(1. / this->frequency);
526 double end = yarp::os::Time::now();
527 std::string reply_str =
"Timing: " +
doubletostring(noSamples / (end - start)) +
" samples per second";
528 reply.addString(reply_str.c_str());
531 for(
size_t i = 0; i <
error.size(); i++) {
534 for(
size_t i = 0; i <
error.size(); i++) {
535 nmlp[i] = nmlp[i] / double(noSamples);
539 reply.addString(reply_str.c_str());
542 reply.addString(reply_str.c_str());
547 case yarp::os::createVocab32(
's',
'h',
'o',
'o'):
551 for(
int i = 1; i <=
cmd.size(); i++) {
552 if(
cmd.get(i).isFloat64() ||
cmd.get(i).isInt32()) {
553 input.push_back(
cmd.get(i).asFloat64());
558 reply.addString(reply_str.c_str());
563 case yarp::os::createVocab32(
'o',
'p',
'e',
'n'):
565 if(
cmd.get(1).isString()) {
566 std::cout <<
"Open..." << std::endl;
567 this->dataset.
open(
cmd.get(1).asString().c_str());
569 reply.addString((std::string(
"Opened dataset: ") +
cmd.get(1).asString().c_str()).c_str());
572 case yarp::os::createVocab32(
'r',
'e',
's',
'e'):
573 case yarp::os::createVocab32(
'r',
's',
't'):
575 this->dataset.
reset();
576 reply.addString(
"Dataset reset to beginning");
579 case yarp::os::createVocab32(
'f',
'r',
'e',
'q'):
581 if(
cmd.size() > 1 &&
cmd.get(1).isInt32()) {
583 this->frequency =
cmd.get(1).asInt32();
584 reply.addString((std::string(
"Current frequency: ") +
inttostring(this->frequency)).c_str());
589 case yarp::os::createVocab32(
's',
'e',
't'):
596 }
catch(
const std::exception&
e) {
598 std::string msg = std::string(
"Error: ") +
e.what();
599 reply.addString(msg.c_str());
602 std::string msg = std::string(
"Error. (something bad happened, but I wouldn't know what!)");
603 reply.addString(msg.c_str());