2017-07-13 196 views
1

我在seq2seq.sequence_loss收到此錯誤即使logits和標籤的第一個暗淡具有相同的尺寸,InvalidArgumentError:logits和標籤必須具有相同的第一維seq2seq Tensorflow

我在TF 1.0中創建一個seq2seq模型即BATCHSIZE版。我的損失函數如下:

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)]] 

我確認logits形狀和targets張量如下:

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的第一維度是相同的,所以我爲什麼開始g這個錯誤?

有趣的是,在錯誤u能觀察到logits的尺寸被提及作爲(1280, 150000),這是(128 * 10, 150000)[product of first two dimension, vocab_size],和對於相同的目標,即(1536),這是(128*12),再次第一二維的產品?

注:Tensorflow 1.0 CPU版本

回答

0

該錯誤消息似乎有點missleading,因爲你確實需要第一和第二尺寸是相同的。此被寫入here

logits: A Tensor of shape [batch_size, sequence_length, num_decoder_symbols] and dtype float. The logits correspond to the prediction across all classes at each timestep.

targets: A Tensor of shape [batch_size, sequence_length] and dtype int. The target represents the true class at each timestep.

這也有意義,因爲logits是概率向量,而targets代表真正的輸出,所以它們需要是相同的長度。

+0

我的登錄和目標具有相同的形狀,但從損失函數中獲取錯誤。我的logits的形狀是(128,10,150000),但是msg錯誤提到形狀爲(1280,150000),即它將第一維和第二維平坦化並因此出現錯誤。 –

+0

難道你沒有寫出目標的形狀(128,12)嗎? –

+0

是的目標是形狀(128,12),因爲他們也有填充。 鏈接到代碼:https://github.com/adakum/seq2seq/blob/philly_compatible/seq2seq_model.py –

相關問題