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

2025-09-27 07:25:01

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

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

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

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

2、可视化数据:

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

ListLinePlot[d0,PlotRange->All]

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

3、创建一个神经网络:

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

里面有五个层。

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

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

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

训练10秒。

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

5、可视化训练结果:

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

 

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

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

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

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

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

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

7、下面是训练2秒的结果:

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

8、训练20秒的结果:

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

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

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