我正在为 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;
}