我正在创建一个用于2类文本分类的小型CNN。我能够使用单个卷积层创建并运行(成功)CNN,但是当我尝试添加第二个卷积层时,出现了我无法解决的错误。错误出现在第二次转化的输出上。
NN会编译并开始拟合,但随后会失败并显示错误。
我尝试删除第一个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)]]
答案 0 :(得分:0)
那是因为您的Tensor形状小于conv内核的大小。
例如张量形状为(None,None,10,None),但conv的过滤器为(X,16,X,X)。
10小于16。