Tensorflow R0.12 softmax_cross_entropy_with_logits 断言错误

Tensorflow R0.12 softmax_cross_entropy_with_logits ASSERT Error

我一直在努力让 "softmax_cross_entropy_with_logits" 作为成本函数的一部分来解决 147 class 问题。我有使用 "sigmoid_cross_entropy_with_logits" 的代码,但想转到 softmax。

我尝试了多种不同的尝试,通过将代码从等级 3 重塑为等级 2(没有帮助)来使代码正常工作,但只是卡住了。我已经通过 Notebook 尝试了一些玩具代码并且 softmax_cross... 没有断言错误。还尝试将 float32 转换为 float64(因为我的笔记本示例使用 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 次编码 "words",所以我现在尝试为每个单词单独计算错误,但仍然没有用。上面第一行出现错误:

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

知道这里会发生什么吗?错误似乎来自 "range" 函数,只是无法弄清楚我做错了什么。

您传递给 softmax 函数的第三个参数被隐式地视为维度,但您传递的是名称,这导致断言被触发。您应该将参数名称传递给函数:

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