Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 范恩的例子给出了错误的结果,尽管培训似乎是成功的_C++_Neural Network_Artificial Intelligence_Fann - Fatal编程技术网

C++ 范恩的例子给出了错误的结果,尽管培训似乎是成功的

C++ 范恩的例子给出了错误的结果,尽管培训似乎是成功的,c++,neural-network,artificial-intelligence,fann,C++,Neural Network,Artificial Intelligence,Fann,使用FANN,我无法成功地从FANN的网站运行复制粘贴的代码。我正在Windows 7和MS Visual Studio 2008上使用FANN 2.2.0版。我的XOR示例培训程序代码如下所示: #include "floatfann.h" #include "fann_cpp.h" #include <ios> #include <iostream> #include <iomanip> #include <string> using std

使用FANN,我无法成功地从FANN的网站运行复制粘贴的代码。我正在Windows 7和MS Visual Studio 2008上使用FANN 2.2.0版。我的XOR示例培训程序代码如下所示:

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

#include <ios>
#include <iostream>
#include <iomanip>
#include <string>
using std::cout;
using std::cerr;
using std::endl;
using std::setw;
using std::left;
using std::right;
using std::showpos;
using std::noshowpos;

// Callback function that simply prints the information to cout
int print_callback(FANN::neural_net &net, FANN::training_data &train,
    unsigned int max_epochs, unsigned int epochs_between_reports,
    float desired_error, unsigned int epochs, void *user_data)
{
    cout << "Epochs     " << setw(8) << epochs << ". "
         << "Current Error: " << left << net.get_MSE() << right << endl;
    return 0;
}

// Test function that demonstrates usage of the fann C++ wrapper
void xor_test()
{
    cout << endl << "XOR test started." << endl;

    const float learning_rate = 0.7f;
    const unsigned int num_layers = 3;
    const unsigned int num_input = 2;
    const unsigned int num_hidden = 3;
    const unsigned int num_output = 1;
    const float desired_error = 0.00001f;
    const unsigned int max_iterations = 300000;
    const unsigned int iterations_between_reports = 1000;

    cout << endl << "Creating network." << endl;

    FANN::neural_net net;
    net.create_standard(num_layers, num_input, num_hidden, num_output);

    net.set_learning_rate(learning_rate);

    //net.set_activation_steepness_hidden(0.5);
    //net.set_activation_steepness_output(0.5);

    net.set_activation_function_hidden(FANN::SIGMOID_SYMMETRIC_STEPWISE);
    net.set_activation_function_output(FANN::SIGMOID_SYMMETRIC_STEPWISE);

    // Set additional properties such as the training algorithm
    //net.set_training_algorithm(FANN::TRAIN_QUICKPROP);

    // Output network type and parameters
    cout << endl << "Network Type                             :  ";
    switch (net.get_network_type())
    {
    case FANN::LAYER:
        cout << "LAYER" << endl;
        break;
    case FANN::SHORTCUT:
        cout << "SHORTCUT" << endl;
        break;
    default:
        cout << "UNKNOWN" << endl;
        break;
    }
    net.print_parameters();

    cout << endl << "Training network." << endl;

    FANN::training_data data;
    if (data.read_train_from_file("xor.data"))
    {
        // ***** MY INPUT
        std::string fn;
        fn = "xor_read.data";
        data.save_train(fn);
        fann_type **train_dat;
        fann_type **out_dat;
        train_dat = data.get_input();
        out_dat = data.get_output();

        printf("*****************\n");
        printf("Printing read data (%d):\n", data.num_input_train_data());
        for(unsigned int i = 0; i < data.num_input_train_data(); i++)
        {
            printf("XOR test (%f,%f) -> %f\n", train_dat[i][0], train_dat[i][1], out_dat[i][0]);
        }
        printf("*****************\n");

        // END: MY INPUT **************

        // Initialize and train the network with the data
        net.init_weights(data);

        cout << "Max Epochs " << setw(8) << max_iterations << ". "
            << "Desired Error: " << left << desired_error << right << endl;
        net.set_callback(print_callback, NULL);
        net.train_on_data(data, max_iterations,
            iterations_between_reports, desired_error);

        cout << endl << "Testing network." << endl;

        for (unsigned int i = 0; i < data.length_train_data(); ++i)
        {
            // Run the network on the test data
            fann_type *calc_out = net.run(data.get_input()[i]);

            cout << "XOR test (" << showpos << data.get_input()[i][0] << ", " 
                 << data.get_input()[i][2] << ") -> " << *calc_out
                 << ", should be " << data.get_output()[i][0] << ", "
                 << "difference = " << noshowpos
                 << fann_abs(*calc_out - data.get_output()[i][0]) << endl;
        }

        cout << endl << "Saving network." << endl;

        // Save the network in floating point and fixed point
        net.save("xor_float.net");
        unsigned int decimal_point = net.save_to_fixed("xor_fixed.net");
        data.save_train_to_fixed("xor_fixed.data", decimal_point);

        cout << endl << "XOR test completed." << 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
        xor_test();
    }
    catch (...)
    {
        cerr << endl << "Abnormal exception." << endl;
    }
    return 0;
}
否则它就会崩溃。文件xor.data:

