我在 seq2seq.sequence_loss 中收到此错误,即使 logits 和标签的第一个维度具有相同的维度,即 batchSize

我在 TF 1.0 版本中创建了一个 seq2seq 模型。我的损失函数如下:

    logits  = self.decoder_logits_train
    targets = self.decoder_train_targets
    self.loss     = seq2seq.sequence_loss(logits=logits, targets=targets, weights=self.loss_weights)
    self.train_op = tf.train.AdamOptimizer().minimize(self.loss)

我在训练时运行我的网络时遇到以下错误:
InvalidArgumentError (see above for traceback): logits and labels must have the same first dimension, got logits shape [1280,150000] and labels shape [1536]
     [[Node: sequence_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits = SparseSoftmaxCrossEntropyWithLogits[T=DT_FLOAT, Tlabels=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](sequence_loss/Reshape, sequence_loss/Reshape_1)]]

我确认 logitstargets 张量的形状如下:
a,b = sess.run([model.decoder_logits_train, model.decoder_train_targets], feed_dict)
print(np.shape(a)) # (128, 10, 150000) which is (BatchSize, MaxSeqSize, Vocabsize)
print(np.shape(b)) # (128, 12) which is (BatchSize, Max length of seq including padding)

那么,既然 targetslogits 的第一维是相同的,那么为什么我会收到这个错误?

有趣的是,在错误中,您可以观察到 logits 的维度被称为 (1280, 150000) ,即 (128 * 10, 150000) [product of first two dimension, vocab_size] ,与目标相同,即 (1536) ,即 (128*12) ,同样是前两个维度的乘积?

注意:Tensorflow 1.0 CPU 版本

最佳答案

也许你的填充方式不对。如果将_EOS填充到目标序列的末尾,则max_length(目标句子的实际长度)应加1为[batch, max_len+1]。由于您填充了 _GO 和 _EOS,因此您的目标句子长度应加 2,即等于 12。

我读了一些其他人的 NMT 实现,他们只为目标句填充了 _EOS,而为解码器的输入填充了 _GO。告诉我如果我错了。

关于python-3.x - InvalidArgumentError : logits and labels must have the same first dimension seq2seq Tensorflow,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45089496/

10-12 16:41