使用KerasRegressor获得非常糟糕的预测 [英] Getting very bad prediction with KerasRegressor

查看:789
本文介绍了使用KerasRegressor获得非常糟糕的预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在虚拟数据集上使用了KerasRegressor,并试图预测训练值本身.它给我的输出远远不能令人满意.训练数据根本不是随机的.有人可以帮我吗?

I used KerasRegressor on a dummy dataset and tried to predict the training values itself. It is giving me an output far from satisfactory. The training data is not random at all. Could anyone help me out?

from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor

import numpy as ny

X = ny.array([[1,2], [3,4], [5,6], [7,8], [9,10]])
Y = ny.array([3, 4, 5, 6, 7])
N = 5

def brain():
    #Create the brain
    br_model=Sequential()
    br_model.add(Dense(3, input_dim=2, kernel_initializer='normal',activation='relu'))
    br_model.add(Dense(2, kernel_initializer='normal',activation='relu'))
    br_model.add(Dense(1,kernel_initializer='normal'))

    #Compile the brain
    br_model.compile(loss='mean_squared_error',optimizer='adam')
    return br_model


estimator = KerasRegressor(build_fn=brain, nb_epoch=1000000, batch_size=5,verbose=1)
print "Done"


estimator.fit(X,Y)
prediction = estimator.predict(X)

print Y
print prediction

输出为

[3 4 5 6 7]
[0.001 0.001 0.001 0.001 0.001]

基本上,预测值是0.001,而实际值不是. 我尝试了其他网络配置,但遇到了同样的问题. 我该怎么做/(不做)以获得准确的输出?

Basically, the prediction is 0.001 while actual value is not. I've tried with other network configs but I face the same issue. What must I do/(not do) to get an accurate output??

推荐答案

这是由于新开业者犯了一个经典错误,即在将其输入神经网络之前不对其数据进行规范化(请参阅此答案中的第三点对于在卷积神经网络的分类设置中引起相似问题的相同问题.

This is due to a classic mistake made by new practitioners, i.e. not normalizing their data before feeding them into a neural network (see the third point in this answer for the same issue causing similar problems in a classification setting with a convolutional neural network).

(我承认,在我看到的大多数教程中,通常都没有足够强调强调这一关键点;例如,甚至在Tensorflow

(I confess that, in most tutorials I have seen, this crucial point is usually not emphasized strongly enough; and it can be even worse, e.g. in the Tensorflow MNIST For ML Beginners tutorial, it turns out that the data returned by the Tensorflow-provided utility functions are already normalized in [0, 1], transparently to the user and without any hint provided, hence hiding from the reader a crucial step that will certainly need to be done later, when using own data).

因此,您需要规范化功能输出;保留显示的XY数据:

So, you need to normalize both your features and your output; keeping your shown X and Y data:

# Feature Scaling (ignore possible warnings due to conversion of integers to floats)
from sklearn.preprocessing import StandardScaler

sc_X = StandardScaler()
X_train = sc_X.fit_transform(X)

sc_Y = StandardScaler()
Y_train = sc_Y.fit_transform(Y)

然后,将您的时期更改为仅1000个(对于这些数据,您绝对不需要100万个时代!),并适合缩放后的数据:

Then, changing your epochs to just 1000 (you definitely don't need 1 million epochs for these data!), and fitting on the scaled data:

estimator = KerasRegressor(build_fn=brain, epochs=1000, batch_size=5,verbose=1)
estimator.fit(X_train,Y_train)

您将得到:

prediction = estimator.predict(X_train)

print(Y_train)
print(prediction)
# [-1.41421356 -0.70710678  0.          0.70710678  1.41421356]
# [-1.01221371 -1.01221371 -0.08764046  0.69051325  1.42577887]

或者,使用

Or, scaling your output back to its original range with inverse_transform:

print(Y)
print(sc_Y.inverse_transform(prediction))
# [3 4 5 6 7]
# [ 3.56851363  3.56851363  4.87605762  5.97653294  7.01635551]

这篇关于使用KerasRegressor获得非常糟糕的预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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