无法确定keras输入形状错误吗?

时间:2018-10-30 19:03:53

标签: 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,)

所以我还是很困惑吗?

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

我觉得答案应该是

input_shape=x_train.shape[1:]

由于无法正常工作,您能否分享所得到的错误?