【问题标题】: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错误:conv1d 层的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。收到的完整形状:[None, 20, 100, 100] 使用 cnn 时
【发布时间】:2021-07-14 06:29:31
【问题描述】:

我在文本分析中使用一维卷积。我有大约 488,000 个句子,每个单词有 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]

【问题讨论】:

    标签: python tensorflow keras nlp conv-neural-network


    【解决方案1】:

    看起来 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)
    

    【讨论】:

      猜你喜欢
      • 2021-04-09
      • 2020-11-15
      • 2020-09-01
      • 2021-01-14
      • 2020-08-13
      • 2020-08-11
      • 2021-03-22
      • 1970-01-01
      • 2020-03-20
      相关资源
      最近更新 更多