这是我从 FANN 网站获取的一个小修改示例程序。
我创建的等式是 c = pow(a,2) + b。
火车.c
#include "fann.h"
int main()
{
const unsigned int num_input = 2;
const unsigned int num_output = 1;
const unsigned int num_layers = 4;
const unsigned int num_neurons_hidden = 3;
const float desired_error = (const float) 0.001;
const unsigned int max_epochs = 500000;
const unsigned int epochs_between_reports = 1000;
struct fann *ann = fann_create_standard(num_layers, num_input,
num_neurons_hidden, num_output);
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC);
fann_train_on_file(ann, "sample.data", max_epochs,
epochs_between_reports, desired_error);
fann_save(ann, "sample.net");
fann_destroy(ann);
return 0;
}
结果.c
#include <stdio.h>
#include "floatfann.h"
int main()
{
fann_type *calc_out;
fann_type input[2];
struct fann *ann = fann_create_from_file("sample.net");
input[0] = 1;
input[1] = 1;
calc_out = fann_run(ann, input);
printf("sample test (%f,%f) -> %f\n", input[0], input[1], calc_out[0]);
fann_destroy(ann);
return 0;
}
我创建了自己的数据集
数据集.rb
f= File.open("sample.data","w")
f.write("100 2 1\n")
i=0
while i<100 do
first = rand(0..100)
second = rand(0..100)
third = first ** 2 + second
string1 = "#{first} #{second}\n"
string2 = "#{third}\n"
f.write(string1)
f.write(string2)
i=i+1
end
f.close
样本数据
100 2 1
95 27
9052
63 9
3978
38 53
1497
31 84
1045
28 56
840
95 80
9105
10 19
...
...
样本数据第一行给出样本数、输入数和最后输出数。
但我收到一个错误
FANN Error 20: The number of output neurons in the ann (4196752) and data (1) don't match Epochs
这里有什么问题?它是如何计算4196752
神经元的?