Keras抛出:输入0与层conv2d_1不兼容:预期ndim = 4,发现ndim = 3

时间:2019-09-09 08:27:57

标签: python keras

摘要: 我的数据形状像(10, 300):长度为300的浮点数数组,以及由这300个数字组成的10数组。批量大小为400。

当我尝试使用此数据填充以下模型时,它将引发此异常:

  

ValueError:输入0与层conv2d_1不兼容:预期ndim = 4,找到ndim = 3

我尝试在以下代码中的inputsconv_0之间添加一个重塑层:reshape = Reshape((10, EMBEDDING_DIM, 1))(inputs),但这一次引发异常:

  

检查输入时出错:预期input_1具有3维,但数组的形状为(400,1)

我使用asserts来确保数据的形状,并通过以下所有内容:

assert len(X) == 400, "Total number of documents is not 400"
assert len(X[0]) == 10, "Not splitted well"
assert len(X[0][0]) == 300, "Word vector error"

模型:

# model
inputs = Input(batch_shape=(None, 10, EMBEDDING_DIM), dtype='float32')

conv_0 = Conv2D(256, kernel_size=(3, 3), activation='relu')(reshape)
maxpool_0 = MaxPooling2D(pool_size=(2, 2), strides=(1, 1))(conv_0)

conv_1 = Conv2D(256, kernel_size=(3, 3), activation='relu')(maxpool_0)
maxpool_1 = MaxPooling2D(pool_size=(2, 2), strides=(1, 1))(conv_1)

conv_2 = Conv2D(256, kernel_size=(3, 3), activation='relu')(maxpool_1)
maxpool_2 = MaxPooling2D(pool_size=(2, 2), strides=(1, 1))(conv_2)

flatten = Flatten()(maxpool_2)
dropout = Dropout(0.5)(flatten)
outputs = Dense(units=len(le.classes_),
                activation='softmax')(dropout)

model = Model(inputs=inputs, outputs=outputs)

adamoptimizer = Adam(lr=0.0001)

model.compile(optimizer=adamoptimizer,
                loss='sparse_categorical_crossentropy', metrics=['accuracy'])
print(model.summary())

摘要:

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         (None, 10, 300)           0
_________________________________________________________________
reshape_1 (Reshape)          (None, 10, 300, 1)        0
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 8, 298, 256)       2560
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 297, 256)       0
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 5, 295, 256)       590080
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 4, 294, 256)       0
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 2, 292, 256)       590080
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 1, 291, 256)       0
_________________________________________________________________
flatten_1 (Flatten)          (None, 74496)             0
_________________________________________________________________
dropout_1 (Dropout)          (None, 74496)             0
_________________________________________________________________
dense_1 (Dense)              (None, 21)                1564437
=================================================================
Total params: 2,747,157
Trainable params: 2,747,157
Non-trainable params: 0
_________________________________________________________________
None

0 个答案:

没有答案