[英]Python: Fitting a 3D function using MLPRegressor
我正在嘗試使用MLPRegressor來適應預定義的3D功能。 問題是我無法打印出正確的結果,因此我的擬合在繪制時看起來很糟糕。
它的功能如下:
def threeDFunc(xin,yin):
z = np.zeros((40,40))
for xIndex in range(0,40,1):
for yIndex in range(0,40,1):
z[xIndex,yIndex]=(np.exp(-(xin[xIndex]**2+yin[yIndex]**2)/0.1))
return z
xThD = np.arange(-1,1,0.05)
yThD = np.arange(-1,1,0.05)
zThD = threeDFunc(xThD, yThD)
上面的圖是應該近似的。
紅色就是它的功能。
代碼如下所示:
classifier = neural_network.MLPRegressor(hidden_layer_sizes=(200, 200), activation='logistic', learning_rate='adaptive')
xy = np.array((xThD.flatten(),yThD.flatten()))
classifier.fit(np.transpose(xy), zThD)
pre = classifier.predict(np.transpose(xy))
import pylab
from mpl_toolkits.mplot3d import Axes3D
fig = pylab.figure()
ax = Axes3D(fig)
X, Y = np.meshgrid(xThD, yThD)
ax.plot_wireframe(X, Y, zThD)
ax.plot_wireframe(X, Y, pre, color='red')
print(np.shape(zThD))
print(np.shape(pre))
plt.show()
使用activation='tanh'
將激活函數更改為雙曲線tan函數,使用solver='lbfgs'
將解算器更改為lbfgs。
如果你的分類器實例化如下所示,紅色和藍色的圖應該幾乎相同:
classifier = neural_network.MLPRegressor(hidden_layer_sizes=(200, 200), solver='lbfgs', activation='tanh', learning_rate='adaptive')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.