Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidArgumentError: computed output size would be negative

I am creating a small CNN for 2-class text classification. I was able to create and run (successfully) the CNN with a single convolution layer, but when I try adding a second, I get an error that I cannot resolve. The error is on the output from the second conv.

The NN compiles and starts to fit but then fails with an error.

I have tried removing the first conv and maxpool layers, and everything worked.

Suggestions on what to do would be appreciated.


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)

The results of compilation:

_________________________________________________________________
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

The (relevant portion of) error:

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)]]
like image 749
Jebus Avatar asked Oct 28 '22 16:10

Jebus


1 Answers

That's because your Tensor shape is less then the size of conv kernel.

e.g. Tensor shape is (None, None, 10, None), but the filter of conv is (X, 16, X, X).

10 is less then 16.

like image 163
xingzhang ren Avatar answered Nov 15 '22 06:11

xingzhang ren