FANN cascade_train - eiichiromomma/CVMLAB GitHub Wiki

(FANN) cascade_train

カスケード学習の例

ソース

    //カスケード学習の例
    #include <stdio.h>
    #include "fann.h"
    
    int main()
    {
      //ネットワークのポインタ
      struct fann *ann;
      //学習とテストデータのポインタ
      struct fann_train_data *train_data, *test_data;
      //終了条件
      const float desired_error = (const float) 0.001;
      //Cascade2で増加させるニューロンの最大数
      unsigned int max_neurons = 40;
      //1ニューロン増加させるごとにレポート
      unsigned int neurons_between_reports = 1;
      
      printf("Reading data.\n");
      //学習とテストデータの読み込み
      //(fann-2.0のソースファイルに含まれるので必要に応じてパスを書き換える)
      train_data = fann_read_train_from_file("../benchmarks/datasets/two-spiral.train");
      test_data = fann_read_train_from_file("../benchmarks/datasets/two-spiral.test");
      //学習とテストデータの正規化
      fann_scale_train_data(train_data, 0, 1);
      fann_scale_train_data(test_data, 0, 1);
    
      printf("Creating network.\n");
    
      //ショートカット接続のネットワークを作成
      ann = fann_create_shortcut(2, fann_num_input_train_data(train_data), fann_num_output_train_data(train_data));
      
      //学習アルゴリズムの設定
      fann_set_training_algorithm(ann, FANN_TRAIN_RPROP);
      //関数の設定
      fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
      fann_set_activation_function_output(ann, FANN_LINEAR_PIECE);
      fann_set_train_error_function(ann, FANN_ERRORFUNC_LINEAR);
      //ネットワークパラメータの表示
      fann_print_parameters(ann);
    
      printf("Training network.\n");
      //カスケード学習の開始
      fann_cascadetrain_on_data(ann, train_data, max_neurons, neurons_between_reports, desired_error);
      //ネットワーク接続状態の表示
      fann_print_connections(ann);
      //テスト結果の表示
      printf("\nTrain error: %f, Test error: %f\n\n", fann_test_data(ann, train_data),
    	 fann_test_data(ann, test_data));
      
      printf("Saving network.\n");
      //ネットワークの保存
      fann_save(ann, "two_spirali.net");
      //後片付け
      printf("Cleaning up.\n");
      fann_destroy_train(train_data);
      fann_destroy_train(test_data);
      fann_destroy(ann);
    
      return 0;
    }

学習データ

192 2 1
0.296027 0.703973 
0 
0.385146 0.671890 
1 
0.401172 0.352095 
0 

みたいな192個の入力2出力1のデータ

実行結果(Cascade2をやる前のfann_print_parameters)

Input layer                          :   2 neurons, 1 bias
Output layer                         :   1 neurons
Total neurons and biases             :   4
Total connections                    :   3
Connection rate                      :   1.000
Shortcut connections                 :   1
Training algorithm                   :   FANN_TRAIN_RPROP
Training error function              :   FANN_ERRORFUNC_LINEAR
Training stop function               :   FANN_STOPFUNC_MSE
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 max candidate epochs         : 150
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 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            :  48

Cascade2をやる前に表示したので中間層が無いのに注目

実行結果(fann_print_connections)

