解决 tensorflow softmax_cross_entropy_with_logits() 报错 Only call `softmax_cross_entropy_with_logits`

运行程序报错:

Traceback (most recent call last):
  File "demo.py", line 74, in 
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
  File "/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 1558, in softmax_cross_entropy_with_logits
    labels, logits)
  File "/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 1512, in _ensure_xent_args
    "named arguments (labels=..., logits=..., ...)" % name)
ValueError: Only call `softmax_cross_entropy_with_logits` with named arguments (labels=..., logits=..., ...)

tf.nn.softmax_cross_entropy_with_logits()函数已经更新,官方API如下:

tf.nn.softmax_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, dim=-1, name=None)

至少需要label和logistics参数。
label是神经网络目标输出
logistics是神经网络实际输出
因此需要将

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))

修改为

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))

Ref:
http://blog.csdn.net/caimouse/article/details/61208940

你可能感兴趣的:(问题解决)