[关闭]
@devilogic 2016-08-02T06:10:08.000000Z 字数 3340 阅读 1752

神经网络工具箱教程(一)

matlab


使用神经网络拟合数据

神经网络是非常善于拟合函数的,事实上,有一个很精巧的证明显示神经网络能拟合任意实际的函数。
假设,举例说明你有一些房屋的信息。你想设计一个神经网络让它来预测一个房屋的价值,给定13个地理与真实的房产信息。你有506个这样13个特征的信息,并且有这506个样本关联的市场价格。

定义一个问题

整理一个Q个输入向量以列为单位组合成一个矩阵。然后安排另外一个Q列目标向量(有效的输出向量是一一对应每个输入向量)组合成第二个矩阵(理解神经网络的数据结构一文中详细描述了数据格式如何适应静态数据与时间序列数据)。举个例子,你能定义一个布尔值的与门拟合问题,它有四个两元素输入向量与一个目标向量如下:

  1. inputs = [0 1 0 1; 0 0 1 1];
  2. targets = [0 0 0 1];

简单的函数

  1. % Solve an Input-Output Fitting problem with a Neural Network
  2. % Script generated by Neural Fitting app
  3. % Created 01-Aug-2016 15:03:58
  4. %
  5. % This script assumes these variables are defined:
  6. %
  7. % houseInputs - 输入数据.
  8. % houseTargets - 目标数据.
  9. x = houseInputs;
  10. t = houseTargets;
  11. % 选择一个训练函数
  12. % 列出所有的训练函数,键入:help nntrain
  13. % 'trainlm' 通常是最快的.
  14. % 'trainbr' 花费时间更长但是可以适用更有复杂的问题.
  15. % 'trainscg' 使用较少的内存.适合配置较低的机器.
  16. trainFcn = 'trainlm'; % Levenberg-Marquardt 反向传播.
  17. % 创建一个拟合网络
  18. hiddenLayerSize = 10; % 隐藏层为10
  19. net = fitnet(hiddenLayerSize,trainFcn);
  20. % 分离数据为:训练,验证,测试
  21. net.divideParam.trainRatio = 70/100; % 训练使用70%的数据
  22. net.divideParam.valRatio = 15/100; % 验证使用15%的数据
  23. net.divideParam.testRatio = 15/100; % 测试使用15%的数据
  24. % 训练网络
  25. [net,tr] = train(net,x,t);
  26. % 测试网络
  27. y = net(x);
  28. e = gsubtract(t,y);
  29. performance = perform(net,t,y)
  30. % 浏览网络
  31. view(net)
  32. % 画图
  33. % 反注释掉以下代码开启画图功能.
  34. %figure, plotperform(tr)
  35. %figure, plottrainstate(tr)
  36. %figure, ploterrhist(e)
  37. %figure, plotregression(t,y)
  38. %figure, plotfit(net,x,t)

高级些的函数

  1. % Solve an Input-Output Fitting problem with a Neural Network
  2. % Script generated by Neural Fitting app
  3. % Created 01-Aug-2016 15:04:12
  4. %
  5. % This script assumes these variables are defined:
  6. %
  7. % houseInputs - 输入数据.
  8. % houseTargets - 目标数据.
  9. x = houseInputs;
  10. t = houseTargets;
  11. % 选择一个训练函数
  12. % 列出所有的训练函数,键入:help nntrain
  13. % 'trainlm' 通常是最快的.
  14. % 'trainbr' 花费时间更长但是可以适用更有复杂的问题.
  15. % 'trainscg' 使用较少的内存.适合配置较低的机器.
  16. trainFcn = 'trainlm'; % Levenberg-Marquardt 反向传播.
  17. % 创建一个拟合网络
  18. hiddenLayerSize = 10; % 隐藏层为10
  19. net = fitnet(hiddenLayerSize,trainFcn);
  20. % 创建适用性网络
  21. hiddenLayerSize = 10;
  22. net = fitnet(hiddenLayerSize,trainFcn);
  23. % 选择输入与输出预处理处理函数.
  24. % 列出所有的数据预处理函数,键入:help nnprocess
  25. net.input.processFcns = {'removeconstantrows','mapminmax'};
  26. net.output.processFcns = {'removeconstantrows','mapminmax'};
  27. % 设置分离数据为:训练,验证,测试
  28. % 列出所有数据分离函数,键入:help nndivide
  29. net.divideFcn = 'dividerand'; % 分离数据随机
  30. net.divideMode = 'sample'; % 分离每个样本
  31. net.divideParam.trainRatio = 70/100;
  32. net.divideParam.valRatio = 15/100;
  33. net.divideParam.testRatio = 15/100;
  34. % 选择一个性能评估函数
  35. % 列出所有性能函数,键入:help nnperformance
  36. net.performFcn = 'mse'; % Mean Squared Error
  37. % 选择绘图函数
  38. % 列出所有绘图函数,键入:help nnplot
  39. net.plotFcns = {'plotperform','plottrainstate','ploterrhist', ...
  40. 'plotregression', 'plotfit'};
  41. % 训练
  42. [net,tr] = train(net,x,t);
  43. % 测试网络
  44. y = net(x);
  45. e = gsubtract(t,y);
  46. performance = perform(net,t,y)
  47. % 重新计算 训练,验证和测试性能
  48. trainTargets = t .* tr.trainMask{1};
  49. valTargets = t .* tr.valMask{1};
  50. testTargets = t .* tr.testMask{1};
  51. trainPerformance = perform(net,trainTargets,y)
  52. valPerformance = perform(net,valTargets,y)
  53. testPerformance = perform(net,testTargets,y)
  54. % 浏览网络
  55. view(net)
  56. % 画图
  57. % 反注释掉以下代码开启画图功能.
  58. %figure, plotperform(tr)
  59. %figure, plottrainstate(tr)
  60. %figure, ploterrhist(e)
  61. %figure, plotregression(t,y)
  62. %figure, plotfit(net,x,t)
  63. % 部署
  64. % 改变 (false) 值到 (true) 来启用以下代码块.
  65. % See the help for each generation function for more information.
  66. if (false)
  67. % Generate MATLAB function for neural network for application
  68. % deployment in MATLAB scripts or with MATLAB Compiler and Builder
  69. % tools, or simply to examine the calculations your trained neural
  70. % network performs.
  71. genFunction(net,'myNeuralNetworkFunction');
  72. y = myNeuralNetworkFunction(x);
  73. end
  74. if (false)
  75. % Generate a matrix-only MATLAB function for neural network code
  76. % generation with MATLAB Coder tools.
  77. genFunction(net,'myNeuralNetworkFunction','MatrixOnly','yes');
  78. y = myNeuralNetworkFunction(x);
  79. end
  80. if (false)
  81. % 为仿真或者部署产生一个仿真模型图
  82. gensim(net);
  83. end
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注