【Mathematica】用神经网络来拟合数据

  • 原创
  • |
  • 浏览:1068
  • |
  • 更新:

本文,用Mathematica的NetTrain,来拟合非线性数据。

【Mathematica】用神经网络来拟合数据

工具/原料

  • 电脑
  • Mathematica

方法/步骤

  1. 1

    把非线性数据,转化为训练集:

    data = Table[x -> Sin[10 x]*Exp[-x^2], {x, -3, 3, .1}]

    训练集一定是个集合的元素的一一对应。

    【Mathematica】用神经网络来拟合数据
  2. 2

    可视化数据:

    d0=Table[{x , Sin[10 x]*Exp[-x^2]}, {x, -3, 3, .1}];

    ListLinePlot[d0,PlotRange->All]

    【Mathematica】用神经网络来拟合数据
  3. 3

    创建一个神经网络:

    net = NetChain[{10, Ramp, 10, Tanh, 1}, "Input" -> "Scalar", "Output" -> "Scalar"];

    里面有五个层。

    【Mathematica】用神经网络来拟合数据
  4. 4

    用训练集来训练这个网络:

    tnet= NetTrain[net, data, MaxTrainingRounds -> Quantity[1, "Seconds"]];

    训练10秒。

    【Mathematica】用神经网络来拟合数据
  5. 4
    该信息非法爬取自百度经验
  6. 5

    可视化训练结果:

    Show[ListLinePlot[d0,PlotStyle->Red,PlotRange->All],

     

    Plot[tnet[x],{x,-3,3},PlotStyle->Green,PlotRange->All]]

    【Mathematica】用神经网络来拟合数据
  7. 6

    如果只训练1秒,结果会如何?

    tnet= NetTrain[net, data, MaxTrainingRounds -> Quantity[1, "Seconds"]];

    可以看到,拟合结果很差。

    【Mathematica】用神经网络来拟合数据
  8. 7

    下面是训练2秒的结果:

    【Mathematica】用神经网络来拟合数据
  9. 8

    训练20秒的结果:

    tnet= NetTrain[net, data, MaxTrainingRounds -> Quantity[20, "Seconds"]];

    【Mathematica】用神经网络来拟合数据
    END
经验内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。
作者声明:本篇经验系本人依照真实经历原创,未经许可,谢绝转载。
展开阅读全部