简体   繁体   中英

error :Input 0 of layer conv1d is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 20, 100, 100] when using cnn

I am using 1D convolution on text analysis. I have approximately 488,000 sentence with 20 words each each word has vector of dimension 100.

Word to vector of dimension 100:

model1 = gensim.models.Word2Vec(data, min_count = 1, size = 100, window = 5)

maximum words in post:

max_length=20

Shape of X train and X validation tensor: (390763, 20, 100) (97691, 20, 100)

Shape of label train and label validation tensor: (390763, 7) (97691, 7)

My model:

model.add(Embedding(input_dim=vocab_size,  output_dim=100,  input_length=20))
model.add(Conv1D(filters=128, kernel_size=5, activation='relu',input_shape=(20,100)))
model.add(Dropout(0.25))
model.add(MaxPooling1D(pool_size=3))
model.add(Dense(7, activation="softmax"))

Error:

Input 0 of layer conv1d is incompatible with the layer: expected ndim=3, 
found ndim=4. Full shape received: [None, 20, 100, 100]

Looks like problem with Conv1D layer.

Working sample code

import tensorflow as tf
import numpy as np
input_shape = (390763, 20, 100)
x = tf.random.normal(input_shape)

model = tf.keras.Sequential()
model.add(tf.keras.layers.Embedding(1000, 100, input_length=20))
model.add(tf.keras.layers.Conv1D(32, 3, activation='relu',input_shape=(20,100)))
input_array = np.random.randint(1000, size=(32, 20))
print(input_array.shape)
model.compile('rmsprop', 'mse')
output_array = model.predict(input_array)
print(output_array.shape)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question ValueError: Input 0 of layer conv2d_10 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 100, 100] Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 30) Conv1D: ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 2) ValueError: Input 0 of layer conv3d_8 is incompatible with the layer: : expected min_ndim=5, found ndim=4. Full shape received: [None, 4, 150, 150] ValueError: Input 0 of layer max_pooling1d is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 128, 1, 32) Input 0 of layer conv1d_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 200] Input 0 of layer conv1d_39 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 64) DCGAN: ValueError: Input 0 of layer deconv is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 100] Keras Conv1d input shape problem, Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2 Error 'Input 0 is incompatible with layer conv1d_48: expected ndim=3, found ndim=2' when adding Conv1D layer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM