非线性回归:为什么模型不学习? [英] Non linear Regression: Why isn't the model learning?

查看:27
本文介绍了非线性回归:为什么模型不学习?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习 keras.我正在尝试在 keras 中训练非线性回归模型,但模型似乎没有学到太多东西.

#datapointsX = 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.add(Dense(50, activation='relu', input_dim=1))模型.添加(密集(30,激活='relu',init='uniform'))模型.add(密集(output_dim=1, activation='linear'))#训练SGD = SGD(lr=0.1);model.compile(loss='mse', optimizer=sgd, metrics=['accuracy'])模型拟合(X,Y,nb_epoch=1000)#预测预测 = 模型.预测(X)#阴谋plt.scatter(X, y,edgecolors='g')plt.plot(X,预测,'r')plt.legend(['预测 Y','实际 Y'])plt.show()

我做错了什么?

解决方案

你的学习率太高了.

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

I just started learning keras. I am trying to train a non-linear regression model in keras but model doesn't seem to learn much.

#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()

what am I doing wrong?

解决方案

Your learning rate is way too high.

Also, irrelevant to your issue, but you should not ask for metrics=['accuracy'], as this is a regression setting and accuracy is meaningless.

So, with these changes:

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

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

here are some outputs (results will be different among runs, due to the random element of your y):

这篇关于非线性回归:为什么模型不学习?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