我在文本分析中使用1D卷积。我有大约488000个句子,每个单词有20个单词,每个单词的向量为100。
字到维度为100的矢量:
model1 = gensim.models.Word2Vec(data, min_count = 1, size = 100, window = 5)
帖子中的最大字数:
max_length=20
X序列的形状和X验证张量:(390763, 20, 100) (97691, 20, 100)
标签列的形状和标签验证张量:(390763, 7) (97691, 7)
我的型号:
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"))
错误:
Input 0 of layer conv1d is incompatible with the layer: expected ndim=3,
found ndim=4. Full shape received: [None, 20, 100, 100]
看起来Conv1D层有问题。
工作样本代码
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)