Layer / Neuron 012345678901234567890123456789012
L   1 / N    3 ZeD..............................
L   2 / N    4 jBAi.............................
L   3 / N    5 ezJZZ............................
L   4 / N    6 ddbeAZ...........................
L   5 / N    7 ddAzZhh..........................
L   6 / N    8 fdAzKysd.........................
L   7 / N    9 AAaABAAAB........................
L   8 / N   10 bccFMfIZeZ.......................
L   9 / N   11 caAaEBaCCbC......................
L  10 / N   12 CcaZzBlbzzLo.....................
L  11 / N   13 aABcCCbbglbBA....................
L  12 / N   14 albcTjcazBleBB...................
L  13 / N   15 BBBBZZRKbcACaEg..................
L  14 / N   16 BbACEAbMqKaAabFC.................
L  15 / N   17 BbaBcABdSfbbbAnCc................
L  16 / N   18 afbzzczQzkAbCBzCBB...............
L  17 / N   19 aAaacbABBfbAAAgbbbC..............
L  18 / N   20 AaabHbaEmaaaBbedCaHc.............
L  19 / N   21 abARZbEOGBBDbBzBBCNaC............
L  20 / N   22 afaCzczCUfjAaAzzBbaaac...........
L  21 / N   23 AABJECbAGZMaBJZGAACbANJ..........
L  22 / N   24 BdazzezzzMbaACpzaafbazCE.........
L  23 / N   25 baazbzzzbbzaAbzDAczEazzdn........
L  24 / N   26 cbazhdzcdlbAaazCACdBbzbcqD.......
L  25 / N   27 cbazhAzzaBkbBCzzaBzaBzzBazL......
L  26 / N   28 BBBCdCzLFZCbCDWcaBIbcBzBSbZA.....
L  27 / N   29 acabzzzZOdzCabzzCBGCabzzzeDDz....
L  28 / N   30 cBAqgzamazzABazEbAzbamBafuzzBz...
L  29 / N   31 AaAZZOECdAcAaAxCbCEfBhDbCbcgCCZ..
L  30 / N   32 AfaIzbzfAmyaABdFaAybCbzCwMFckzza.
L  31 / N   33 BcbzFmZzDZeceBZHcBOdbMuDPPOkGeZcI

Train error: 0.000021, Test error: 0.113035

イメージが湧かないが要するに全部が並列に繋がっているということだろう。

実行結果(Cascade2後のfann_print_parameters)

Saving network.
Input layer                          :   2 neurons, 1 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
  Hidden layer                       :   1 neurons, 0 bias
Output layer                         :   1 neurons
Total neurons and biases             :  34
Total connections                    : 558
Connection rate                      :   1.000
Shortcut connections                 :   1
Training algorithm                   :   FANN_TRAIN_RPROP
Training error function              :   FANN_ERRORFUNC_LINEAR
Training stop function               :   FANN_STOPFUNC_MSE
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 max candidate epochs         : 150
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 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            :  48

Cascadeなので1のバイアス無しが続く。

実行結果

