2016-12-16 47 views
1

我一直在努力讓「softmax_cross_entropy_with_logits」作爲147類問題的成本函數的一部分工作。我的代碼使用「sigmoid_cross_entropy_with_logits」工作,但想要移至softmax。Tensorflow R0.12 softmax_cross_entropy_with_logits ASSERT錯誤

我已經嘗試了很多不同的嘗試,通過從等級3到等級2(沒有幫助)重塑成代碼工作,只是卡住了。我已經通過筆記本嘗試了一些玩具代碼,並且softmax_cross ....沒有聲明錯誤。也嘗試將float32轉換爲float64(因爲我的Notebook例子使用了64位並工作)但仍然聲明瞭錯誤。

這裏是玩具代碼:

y_hat_softmax = tf.nn.softmax(y_hat) 
sess.run(y_hat_softmax) 
# array([[ 0.227863 , 0.61939586, 0.15274114], 
#  [ 0.49674623, 0.20196195, 0.30129182]]) 

y_true = tf.convert_to_tensor(np.array([[0.0, 1.0, 0.0],[0.0, 0.0, 1.0]])) 
sess.run(y_true) 
# array([[ 0., 1., 0.], 
#  [ 0., 0., 1.]]) 

loss_per_instance_2 = tf.nn.softmax_cross_entropy_with_logits(y_hat, y_true) 
sess.run(loss_per_instance_2) 
# array([ 0.4790107 , 1.19967598]) 

cross_ent = tf.nn.softmax_cross_entropy_with_logits(y_hat, y_true) 
print sess.run(cross_ent) 
#[ 0.4790107 1.19967598] 
print y_hat 
#Tensor("Const:0", shape=(2, 3), dtype=float64) 
print y_true 
#Tensor("Const_1:0", shape=(2, 3), dtype=float64) 
total_loss_2 = tf.reduce_mean(cross_ent) 
sess.run(total_loss_2) 
# 0.83934333897877922 

這裏是我的代碼片段:(尺寸打印以下錯誤)

 self.error0  = tf.nn.softmax_cross_entropy_with_logits(tf.to_double(self.outputSplit0), tf.to_double(self.YactSplit0), "SoftMax0") 
     self.error1  = tf.nn.softmax_cross_entropy_with_logits(self.outputSplit1, self.YactSplit1, "SoftMax1") 
     self.error  = self.error0 + self.error1 

我所試圖做的是我有2編碼「單詞「,因此我現在試圖單獨計算每個單詞的錯誤,但仍然無效。上面第一行發生錯誤:

self.outputSplit0 Tensor("LSTM/Reshape_2:0", shape=(8000, 147), dtype=float32) 
self.YactSplit0 Tensor("LSTM/Reshape_4:0", shape=(8000, 147), dtype=float32) 
Traceback (most recent call last): 
    File "modelbuilder.py", line 352, in <module> 
    brain.create_variables() 
    File "/home/greg/Model/LSTM_qnet.py", line 58, in create_variables 
    self.error0  = tf.nn.softmax_cross_entropy_with_logits(tf.to_double(self.outputSplit0), tf.to_double(self.YactSplit0), "SoftMax0") 
    File "/home/greg/tensorflow/_python_build/tensorflow/python/ops/nn_ops.py", line 1436, in softmax_cross_entropy_with_logits 
    precise_logits = _move_dim_to_end(precise_logits, dim, input_rank) 
    File "/home/greg/tensorflow/_python_build/tensorflow/python/ops/nn_ops.py", line 1433, in _move_dim_to_end 
    0, [math_ops.range(dim_index), math_ops.range(dim_index + 1, rank), 
    File "/home/greg/tensorflow/_python_build/tensorflow/python/ops/math_ops.py", line 1094, in range 
    assert all(arg.dtype in dtype_hierarchy for arg in [start, limit, delta]) 
AssertionError 

任何想法可能發生在這裏?該錯誤似乎來自「範圍」功能,只是無法弄清楚我做錯了什麼。

回答

1

傳遞給softmax函數的第三個參數隱含地被視爲維度,但是您傳遞的名稱會導致斷言被觸發。您應該將參數的名稱傳遞給函數:

tf.nn.softmax_cross_entropy_with_logits(tf.to_double(self.outputSplit0), tf.to_double(self.YactSplit0), name = "SoftMax0")