python - 无法找出 keras 输入形状错误?

标签 python neural-network keras keras-layer

我试图向我的神经网络传递一个形状为 (1169909, 10, 10) 的数组。

但无论我做什么...

input_shape=(None,10,10)
input_shape=x_train.shape[1:]
input_shape=x_train.shape
input_shape=(1169909, 1)
input_shape=(10,10)
input_shape=(1169909,10)
input_shape=(1169909,10,10)
input_shape=(1,10,10)

我仍然收到错误。错误发生变化:

but got array with shape (1169909, 10, 10)
but got array with shape (1169909, 1)

取决于我提供输入的方式,这只会增加我的困惑。

实际输入如下所示,它是这些较小的 10x10 数组的数组:

array([[ 2, -1, -1, -1, -1, -1, -1, -1, -1,  0],
       [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
       [11, -1, 11,  6,  3,  2, -1, -1, -1, 11],
       [-1, -1, -1, -1,  5,  7, -1, -1,  2,  7],
       [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
       [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
       [-1, -1, -1, -1, 22, 25, -1, -1, -1, 22],
       [22, -1, -1, -1, 26, 29, -1, -1, 26, 25],
       [27, -1, -1, -1, -1, -1, -1, -1, -1, 23],
       [31, 24, 31, -1, -1, -1, -1, -1, -1, 31]])

我试图通过查看堆栈溢出和其他地方来理解这个问题,但我无法找出问题所在,并且这些解决方案对我不起作用。

这是目前的模型:

x_train, x_test, y_train, y_test = train_test_split(xving, ywing, test_size=0.2)

x_train = numpy.array(x_train)
y_train = numpy.array(y_train)


model = Sequential()
model.add(keras.layers.Dense(250,activation='tanh', input_shape=(None,10,10)))
model.add(keras.layers.Dense(150,activation='relu'))
model.add(keras.layers.Dense(25,activation='sigmoid'))
model.add(keras.layers.Dense(2,activation='softmax'))
optimizerr = keras.optimizers.SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False)
model.compile(optimizer=optimizerr, loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(x_train, y_train,epochs = 25, batch_size = 32, verbose=1)

编辑: 所以当我使用时:

input_shape=x_train.shape[1:]

目标出现错误:

Error when checking target: expected dense_32 to have 3 dimensions, but got array with shape (1169909, 1)

但是目标是一个数组。当我将其保留为列表时,我收到错误:

ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 1169909 arrays: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...

所以现在我想我的问题是关于为什么目标是一个问题。

y_train.shape

产量:

(1169909,)

所以我还是很困惑?

最佳答案

假设你有 1169909 张图片(或 2D 数据项)作为数据集,如果你想将它们作为矩阵传递给你的模型,你应该在前面使用 2D 卷积层,否则它与之前展平每张图片没有什么不同将它们全部传递给您的模型。我建议使用卷积模式,但如果不是这种情况,您可以像这样展平数组:

x = x.reshape((1169909, 100))
model.add(keras.layers.Dense(250, activation='tanh', input_dim=100))

同样,将 2D 数据传递到密集层不会利用数据的结构属性。

关于python - 无法找出 keras 输入形状错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53071237/

相关文章:

keras - Keras 中的 GRU/LSTM 具有不同长度的输入序列

python - 卡住神经网络的权重,使其输出在特定点取特定值( tensorflow )

嵌套在数据框中的列表上的 Python 绝对值

python - 在 Django 中从 DateField 迁移到 DateTimeField

python - 如何通过名称访问对象的属性?

python - 在 Tensorflow 中重新初始化变量

python - 神经网络训练平台中的梯度下降

keras - 嵌入层,我可以更改模型管道中经过训练的嵌入层的值吗?

python - 如何 "standardize"具有可变长度的数据集?

python - 使用 redirect() 时 View 没有返回 HttpResponse 对象