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.