非线性回归:为什么不进行模型学习?

时间:2018-02-22 18:04:35

标签: machine-learning neural-network keras regression non-linear-regression

我刚开始学习keras。我正在尝试在keras中训练一个非线性回归模型,但模型似乎并没有多大的学习。

#datapoints
X = np.arange(0.0, 5.0, 0.1, dtype='float32').reshape(-1,1)
y = 5 * np.power(X,2) + np.power(np.random.randn(50).reshape(-1,1),3)

#model
model = Sequential()
model.add(Dense(50, activation='relu', input_dim=1))
model.add(Dense(30, activation='relu', init='uniform'))
model.add(Dense(output_dim=1, activation='linear'))

#training
sgd = SGD(lr=0.1);
model.compile(loss='mse', optimizer=sgd, metrics=['accuracy'])
model.fit(X, y, nb_epoch=1000)

#predictions
predictions = model.predict(X)

#plot
plt.scatter(X, y,edgecolors='g')
plt.plot(X, predictions,'r')
plt.legend([ 'Predictated Y' ,'Actual Y'])
plt.show()

enter image description here

我做错了什么?

1 个答案:

答案 0 :(得分:6)

你的学习率太高了。

此外,与您的问题无关,但您不应该要求metrics=['accuracy'],因为这是回归设置和accuracy is meaningless

所以,随着这些变化:

sgd = SGD(lr=0.001);
model.compile(loss='mse', optimizer=sgd)

plt.legend([ 'Predicted Y' ,'Actual Y']) # typo in legend :)

这里有一些输出(由于y的随机元素,结果在运行中会有所不同):

enter image description here

enter image description here