ValueError向KerasRegressor提供数据

时间:2017-07-12 17:44:13

标签: python scikit-learn keras

我正在尝试使用Keras(版本2.0.6)和SKLearn(版本0.18.1)与Python 2.7进行一些基本的机器学习。

我的X输入有2,208行和51,786列,我正在设置我的基本神经网络如下:

def baseline_model():
    # create model
    model = Sequential()
    model.add(Dense(13, input_dim=51786, kernel_initializer='normal', activation='relu'))
    model.add(Dense(1, kernel_initializer='normal'))
    # Compile model
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model

estimator = KerasRegressor(build_fn=baseline_model, nb_epoch=100, batch_size=5, verbose=2)

kfold = KFold(n_splits=5, random_state=100)
results = cross_val_score(estimator, X, y, cv=kfold)

不幸的是,我得到了ValueError如下:

ValueError: Cannot feed value of shape (1766, 10) for Tensor u'dense_45_input:0', which has shape '(?, 51786)'

该形状(1766, 10)是我正在借用代码的教程中使用的原始形状。但是,我似乎无法找到改变这种形状的地方(除了input_dim)...

有什么建议吗?

谢谢!

编辑:奇怪地运行以下似乎运行:

estimators = []
estimators.append(('standardize', StandardScaler()))
estimators.append(('mlp', KerasRegressor(build_fn=baseline_model, epochs=50, batch_size=5, verbose=2)))
pipeline = Pipeline(estimators)
kfold = KFold(n_splits=5, random_state=100)
results = cross_val_score(pipeline, X, y, cv=kfold)
print("Standardized: %.2f (%.2f) MSE" % (results.mean(), results.std()))

0 个答案:

没有答案