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

时间:2024-10-12 15:22:29

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" -> "Sc锾攒揉敫alar", "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】用神经网络来拟合数据
© 手抄报圈