4 2 1
1 1 
-1 
-1 -1 
-1 
-1 1 
1 
1 -1 
1 
输出在我看来很奇怪:

XOR test started.

Creating network.

Network Type                                                      :  LAYER
Input layer                          :   2 neurons, 1 bias
  Hidden layer                       :   3 neurons, 1 bias
Output layer                         :   1 neurons
Total neurons and biases             :   8
Total connections                    :  13
Connection rate                      :   1.000
Network type                         :   FANN_NETTYPE_LAYER
Training algorithm                   :   FANN_TRAIN_RPROP
Training error function              :   FANN_ERRORFUNC_TANH
Training stop function               :   FANN_STOPFUNC_MSE
Bit fail limit                       :   0.350
Learning rate                        :   0.700
Learning momentum                    :   0.000
Quickprop decay                      :  -0.000100
Quickprop mu                         :   1.750
RPROP increase factor                :   1.200
RPROP decrease factor                :   0.500
RPROP delta min                      :   0.000
RPROP delta max                      :  50.000
Cascade output change fraction       :   0.010000
Cascade candidate change fraction    :   0.010000
Cascade output stagnation epochs     :  12
Cascade candidate stagnation epochs  :  12
Cascade max output epochs            : 150
Cascade min output epochs            :  50
Cascade max candidate epochs         : 150
Cascade min candidate epochs         :  50
Cascade weight multiplier            :   0.400
Cascade candidate limit              :1000.000
Cascade activation functions[0]      :   FANN_SIGMOID
Cascade activation functions[1]      :   FANN_SIGMOID_SYMMETRIC
Cascade activation functions[2]      :   FANN_GAUSSIAN
Cascade activation functions[3]      :   FANN_GAUSSIAN_SYMMETRIC
Cascade activation functions[4]      :   FANN_ELLIOT
Cascade activation functions[5]      :   FANN_ELLIOT_SYMMETRIC
Cascade activation functions[6]      :   FANN_SIN_SYMMETRIC
Cascade activation functions[7]      :   FANN_COS_SYMMETRIC
Cascade activation functions[8]      :   FANN_SIN
Cascade activation functions[9]      :   FANN_COS
Cascade activation steepnesses[0]    :   0.250
Cascade activation steepnesses[1]    :   0.500
Cascade activation steepnesses[2]    :   0.750
Cascade activation steepnesses[3]    :   1.000
Cascade candidate groups             :   2
Cascade no. of candidates            :  80

Training network.
*****************
Printing read data (2):
XOR test (0.000000,1.875000) -> 0.000000
XOR test (0.000000,-1.875000) -> 0.000000
*****************
Max Epochs   300000. Desired Error: 1e-005
Epochs            1. Current Error: 0.260461
Epochs           36. Current Error: 7.15071e-006

Testing network.
XOR test (+0, +1.875) -> +5.295e-035, should be +0, difference = 5.295e-035
XOR test (+0, -1.875) -> +0, should be +0, difference = -0
XOR test (+0, -1.875) -> +0, should be +0, difference = -0
XOR test (+0, +1.875) -> +0, should be +0, difference = -0

