我完全没有神经网络的经验,现在我只是在玩FANN库来学习它们。因此,目标是训练网络以近似正弦函数。为此,我使用3层NN 1输入,3个隐藏和1个输出神经元。代码是
const unsigned int num_input = 1;
const unsigned int num_output = 1;
const unsigned int num_layers = 3;
const unsigned int num_neurons_hidden = 3;
struct fann *ann;
ann = fann_create_standard(num_layers, num_input, num_neurons_hidden, num_output);
fann_set_activation_steepness_hidden(ann, 1);
fann_set_activation_steepness_output(ann, 1);
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_train_stop_function(ann, FANN_STOPFUNC_BIT);
fann_set_bit_fail_limit(ann, 0.01f);
fann_set_training_algorithm(ann, FANN_TRAIN_RPROP);
fann_randomize_weights(ann, 0, 1);
for(int i=0; i<2; ++i) {
for(float angle=0; angle<10; angle+=0.1) {
float sin_anle = sinf(angle);
fann_train(ann, &angle, &sin_anle);
}
}
int k = 0;
for(float angle=0; angle<10; angle+=0.1) {
float sin_anle = sinf(angle);
float *o = fann_run(ann, &angle);
printf("%d\t%f\t%f\t\n", k++, *o, sin_anle);
}
fann_destroy(ann);
但是我得到的结果与真正的正弦函数无关。我想我的网络设计存在一些根本性的错误。
答案 0 :(得分:6)
您可以在此行中选择优化算法Resilient Backpropagation(Rprop):
fann_set_training_algorithm(ann, FANN_TRAIN_RPROP);
Rprop是批量更新算法。这意味着您必须为每次更新提供整个训练集。 fann_train的文档说
此培训始终是增量培训(请参阅fann_train_enum),因为只提供了一种模式。
因此,相应的优化选项为FANN_TRAIN_INCREMENTAL
。您必须使用其中一种方法进行批量学习:fann_train_on_data
,fann_train_on_file
或fann_train_epoch
。
我在更改代码时注意到的是:
0.02f
。我得到的解决方案并不完美,但至少大致正确:
0 0.060097 0.000000
1 0.119042 0.099833
2 0.188885 0.198669
3 0.269719 0.295520
4 0.360318 0.389418
5 0.457665 0.479426
6 0.556852 0.564642
7 0.651718 0.644218
8 0.736260 0.717356
9 0.806266 0.783327
10 0.860266 0.841471
11 0.899340 0.891207
12 0.926082 0.932039
...
我使用了这个修改过的代码:
#include <cstdio>
#include <cmath>
#include <fann.h>
#include <floatfann.h>
int main()
{
const unsigned int num_input = 1;
const unsigned int num_output = 1;
const unsigned int num_layers = 3;
const unsigned int num_neurons_hidden = 2;
const float angleRange = 3.0f;
const float angleStep = 0.1;
int instances = (int)(angleRange/angleStep);
struct fann *ann;
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_set_train_stop_function(ann, FANN_STOPFUNC_BIT);
fann_set_bit_fail_limit(ann, 0.02f);
fann_set_training_algorithm(ann, FANN_TRAIN_INCREMENTAL);
fann_randomize_weights(ann, 0, 1);
fann_train_data *trainingSet;
trainingSet = fann_create_train(instances, 1, 1); // instances, input dimension, output dimension
float angle=0;
for(int instance=0; instance < instances; angle+=angleStep, instance++) {
trainingSet->input[instance][0] = angle;
trainingSet->output[instance][0] = sinf(angle);
}
fann_train_on_data(ann, trainingSet, 20000, 10, 1e-8f); // epochs, epochs between reports, desired error
int k = 0;
angle=0;
for(int instance=0; instance < instances; angle+=angleStep, instance++) {
float sin_angle = sinf(angle);
float *o = fann_run(ann, &angle);
printf("%d\t%f\t%f\t\n", k++, *o, sin_angle);
}
fann_destroy(ann);
return 0;
}
请注意,自FANN 2.2.0起,fann_create_train
可用。