FANN:将输出作为输入返回到run()(C ++)

时间:2013-10-22 01:42:55

标签: c++ fann

我正在使用FANN的C ++包装器,并且已经训练了时间序列输入的预测器。现在我想查看将网络输出作为输入反馈的结果。

我最初尝试过这个:

fann_type *previousOutput = net.run(previousOutput);

结果是:

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

我也尝试过:

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

导致相同的错误。

那么这样做的正确方法是什么?似乎问题是run()返回一个指针,而不是实际的数据。

另外,因为我的输入在我的训练数据中是离散的(0和1缩放到-1和1),我可能需要在反馈之前离散网络输出。这将涉及循环网络输出并构造一个新的离散化值的fann_type数组,但由于run()想要一个指针,我不知道如何做到这一点。

感谢。

EDIT1(完整代码列表)

#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;
}

1 个答案:

答案 0 :(得分:0)

如果我单独处理数组元素,那么它按照我的预期工作:

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.