libfann R binding 快速神经网络在R上的绑定
视神经传导分层 Picture from Simon. Thorpe
Nerual Network
Activation Function
libfann 介绍 主要特点: C开发、多层、易用、快速、多参数易调节 支持NN BP 多种方法 (RPROP, Quickprop, Batch, Incremental) 主要语言种类: C++、Java、Ada、PERL、PHP、Python、Ruby、Visual Prolog 7 计算相关的: Mathematica、Matlab、Octave
rfann下载及安装 获取软件包: https://sourceforge.net/projects/rfann/ 获取软件包: https://sourceforge.net/projects/rfann/ *NIX 环境,gcc 开发工具环境 安装命令 R CMD INSTALL ./fann.tgz
rfann 之 R API fann_create fann_SetOpt fann_opts fann_Commit fann_train fann_test fann_save fann_read fann_version_info
rfann参数 [1] "------------------------------------------------------------" [2] "network type [ LAYER | SHORTCUT ] Can't be set" [3] "network_type=FANN_NETTYPE_LAYER" [5] "------------------------------------------------------------" [6] "ann type [ Simple | Cascade | Expand | Sparse ] For Create" [9] "-------------------- Basic parameters --------------------" [10] "init_weight=Init" [11] "num_layers=2" [12] "" [13] "num_input=2" [14] "num_output=1" [15] "num_neurons_hidden=3" [16] "desired_error=0.002000" [17] "-------------------- Expand parameters --------------------" [18] "randomize_weight_high=0.350000" [19] "randomize_weight_low=0.350000" [20] "learning_rate=0.700000" [21] "connection_rate=0.200000" [22] "bit_fail_limit=0.010000" [23] "max_epochs=10000" [24] "epochs_between_reports=1000" [25] "learning_momentum=0.700000" [26] "------------------------------------------------------------" [27] "train algorithm: [ INCREMENTAL | BATCH | RPROP | QUICKPROP ]"
rfann参数 cont. [30] "------------------------------------------------------------" [31] "error function: [ LINEAR | TANH ]" [32] "train_error_function=FANN_ERRORFUNC_TANH" [33] "" [34] "------------------------------------------------------------" [35] "stop function: [ MSE | BIT ]" [36] "train_stop_function=FANN_STOPFUNC_BIT" [37] "" [38] "-------------------- rprop parameters ---------------------" [39] "quickprop_decay=-0.000100" [40] "quickprop_mu=1.750000" [41] "rprop_increase_factor=1.200000" [42] "rprop_decrease_factor=0.500000" [43] "rprop_delta_min=0.000000" [44] "rprop_delta_max=50.000000" [45] "rprop_delta_zero=0.000000" [46] "" [47] "activation_steepness_hidden=2.000000" [48] "activation_steepness_output=4.000000" [49] "------------------------------------------------------------" [50] "Options below [ LINEAR | THRESHOLD | THRESHOLD_SYMMETRIC ]" [51] "cont: [ SIGMOID| SIGMOID_STEPWISE| SIGMOID_SYMMETRIC | SIGMOID_SYMMETRIC_STEPWISE ]" [52] "cont: [ GAUSSIAN| GAUSSIAN_SYMMETRIC | GAUSSIAN_STEPWISE ]" [53] "cont: [ ELLIOT | ELLIOT_SYMMETRIC" [54] "cont: [ LINEAR_PIECE | LINEAR_PIECE_SYMMETRIC ]" [55] "cont: [ SIN_SYMMETRIC| COS_SYMMETRIC | SIN | COS ]" [56] "------------------------------------------------------------" [57] "activation_function_hidden=FANN_SIGMOID_SYMMETRIC" [58] "activation_function_output=FANN_GAUSSIAN_SYMMETRIC"
例程 library(fann) x<-c( 1, 1,-1,-1, 1,-1, 1,-1) dim(x) <- c(4,2) y<-c(-1,1,1,-1) ann<-fann_create(num_layers=3,layer1=2,layer2=2,layer3=1,num_neurons_hidden=3); fann_SetOpt(ann,bit_fail_limit=0.35,train_stop_function="MSE") fann_SetOpt(ann,........) fann_Commit(ann) fann_train(ann,x,y) fann_save(ann,"./xortemp.net") o<-fann_test(ann,x,y)
rfann中的结构 Nncomon.h FannOpt.c FannProc.c FannData.c typedef struct FANNOPT { char *optname; int type; FANNCODE opt; // (void *) value; } FannOpt; FannOpt.c FannProc.c FannData.c
遗留问题 *NIX 版本 list 类型的支持 fann GUI 目前在R-exts还不能解析list类型 图形化显示rfann结构 图形化显示训练过程
rfann适用局限性 训练时间长:人工神经网络需要长时间的训练,有时可能使之变得不实用。大多数简单问题的网络训练需要至少上千次迭代,复杂问题的训练可能需要多达数万次迭代。 需大量训练数据:只有少量输入-输出数据,一般不考虑使用人工神经网络。 不能保证最佳结果:训练可能导致网络发生偏离,使之在一些操作区域内结果准确,而在其他区域则不准确。此外,在训练过程中,有可能偶尔陷入“局部最小”。 不能保证完全可靠:尽管这一点对所有的计算问题均适用,但对人工神经网络尤其如此。需要高度可靠的问题,在采用人工神经网络时必须小心谨慎。生物制药【Eric Xing】、金融程序交易④、火控;
结论:神经网络的隐层 隐层数 通常增加隐层数可以降低网络误差(也有文献认为不一定能有效降低),提高精度,但也使网络复杂化,从而增加了网络的训练时间和出现“过拟合”的倾向。Hornik等早已证明:若输入层和输出层采用线性转换函数,隐层采用Sigmoid转换函数,则含一个隐层的MLP网络能够以任意精度逼近任何有理函数。□ 一般地,靠增加隐层节点数来获得较低的误差,其训练效果要比增加隐层数更容易实现。
推荐阅读材料 Steffen Nissen Implementation of a Fast Artificial Neural Network Library (fann) Anil K.Jain Jianchang Mao K.M Mohiuddin: Artificial Neural Network:: A Tutorial IEEE March 1996 Ben Krose Patrick van der Smagt An introduction to Neural Networks eighth edition JingTao YAO &Chew Lim TAN (10.1.1.18.6874) Guidelines for Financial Forecasting with Neural Networks
感谢