我正在创建一个用于 2 类文本分类的小型 CNN。我能够使用单个卷积层创建并(成功)运行 CNN,但当我尝试添加第二个卷积层时,出现无法解决的错误。错误出现在第二个转换的输出上。
神经网络编译并开始适应,但随后失败并出现错误。
我尝试删除第一个 conv 层和 maxpool 层,一切正常。
有关如何操作的建议将不胜感激。
kerCNN2 = keras.Sequential()
kerCNN2.add(keras.layers.Embedding(len(dictChck), 32))
kerCNN2.add(keras.layers.Conv1D(24,5,activation=tf.nn.relu))
kerCNN2.add(keras.layers.MaxPooling1D(5))
kerCNN2.add(keras.layers.Conv1D(16,5,activation=tf.nn.relu))
kerCNN2.add(keras.layers.GlobalAveragePooling1D())
kerCNN2.add(keras.layers.Dense(16, activation=tf.nn.relu))
kerCNN2.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))
kerCNN2.summary()
kerCNN2.compile(optimizer="adam", loss="binary_crossentropy", metrics=["acc"])
trainHistCNN2 = kerCNN2.fit(encTrain, trainYPartial, epochs = 1, batch_size = 128, validation_data=(encTrainEval, trainYEval), verbose=1)
编译结果:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_23 (Embedding) (None, None, 32) 76915776
_________________________________________________________________
conv1d_32 (Conv1D) (None, None, 24) 3864
_________________________________________________________________
max_pooling1d_13 (MaxPooling (None, None, 24) 0
_________________________________________________________________
conv1d_33 (Conv1D) (None, None, 16) 1936
_________________________________________________________________
global_average_pooling1d_3 ( (None, 16) 0
_________________________________________________________________
dense_31 (Dense) (None, 16) 272
_________________________________________________________________
dense_32 (Dense) (None, 1) 17
=================================================================
Total params: 76,921,865
Trainable params: 76,921,865
Non-trainable params: 0
错误(相关部分):
InvalidArgumentError (see above for traceback): computed output size would be negative
[[Node: conv1d_33/convolution/Conv2D = Conv2D[T=DT_FLOAT, data_format="NHWC", padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/cpu:0"](conv1d_33/convolution/ExpandDims, conv1d_33/convolution/ExpandDims_1)]]
最佳答案
这是因为你的张量形状小于卷积核的大小。
e.g. Tensor shape is (None, None, 10, None), but the filter of conv is (X, 16, X, X).
10 小于 16。
关于python - 无效参数错误 : computed output size would be negative,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55693948/