Training network.
Max neurons  40. Desired error: 0.001000
Neurons       1. Current error: 0.247635. Total error: 47.5459. Epochs    56. Bit fail 192
Neurons       2. Current error: 0.242957. Total error: 46.6477. Epochs   320. Bit fail 186. candidate steepness 0.50. function FANN_GAUSSIAN
Neurons       3. Current error: 0.240952. Total error: 46.2627. Epochs   574. Bit fail 186. candidate steepness 1.00. function FANN_SIGMOID
Neurons       4. Current error: 0.232480. Total error: 44.6362. Epochs   775. Bit fail 176. candidate steepness 1.00. function FANN_ELLIOT
Neurons       5. Current error: 0.221806. Total error: 42.5867. Epochs   961. Bit fail 171. candidate steepness 1.00. function FANN_GAUSSIAN
Neurons       6. Current error: 0.213415. Total error: 40.9758. Epochs  1204. Bit fail 161. candidate steepness 1.00. function FANN_SIGMOID
Neurons       7. Current error: 0.205998. Total error: 39.5516. Epochs  1504. Bit fail 153. candidate steepness 0.50. function FANN_GAUSSIAN
Neurons       8. Current error: 0.204457. Total error: 39.2557. Epochs  1713. Bit fail 143. candidate steepness 1.00. function FANN_SIGMOID_SYMMETRIC
Neurons       9. Current error: 0.192103. Total error: 36.8839. Epochs  1904. Bit fail 126. candidate steepness 0.50. function FANN_GAUSSIAN
Neurons      10. Current error: 0.182559. Total error: 35.0513. Epochs  2157. Bit fail 127. candidate steepness 1.00. function FANN_GAUSSIAN_SYMMETRIC
Neurons      11. Current error: 0.171901. Total error: 33.0049. Epochs  2404. Bit fail  84. candidate steepness 0.50. function FANN_SIGMOID_SYMMETRIC
Neurons      12. Current error: 0.163253. Total error: 31.3447. Epochs  2649. Bit fail  93. candidate steepness 1.00. function FANN_GAUSSIAN_SYMMETRIC
Neurons      13. Current error: 0.160835. Total error: 30.8803. Epochs  2877. Bit fail  86. candidate steepness 0.50. function FANN_SIGMOID
Neurons      14. Current error: 0.151457. Total error: 29.0798. Epochs  3090. Bit fail  77. candidate steepness 1.00. function FANN_GAUSSIAN
Neurons      15. Current error: 0.136264. Total error: 26.1626. Epochs  3338. Bit fail  69. candidate steepness 1.00. function FANN_GAUSSIAN_SYMMETRIC
Neurons      16. Current error: 0.127493. Total error: 24.4787. Epochs  3561. Bit fail  60. candidate steepness 1.00. function FANN_GAUSSIAN_SYMMETRIC
Neurons      17. Current error: 0.120770. Total error: 23.1879. Epochs  3829. Bit fail  54. candidate steepness 1.00. function FANN_SIGMOID
Neurons      18. Current error: 0.113881. Total error: 21.8651. Epochs  4029. Bit fail  60. candidate steepness 1.00. function FANN_GAUSSIAN_SYMMETRIC
Neurons      19. Current error: 0.102948. Total error: 19.7661. Epochs  4259. Bit fail  51. candidate steepness 0.75. function FANN_GAUSSIAN_SYMMETRIC
Neurons      20. Current error: 0.084112. Total error: 16.1496. Epochs  4556. Bit fail  40. candidate steepness 1.00. function FANN_GAUSSIAN
Neurons      21. Current error: 0.076130. Total error: 14.6169. Epochs  4832. Bit fail  36. candidate steepness 1.00. function FANN_SIGMOID
Neurons      22. Current error: 0.068355. Total error: 13.1241. Epochs  5065. Bit fail  30. candidate steepness 0.75. function FANN_GAUSSIAN
Neurons      23. Current error: 0.058445. Total error: 11.2215. Epochs  5310. Bit fail  23. candidate steepness 1.00. function FANN_SIGMOID
Neurons      24. Current error: 0.046348. Total error:  8.8988. Epochs  5599. Bit fail  21. candidate steepness 0.75. function FANN_SIGMOID
Neurons      25. Current error: 0.035114. Total error:  6.7418. Epochs  5899. Bit fail  15. candidate steepness 1.00. function FANN_SIGMOID
Neurons      26. Current error: 0.027708. Total error:  5.3199. Epochs  6199. Bit fail  12. candidate steepness 1.00. function FANN_SIGMOID
Neurons      27. Current error: 0.017783. Total error:  3.4143. Epochs  6471. Bit fail   8. candidate steepness 1.00. function FANN_GAUSSIAN
Neurons      28. Current error: 0.013958. Total error:  2.6800. Epochs  6771. Bit fail   6. candidate steepness 1.00. function FANN_SIGMOID
Neurons      29. Current error: 0.006293. Total error:  1.2082. Epochs  7071. Bit fail   3. candidate steepness 1.00. function FANN_SIGMOID
Neurons      30. Current error: 0.002894. Total error:  0.5557. Epochs  7371. Bit fail   1. candidate steepness 1.00. function FANN_GAUSSIAN
Neurons      31. Current error: 0.000964. Total error:  0.1851. Epochs  7559. Bit fail   0. candidate steepness 1.00. function FANN_SIGMOID
Train outputs    Current error: 0.000022. Epochs   7709
⚠️ **GitHub.com Fallback** ⚠️