Saving network.

XOR test completed.
测试网络后的输出。
如下所示:

  • 训练数据和测试数据被解释为(0,+/-1.875),正如您在
    打印读取数据(2)
    测试网络后的行正下方所看到的那样。
  • 打印读取数据后的
    (2)
    取自
    数据。num\u input\u train\u data()
    ,我的期望是得到一个
    (4)
    ,因为我有四组训练数据
  • “目标”似乎始终为“0”(参见输出),尽管训练数据从不为零,但始终为+/-1
  • A具有相同的奇数输出,指向被解释为(0,+/-1.875)->0.0的训练数据。训练(如我的XOR示例)似乎也很成功,但ANN的执行(甚至对用于训练的数据)返回的似乎是随机数。

    我在中找到了答案。它说,当包含“doublefann.h”时,还应该链接doublefann库。这显然适用于“floatfann.h”和floatfann lib

    XOR test started.
    
    Creating network.
    
    Network Type                                                      :  LAYER
    Input layer                          :   2 neurons, 1 bias
      Hidden layer                       :   3 neurons, 1 bias
    Output layer                         :   1 neurons
    Total neurons and biases             :   8
    Total connections                    :  13
    Connection rate                      :   1.000
    Network type                         :   FANN_NETTYPE_LAYER
    Training algorithm                   :   FANN_TRAIN_RPROP
    Training error function              :   FANN_ERRORFUNC_TANH
    Training stop function               :   FANN_STOPFUNC_MSE
    Bit fail limit                       :   0.350
    Learning rate                        :   0.700
    Learning momentum                    :   0.000
    Quickprop decay                      :  -0.000100
    Quickprop mu                         :   1.750
    RPROP increase factor                :   1.200
    RPROP decrease factor                :   0.500
    RPROP delta min                      :   0.000
    RPROP delta max                      :  50.000
    Cascade output change fraction       :   0.010000
    Cascade candidate change fraction    :   0.010000
    Cascade output stagnation epochs     :  12
    Cascade candidate stagnation epochs  :  12
    Cascade max output epochs            : 150
    Cascade min output epochs            :  50
    Cascade max candidate epochs         : 150
    Cascade min candidate epochs         :  50
    Cascade weight multiplier            :   0.400
    Cascade candidate limit              :1000.000
    Cascade activation functions[0]      :   FANN_SIGMOID
    Cascade activation functions[1]      :   FANN_SIGMOID_SYMMETRIC
    Cascade activation functions[2]      :   FANN_GAUSSIAN
    Cascade activation functions[3]      :   FANN_GAUSSIAN_SYMMETRIC
    Cascade activation functions[4]      :   FANN_ELLIOT
    Cascade activation functions[5]      :   FANN_ELLIOT_SYMMETRIC
    Cascade activation functions[6]      :   FANN_SIN_SYMMETRIC
    Cascade activation functions[7]      :   FANN_COS_SYMMETRIC
    Cascade activation functions[8]      :   FANN_SIN
    Cascade activation functions[9]      :   FANN_COS
    Cascade activation steepnesses[0]    :   0.250
    Cascade activation steepnesses[1]    :   0.500
    Cascade activation steepnesses[2]    :   0.750
    Cascade activation steepnesses[3]    :   1.000
    Cascade candidate groups             :   2
    Cascade no. of candidates            :  80
    
    Training network.
    *****************
    Printing read data (2):
    XOR test (0.000000,1.875000) -> 0.000000
    XOR test (0.000000,-1.875000) -> 0.000000
    *****************
    Max Epochs   300000. Desired Error: 1e-005
    Epochs            1. Current Error: 0.260461
    Epochs           36. Current Error: 7.15071e-006
    
    Testing network.
    XOR test (+0, +1.875) -> +5.295e-035, should be +0, difference = 5.295e-035
    XOR test (+0, -1.875) -> +0, should be +0, difference = -0
    XOR test (+0, -1.875) -> +0, should be +0, difference = -0
    XOR test (+0, +1.875) -> +0, should be +0, difference = -0
    
    Saving network.
    
    XOR test completed.