FANN xor_train - eiichiromomma/CVMLAB GitHub Wiki

(FANN) xor_train

XORの学習のサンプル

ソース

    #include <stdio.h>
    
    #include "fann.h"
    //コールバック関数のテスト
    int FANN_API test_callback(struct fann *ann, struct fann_train_data *train,
    			   unsigned int max_epochs, unsigned int epochs_between_reports, 
    			   float desired_error, unsigned int epochs)
    {
      printf("Epochs     %8d. MSE: %.5f. Desired-MSE: %.5f\n", epochs, fann_get_MSE(ann), desired_error);
      return 0;
    }
    
    int main()
    {
      //fann_typeは通常float。fanndouble.hを使うとdoubleになる。
      fann_type *calc_out;
      //ネットワークの構成パラメータ
      const unsigned int num_input = 2;
      const unsigned int num_output = 1;
      const unsigned int num_layers = 3;
      const unsigned int num_neurons_hidden = 3;
      //学習パラメータ
      const float desired_error = (const float) 0;
      const unsigned int max_epochs = 1000;
      const unsigned int epochs_between_reports = 10;
      //ネットワーク構造体のポインタを作成
      struct fann *ann;
      //学習データのポインタを作成
      struct fann_train_data *data;
    
      unsigned int i = 0;
      unsigned int decimal_point;
    
      printf("Creating network.\n");
      //ネットワークの作成
      ann = fann_create_standard(num_layers, num_input, num_neurons_hidden, num_output);
      //学習データの読み込み
      data = fann_read_train_from_file("xor.data");
      
      //傾きを1とするシグモイド関数(thresholdになる)
      fann_set_activation_steepness_hidden(ann, 1);
      fann_set_activation_steepness_output(ann, 1);
      //Symmetricなシグモイド関数(データの範囲は(-1,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_init_weights(ann, data);
    	
      printf("Training network.\n");
      //学習の開始
      fann_train_on_data(ann, data, max_epochs, epochs_between_reports, desired_error);
      //学習データを使ったテスト
      printf("Testing network. %f\n", fann_test_data(ann, data));
    
      //テスト結果の表示
      for(i = 0; i < fann_length_train_data(data); i++)
        {
          calc_out = fann_run(ann, data->input[i]);
          printf("XOR test (%f,%f) -> %f, should be %f, difference=%f\n",
    	     data->input[i][0], data->input[i][1], calc_out[0], data->output[i][0],
    	     fann_abs(calc_out[0] - data->output[i][0]));
        }
    
      printf("Saving network.\n");
      //重みなどの保存
      fann_save(ann, "xor_float.net");
      //fixedの学習データを保存
      decimal_point = fann_save_to_fixed(ann, "xor_fixed.net");
      fann_save_train_to_fixed(data, "xor_fixed.data", decimal_point);
    
      //後片付け
      printf("Cleaning up.\n");
      fann_destroy_train(data);
      fann_destroy(ann);
      
      return 0;
    }

実行結果

> ./xor_train
Creating network.
Training network.
Max epochs     1000. Desired error: 0.0000000000.
Epochs            1. Current error: 0.2710060775. Bit fail 4.
Epochs           10. Current error: 0.2684144974. Bit fail 4.
Epochs           20. Current error: 0.2478404641. Bit fail 4.
Epochs           30. Current error: 0.1754541397. Bit fail 4.
Epochs           40. Current error: 0.1287820041. Bit fail 4.
Epochs           50. Current error: 0.0897984356. Bit fail 4.
Epochs           60. Current error: 0.0565589033. Bit fail 4.
Epochs           70. Current error: 0.0363917761. Bit fail 4.
Epochs           80. Current error: 0.0257470459. Bit fail 4.
Epochs           90. Current error: 0.0164618175. Bit fail 4.
Epochs          100. Current error: 0.0116132954. Bit fail 4.
Epochs          110. Current error: 0.0067465315. Bit fail 4.
Epochs          120. Current error: 0.0047443812. Bit fail 4.
Epochs          130. Current error: 0.0029264868. Bit fail 4.
Epochs          140. Current error: 0.0018880134. Bit fail 4.
Epochs          150. Current error: 0.0012575694. Bit fail 4.
Epochs          160. Current error: 0.0007982652. Bit fail 4.
Epochs          170. Current error: 0.0005533139. Bit fail 4.
Epochs          180. Current error: 0.0003470994. Bit fail 4.
Epochs          190. Current error: 0.0002377222. Bit fail 4.
Epochs          200. Current error: 0.0001516260. Bit fail 2.
Epochs          210. Current error: 0.0000959772. Bit fail 1.
Epochs          220. Current error: 0.0000645029. Bit fail 1.
Epochs          227. Current error: 0.0000425604. Bit fail 0.
Testing network. 0.000038
XOR test (-1.000000,-1.000000) -> -0.982257, should be -1.000000, difference=0.017743
XOR test (-1.000000,1.000000) -> 0.990769, should be 1.000000, difference=0.009231
XOR test (1.000000,-1.000000) -> 0.991142, should be 1.000000, difference=0.008858
XOR test (1.000000,1.000000) -> -0.988301, should be -1.000000, difference=0.011699
Saving network.
Cleaning up.

たまたま上手く収束した。大抵1000回学習してギブアップする。

⚠️ **GitHub.com Fallback** ⚠️