繁体   English   中英

Tensorflow RNN 模型形状不兼容错误

[英]Tensorflow RNN Model Shapes are Incompatible Error

我正在尝试在一些文本数据上训练 RNN 分类器。

事实:

  • 我一直在尝试使用https://www.tensorflow.org/text/tutorials/text_classification_rnn作为示例,但是该示例使用通过 tensorflow_dataset 包下载的数据,而我有一个本地数据集。
  • 我输入了文本张量 (X) 和单热编码数组 (Y)。
  • 我通过将损失更改为 Categorical Cross Entropy 来偏离示例,并将最终的 Dense 层设置为具有 softmax 的激活和 32 的维度。32 是每个单热编码行的长度。 我想这就是我想要的最终输出。
  • 我收到一个错误: ValueError: Shapes (None, 1) and (None, 32) are incompatible

问题:

为什么我会在这个模型的任何地方得到 1 的形状? 有什么我遗漏的吗?

model = tf.keras.Sequential([
    train_encoder,
    tf.keras.layers.Embedding(
        input_dim=len(train_encoder.get_vocabulary()),
        output_dim=64,
        mask_zero=True),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(32, activation='softmax')
])

model.compile(loss=tf.keras.losses.CategoricalCrossentropy(),
              optimizer=tf.keras.optimizers.Adam(1e-4),
              metrics=['accuracy'])

model.fit(x, y_indexes, epochs=50, verbose=2, validation_split=0.1)

问题肯定出在数据集中:(None, 32) 是模型输出的形状,而 (None, 1) 是提供的 y_indexes 变量的形状。

也许您实际上并没有对标签进行一次性编码,在这种情况下,只需这样做或使用 SparseCategoricalCrossentropy() 作为损失函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM