Question

I'm using the C++ wrapper for FANN and have trained a predictor from time-series input. Now I want see what sequence results from feeding the network output back as an input.

I tried this initially:

fann_type *previousOutput = net.run(previousOutput);

Which results in:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7bc51fb in fann_run () from /usr/local/lib/libfann.so.2
(gdb) bt
#0  0x00007ffff7bc51fb in fann_run () from /usr/local/lib/libfann.so.2
#1  0x000000000040161f in FANN::neural_net::run (this=0x7fffffffe420, 
    input=0x7fffffffe5b8) at /usr/local/include/fann_cpp.h:1107
#2  0x000000000040118d in run () at generate_dream.cpp:34
#3  0x000000000040133b in main (argc=1, argv=0x7fffffffe5a8)
    at generate_dream.cpp:55

I also tried:

fann_type *tmpOutput = net.run(previousOutput);
previousOutput = *tmpOutput; // feedback loop.

Which results in the same error.

So what is the proper way to do this? Seems the issue is that run() returns a pointer, not the actual data.

Also, because my inputs are discrete in my training data (0 and 1 scaled to -1 and 1), I may need to discretize the network output before feeding back. This would involve looping through the network output and constructing a new fann_type array of discretized values, but since run() wants a pointer, I'm not sure how to do this.

Thanks.

EDIT1 (Full code listing)

#include "floatfann.h"
#include "fann_cpp.h"

#include <ios>
#include <iostream>
#include <fstream>
#include <sstream>
#include <sys/time.h>

using namespace std;

// Test function that demonstrates usage of the fann C++ wrapper
void run()
{
    // Load file previously trained (for example by learn_sequence)
    FANN::neural_net net;
    net.create_from_file("learn_sequential.net");

    // load datafile 
    FANN::training_data data;
    if (data.read_train_from_file("../data/backgroundState_FANN.data")) {
        data.scale_train_data(-1, 1);

        // Seed with last pattern from dataset.
        fann_type *previousOutput = data.get_input()[data.length_train_data()];

        // Length of dream
        for (unsigned int i = 0; i < 10000; i++)
        {
            fann_type *tmpOutput;
            tmpOutput = net.run(previousOutput);
            previousOutput = tmpOutput; // feedback loop.

            // print out each 
            for (unsigned int j = 0; j < data.num_input_train_data(); j++) {
                cout << "RESULT " << i << " " << j << " " << previousOutput[j] <<endl;
            }
        }
    } else
        cout << "Data file could not be loaded" << endl;

}

/* Startup function. Syncronizes C and C++ output, calls the test function
   and reports any exceptions */
int main(int argc, char **argv)
{
    try
    {
        std::ios::sync_with_stdio(); // Syncronize cout and printf output
        run();
    }
    catch (...)
    {
        cerr << endl << "Abnormal exception." << endl;
    }
    return 0;
}
Was it helpful?

Solution

If I treat the array elements separately then it works as I intended:

fann_type *tmpOutput = net.run(previousOutput);

// copy element by element from output to new array.
fann_type newInput[data.num_input_train_data()];
for (unsigned int j = 0; j < data.num_input_train_data(); j++) {
    newInput[j] = tmpOutput[j];
}
previousOutput = newInput; // feedback loop.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top