为什么我的 Tensorflow 模型无法预测 2 次的简单二次方程?



我试图在TensorFlow中创建一个模型来预测一个简单的y=x^2函数。但我的模型行为完全错误。我该怎么修?

模型
model = Sequential([Dense(units=1, input_shape=[1], use_bias=True),
Activation('relu')])
model.compile(optimizer='Adam', loss='mean_squared_logarithmic_error',
metrics=['mae','accuracy'])
变量X
xs1 = np.array(list(range(-100,100,1)), dtype=float)
变量Y
ys = xs1*xs1
符合模型
model.fit(xs1, ys, batch_size=30, epochs=500)
预测
y_predict = model.predict(xs1)
显示预测值和实际值
plt.scatter(x, y_predict)
plt.scatter(xs1, ys)

PLOT:预测是完全错误的。当模型应该是二次时,它得到了一个看似线性的方程

预测结果
array([[426.38446  ],
[422.14005  ],
[417.89563  ],
[413.6512   ],
[409.4068   ],
[405.16238  ],
[400.91797  ],
[396.67355  ],
[392.42914  ],
[388.18472  ],
[383.9403   ],
[379.6959   ],
[375.45148  ],
[371.20706  ],
[366.96265  ],
[362.71823  ],
[358.47382  ],
[354.2294   ],
[349.985    ],
[345.74057  ],
[341.49612  ],
[337.2517   ],
[333.0073   ],
[328.76288  ],
[324.51846  ],
[320.27405  ],
[316.02963  ],
[311.78522  ],
[307.5408   ],
[303.2964   ],
[299.05197  ],
[294.80756  ],
[290.56314  ],
[286.31873  ],
[282.0743   ],
[277.8299   ],
[273.58548  ],
[269.34106  ],
[265.09665  ],
[260.85223  ],
[256.60782  ],
[252.3634   ],
[248.11899  ],
[243.87457  ],
[239.63016  ],
[235.38573  ],
[231.14131  ],
[226.8969   ],
[222.65248  ],
[218.40807  ],
[214.16365  ],
[209.91924  ],
[205.67482  ],
[201.4304   ],
[197.18599  ],
[192.94157  ],
[188.69716  ],
[184.45274  ],
[180.20833  ],
[175.96391  ],
[171.71948  ],
[167.47507  ],
[163.23065  ],
[158.98624  ],
[154.74182  ],
[150.4974   ],
[146.25299  ],
[142.00858  ],
[137.76416  ],
[133.51974  ],
[129.27533  ],
[125.030914 ],
[120.7865   ],
[116.542076 ],
[112.29766  ],
[108.053246 ],
[103.80883  ],
[ 99.564415 ],
[ 95.32     ],
[ 91.075584 ],
[ 86.83116  ],
[ 82.58675  ],
[ 78.34233  ],
[ 74.097916 ],
[ 69.8535   ],
[ 65.609085 ],
[ 61.36467  ],
[ 57.12025  ],
[ 52.875835 ],
[ 48.63142  ],
[ 44.387    ],
[ 40.142586 ],
[ 35.89817  ],
[ 31.653757 ],
[ 27.40934  ],
[ 23.164923 ],
[ 18.920507 ],
[ 14.67609  ],
[ 10.431675 ],
[  6.1872582],
[  1.942842 ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ],
[  0.       ]], dtype=float32)

不能用线性函数来预测非线性函数。尝试添加另一层或更多单位

model = Sequential([Dense(units=8, input_shape=[1], use_bias=True),
Activation('relu'),
Dense(units=1, activation='relu')])
model.compile(optimizer='Adam', loss='mean_squared_logarithmic_error',
metrics=['mae','accuracy'])

还有,你为什么要使用损失函数,mse工作不好吗?

相关内容

  • 没有找到相关文章

最新更新