JobPlus知识库 IT 工业智能4.0 文章
matlab BP神经网络 newff函数官方源码

最近在学习BP神经网络,想要自定义神经网络内部,但是网上找了很久都没有找到源代码,于是自己去搬运了一波matlab官方的源代码。


不多说,直接上代码,未编辑过的源码:


[html]

  1. function out1 = newff(varargin)  
  2. %NEWFF Create a feed-forward backpropagation network.  
  3. %  
  4. %  Obsoleted in R2010b NNET 7.0.  Last used in R2010a NNET 6.0.4.  
  5. %  The recommended function is <a href="matlab:doc feedforwardnet">feedforwardnet</a>.  
  6. %  
  7. %  Syntax  
  8. %  
  9. %    net = newff(P,T,S)  
  10. %    net = newff(P,T,S,TF,BTF,BLF,PF,IPF,OPF,DDF)  
  11. %  
  12. %  Description  
  13. %  
  14. %    NEWFF(P,T,S) takes,  
  15. %      P  - RxQ1 matrix of Q1 representative R-element input vectors.  
  16. %      T  - SNxQ2 matrix of Q2 representative SN-element target vectors.  
  17. %      Si  - Sizes of N-1 hidden layers, S1 to S(N-1), default = [].  
  18. %            (Output layer size SN is determined from T.)  
  19. %    and returns an N layer feed-forward backprop network.  
  20. %  
  21. %    NEWFF(P,T,S,TF,BTF,BLF,PF,IPF,OPF,DDF) takes optional inputs,  
  22. %      TFi - Transfer function of ith layer. Default is 'tansig' for  
  23. %            hidden layers, and 'purelin' for output layer.  
  24. %      BTF - Backprop network training function, default = 'trainlm'.  
  25. %      BLF - Backprop weight/bias learning function, default = 'learngdm'.  
  26. %      PF  - Performance function, default = 'mse'.  
  27. %      IPF - Row cell array of input processing functions.  
  28. %            Default is {'fixunknowns','remconstantrows','mapminmax'}.  
  29. %      OPF - Row cell array of output processing functions.  
  30. %            Default is {'remconstantrows','mapminmax'}.  
  31. %      DDF - Data division function, default = 'dividerand';  
  32. %    and returns an N layer feed-forward backprop network.  
  33. %  
  34. %    The transfer functions TF{i} can be any differentiable transfer  
  35. %    function such as TANSIG, LOGSIG, or PURELIN.  
  36. %  
  37. %    The training function BTF can be any of the backprop training  
  38. %    functions such as TRAINLM, TRAINBFG, TRAINRP, TRAINGD, etc.  
  39. %  
  40. %    *WARNING*: TRAINLM is the default training function because it  
  41. %    is very fast, but it requires a lot of memory to run.  If you get  
  42. %    an "out-of-memory" error when training try doing one of these:  
  43. %  
  44. %    (1) Slow TRAINLM training, but reduce memory requirements, by  
  45. %        setting NET.<a href="matlab:doc nnproperty.net_efficiency">efficiency</a>.<a href="matlab:doc nnproperty.net_efficiency_memoryReduction">memoryReduction</a> to 2 or more. (See HELP TRAINLM.)  
  46. %    (2) Use TRAINBFG, which is slower but more memory efficient than TRAINLM.  
  47. %    (3) Use TRAINRP which is slower but more memory efficient than TRAINBFG.  
  48. %  
  49. %    The learning function BLF can be either of the backpropagation  
  50. %    learning functions such as LEARNGD, or LEARNGDM.  
  51. %  
  52. %    The performance function can be any of the differentiable performance  
  53. %    functions such as MSE or MSEREG.  
  54. %  
  55. %  Examples  
  56. %  
  57. %    [inputs,targets] = simplefitdata;  
  58. %    net = newff(inputs,targets,20);  
  59. %    net = train(net,inputs,targets);  
  60. %    outputs = net(inputs);  
  61. %    errors = outputs - targets;  
  62. %    perf = perform(net,outputs,targets)  
  63. %  
  64. %  Algorithm  
  65. %  
  66. %    Feed-forward networks consist of Nl layers using the DOTPROD  
  67. %    weight function, NETSUM net input function, and the specified  
  68. %    transfer functions.  
  69. %  
  70. %    The first layer has weights coming from the input.  Each subsequent  
  71. %    layer has a weight coming from the previous layer.  All layers  
  72. %    have biases.  The last layer is the network output.  
  73. %  
  74. %    Each layer's weights and biases are initialized with INITNW.  
  75. %  
  76. %    Adaption is done with TRAINS which updates weights with the  
  77. %    specified learning function. Training is done with the specified  
  78. %    training function. Performance is measured according to the specified  
  79. %    performance function.  
  80. %  
  81. %  See also NEWCF, NEWELM, SIM, INIT, ADAPT, TRAIN, TRAINS  
  82.   
  83.   
  84. % Mark Beale, 11-31-97  
  85. % Copyright 1992-2010 The MathWorks, Inc.  
  86.   
  87.   
  88. %disp('NEWFF is no longer recommended. FEEDFORWARD is simpler and more efficient.');  
  89. % TODO - Recommendation function NNRECOMMEND  
  90.   
  91.   
  92. %% Boilerplate Code - Same for all Network Functions  
  93.   
  94.   
  95. persistent INFO;  
  96. if (nargin < 1), error(message('nnet:Args:NotEnough')); end  
  97. in1 = varargin{1};  
  98. if ischar(in1)  
  99.   switch in1  
  100.     case 'info',  
  101.       if isempty(INFO), INFO = get_info; end  
  102.       out1 = INFO;  
  103.   end  
  104. else  
  105.   out1 = create_network(varargin{:});  
  106. end  
  107.   
  108.   
  109. %% Boilerplate Code - Same for all Network Functions  
  110.   
  111.   
  112. %%  
  113. function info = get_info  
  114.   
  115.   
  116. info.function = mfilename;  
  117. info.name = 'Feed-Forward';  
  118. info.description = nnfcn.get_mhelp_title(mfilename);  
  119. info.type = 'nntype.network_fcn';  
  120. info.version = 6.0;  
  121.   
  122.   
  123. %%  
  124. function net = create_network(varargin)  
  125.   
  126.   
  127. if nargin < 2, error(message('nnet:Args:NotEnough')), end  
  128.   
  129.   
  130. v1 = varargin{1};  
  131. if isa(v1,'cell'), v1 = cell2mat(v1); end  
  132. v2 = varargin{2};  
  133. if nargin > 2, v3 = varargin{3}; end  
  134.   
  135.   
  136. if (nargin<= 6) && (size(v1,2)==2) && (~iscell(v2)) && (size(v2,1)==1) && ((nargin<3)||iscell(v3))  
  137.   nnerr.obs_use(mfilename,['See help for ' upper(mfilename) ' to update calls to the new argument list.']);  
  138.   net = new_5p0(varargin{:});  
  139. else  
  140.   net = new_5p1(varargin{:});  
  141. end  
  142.   
  143.   
  144. %=============================================================  
  145. function net = new_5p1(p,t,s,tf,btf,blf,pf,ipf,tpf,ddf)  
  146.   
  147.   
  148. if nargin < 2, error(message('nnet:Args:NotEnough')), end  
  149.   
  150.   
  151. % Defaults  
  152. if (nargin < 3), s = []; end  
  153. if (nargin < 4), tf = {}; end  
  154. if (nargin < 5), btf = 'trainlm'; end  
  155. if (nargin < 6), blf = 'learngdm'; end  
  156. if (nargin < 7), pf = 'mse'; end  
  157. if (nargin < 8), ipf = {'fixunknowns','removeconstantrows','mapminmax'}; end  
  158. if (nargin < 9), tpf = {'removeconstantrows','mapminmax'}; end  
  159. if (nargin < 10), ddf = 'dividerand'; end  
  160.   
  161.   
  162. % Format  
  163. if isa(p,'cell'), p = cell2mat(p); end  
  164. if isa(t,'cell'), t = cell2mat(t); end  
  165.   
  166.   
  167. % Error checking  
  168. if ~(isa(p,'double') || isreal(p)  || islogical(t))  
  169.   error(message('nnet:NNet:XNotLegal'))  
  170. end  
  171. if ~(isa(t,'double') || isreal(t) || islogical(t))  
  172.   error(message('nnet:NNet:TNotLegal'))  
  173. end  
  174. if isa(s,'cell')  
  175.   if (size(s,1) ~= 1)  
  176.     error(message('nnet:NNet:LayerSizes'))  
  177.   end  
  178.   for i=1:length(s)  
  179.     si = s{i};  
  180.     if ~isa(si,'double') || ~isreal(si) || any(size(si) ~= 1) || any(si<1) || any(round(si) ~= si)  
  181.       error(message('nnet:NNet:LayerSizes'))  
  182.     end  
  183.   end  
  184.   s = cell2mat(s);  
  185. end  
  186. if (~isa(s,'double')) || ~isreal(s) || (size(s,1) > 1) || any(s<1) || any(round(s) ~= s)  
  187.   error(message('nnet:NNet:LayerSizes'))  
  188. end  
  189.   
  190.   
  191. % Architecture  
  192. Nl = length(s)+1;  
  193. net = network;  
  194. net.numInputs = 1;  
  195. net.numLayers = Nl;  
  196. net.biasConnect = ones(Nl,1);  
  197. net.inputConnect(1,1) = 1;  
  198. [j,i] = meshgrid(1:Nl,1:Nl);  
  199. net.layerConnect = (j == (i-1));  
  200. net.outputConnect(Nl) = 1;  
  201.   
  202.   
  203. % Simulation  
  204. net.inputs{1}.processFcns = ipf;  
  205. for i=1:Nl  
  206.   if (i < Nl)  
  207.     net.layers{i}.size = s(i);  
  208.     if (Nl == 2)  
  209.       net.layers{i}.name = 'Hidden Layer';  
  210.     else  
  211.       net.layers{i}.name = ['Hidden Layer ' num2str(i)];  
  212.     end  
  213.   else  
  214.     net.layers{i}.name = 'Output Layer';  
  215.   end  
  216.   if (length(tf) < i) || all(isnan(tf{i}))  
  217.     if (i<Nl)  
  218.       net.layers{i}.transferFcn = 'tansig';  
  219.     else  
  220.       net.layers{i}.transferFcn = 'purelin';  
  221.     end  
  222.   else  
  223.     net.layers{i}.transferFcn = tf{i};  
  224.   end  
  225. end  
  226. net.outputs{Nl}.processFcns = tpf;  
  227.   
  228.   
  229. % Adaption  
  230. net.adaptfcn = 'adaptwb';  
  231. net.inputWeights{1,1}.learnFcn = blf;  
  232. for i=1:Nl  
  233.   net.biases{i}.learnFcn = blf;  
  234.   net.layerWeights{i,:}.learnFcn = blf;  
  235. end  
  236.   
  237.   
  238. % Training  
  239. net.trainfcn = btf;  
  240. net.dividefcn = ddf;  
  241. net.performFcn = pf;  
  242.   
  243.   
  244. % Initialization  
  245. net.initFcn = 'initlay';  
  246. for i=1:Nl  
  247.   net.layers{i}.initFcn = 'initnw';  
  248. end  
  249.   
  250.   
  251. % Configuration  
  252. % Warning: Use of these properties is no longer recommended  
  253. net.inputs{1}.exampleInput = p;  
  254. net.outputs{Nl}.exampleOutput = t;  
  255.   
  256.   
  257. % Initialize  
  258. net = init(net);  
  259.   
  260.   
  261. % Plots  
  262. net.plotFcns = {'plotperform','plottrainstate','plotregression'};  
  263.   
  264.   
  265. %================================================================  
  266. function net = new_5p0(p,s,tf,btf,blf,pf)  
  267. % Backward compatible to NNT 5.0  
  268.   
  269.   
  270. if nargin < 2, error(message('nnet:Args:NotEnough')), end  
  271.   
  272.   
  273. % Defaults  
  274. Nl = length(s);  
  275. if nargin < 3, tf = {'tansig'}; tf = tf(ones(1,Nl)); end  
  276. if nargin < 4, btf = 'trainlm'; end  
  277. if nargin < 5, blf = 'learngdm'; end  
  278. if nargin < 6, pf = 'mse'; end  
  279.   
  280.   
  281. % Error checking  
  282. if isa(p,'cell') && all(size(p)==[1 1]), p = p{1,1}; end  
  283. if (~isa(p,'double')) || ~isreal(p)  
  284.   error(message('nnet:NNData:XNotMatorCell1Mat'))  
  285. end  
  286. if isa(s,'cell')  
  287.   if (size(s,1) ~= 1)  
  288.     error(message('nnet:NNet:LayerSizes'))  
  289.   end  
  290.   for i=1:length(s)  
  291.     si = s{i};  
  292.     if ~isa(si,'double') || ~isreal(si) || any(size(si) ~= 1) || any(si<1) || any(round(si) ~= si)  
  293.       error(message('nnet:NNet:LayerSizes'))  
  294.     end  
  295.   end  
  296.   s = cell2mat(s);  
  297. end  
  298. if (~isa(s,'double')) || ~isreal(s) || (size(s,1) ~= 1) || any(s<1) || any(round(s) ~= s)  
  299.   error(message('nnet:NNet:LayerSizes'))  
  300. end  
  301.   
  302.   
  303. % Architecture  
  304. net = network(1,Nl);  
  305. net.biasConnect = ones(Nl,1);  
  306. net.inputConnect(1,1) = 1;  
  307. [j,i] = meshgrid(1:Nl,1:Nl);  
  308. net.layerConnect = (j == (i-1));  
  309. net.outputConnect(Nl) = 1;  
  310.   
  311.   
  312. % Simulation  
  313. for i=1:Nl  
  314.   net.layers{i}.size = s(i);  
  315.   net.layers{i}.transferFcn = tf{i};  
  316. end  
  317.   
  318.   
  319. % Performance  
  320. net.performFcn = pf;  
  321.   
  322.   
  323. % Adaption  
  324. net.adaptfcn = 'adaptwb';  
  325. net.inputWeights{1,1}.learnFcn = blf;  
  326. for i=1:Nl  
  327.   net.biases{i}.learnFcn = blf;  
  328.   net.layerWeights{i,:}.learnFcn = blf;  
  329. end  
  330.   
  331.   
  332. % Training  
  333. net.trainfcn = btf;  
  334.   
  335.   
  336. % Initialization  
  337. net.initFcn = 'initlay';  
  338. for i=1:Nl  
  339.   net.layers{i}.initFcn = 'initnw';  
  340. end  
  341.   
  342.   
  343. % Warning: this property is no longer recommended for use  
  344. net.inputs{1}.exampleInput = p;  
  345.   
  346.   
  347. net = init(net);  
  348.   
  349.   
  350. % Plots  
  351. net.plotFcns = {'plotperform','plottrainstate','plotregression'};  


如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏支持
286人赞 举报
分享到
用户评价(0)

暂无评价,你也可以发布评价哦:)

扫码APP

扫描使用APP

扫码使用

扫描使用小程序