Python Keras 错误:预计会看到 1 个数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42596057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:56:17  来源:igfitidea点击:

Keras error : Expected to see 1 array

pythonmachine-learningneural-networkdeep-learningkeras

提问by MysticForce

I got the following error when I tried to train an MLP model in keras(I am using keras version 1.2.2)

当我尝试在 keras 中训练 MLP 模型时出现以下错误(我使用的是 keras 版本1.2.2

Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 arrays but instead got the following list of 12859 arrays:

检查模型输入时出错:您传递给模型的 Numpy 数组列表不是模型预期的大小。预计会看到 1 个数组,但得到了以下 12859 个数组的列表:

This is the summary of the model

这是模型的总结

____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to
====================================================================================================
dense_1 (Dense)                  (None, 20)            4020        dense_input_1[0][0]
____________________________________________________________________________________________________
dense_2 (Dense)                  (None, 2)             42          dense_1[0][0]
====================================================================================================
Total params: 4,062
Trainable params: 4,062
Non-trainable params: 0
____________________________________________________________________________________________________
None

This is the first line of model

这是模型的第一行

 model.add(Dense(20, input_shape=(200,), init='lecun_uniform', activation='tanh'))

For training:

为了训练:

model.fit(X,Y,nb_epoch=100,verbose=1)

where X is a list of elements and each element in turn is a list of 200 values.

其中 X 是一个元素列表,每个元素又是一个包含 200 个值的列表。

Edit :

编辑 :

I also tried

我也试过

model.add(Dense(20, input_shape=(12859,200), init='lecun_uniform', activation='tanh'))

but I am getting the same error

但我遇到了同样的错误

回答by Marcin Mo?ejko

Your error comes from the fact that your Xfor some reason wasn't transformed to a numpy.array. In this your Xis treated as a list of rows and this is a reason behind your error message (that it expected one input instead of list which has a number of rows elements). Transformation:

您的错误来自于您X出于某种原因未转换为numpy.array. 在此您X被视为行列表,这是错误消息背后的原因(它期望一个输入而不是具有多个行元素的列表)。转型:

X = numpy.array(X)
Y = numpy.array(Y)

I would check a data loading process because something might go wrong there.

我会检查数据加载过程,因为那里可能会出错。

UPDATE:

更新:

As it was mentioned in a comment - input_shapeneed to be changed to input_dim.

正如评论中提到的 -input_shape需要更改为input_dim.

UPDATE 2:

更新 2:

In order to keep input_shapeone should change to it to input_shape=(200,).

为了保持input_shape一应改为input_shape=(200,)

回答by rocksyne

I fixed mine by adding

我通过添加修复了我的

np.array

数组

to train_X , train_Y , valid_X and valid_Y. For example,

到 train_X 、 train_Y 、 valid_X 和 valid_Y。例如,

model.fit(np.array(train_X),np.array(train_Y),
          batch_size=32,nb_epoch=20,
          validation_data=(np.array(valid_X),np.array(valid_Y)),
          callbacks=[early_stop])

I got the help from here. This approach is likely to have a slow run because all data features will have to be converted to numpy arrays and it could be a lot of work for your system.

我从这里得到了帮助。这种方法可能会运行缓慢,因为所有数据特征都必须转换为 numpy 数组,这可能会给您的系统带来大量工作。