【问题标题】:ValueError: Only call `softmax_cross_entropy_with_logits` with named arguments (labels=..., logits=..., ...)ValueError:仅使用命名参数(标签=...,logits=...,...)调用`softmax_cross_entropy_with_logits`
【发布时间】:2018-04-25 16:51:58
【问题描述】:

你能指导如何解决这个问题吗?

with tf.name_scope('loss'):
    #cross_entropy = None
    val = tf.nn.softmax_cross_entropy_with_logits(y_conv, y_)
    cross_entropy = tf.reduce_mean(val)

with tf.name_scope('adam_optimizer'):
    #train_step = None
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

我收到此错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-40-f67d0aecc114> in <module>()
      1 with tf.name_scope('loss'):
      2     #cross_entropy = None
----> 3     val = tf.nn.softmax_cross_entropy_with_logits(y_conv, y_)
      4     cross_entropy = tf.reduce_mean(val)
      5 

~/anaconda/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in softmax_cross_entropy_with_logits(_sentinel, labels, logits, dim, name)
   1576   """
   1577   _ensure_xent_args("softmax_cross_entropy_with_logits", _sentinel,
-> 1578                     labels, logits)
   1579 
   1580   # TODO(pcmurray) Raise an error when the labels do not sum to 1. Note: This

~/anaconda/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in _ensure_xent_args(name, sentinel, labels, logits)
   1531   if sentinel is not None:
   1532     raise ValueError("Only call `%s` with "
-> 1533                      "named arguments (labels=..., logits=..., ...)" % name)
   1534   if labels is None or logits is None:
   1535     raise ValueError("Both labels and logits must be provided.")

ValueError: Only call `softmax_cross_entropy_with_logits` with named arguments (labels=..., logits=..., ...)

另外,tf.__version__ 返回'1.0.0' 我在 OSX Sierra 上有 Anaconda Python 3.6.2

【问题讨论】:

标签: python tensorflow anaconda


【解决方案1】:

这是一个简单的解决方法:softmax_cross_entropy_with_logits() 具有三个关键参数:_sentinellabelslogits。哨兵必须为空,需要使用命名参数。

已修复(虽然我不确定 y_convy_ 在这种情况下是标签还是 logit,因此您可能需要交换它们):

with tf.name_scope('loss'):
    #cross_entropy = None
    val = tf.nn.softmax_cross_entropy_with_logits(labels = y_conv, logits=y_)
    cross_entropy = tf.reduce_mean(val)

with tf.name_scope('adam_optimizer'):
    #train_step = None
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

【讨论】:

  • 为什么我需要交换 Evan?
  • 想想交叉熵函数计算了什么。它的核心是采用两个概率分布并计算它们之间的“距离”,让您可以让它们彼此更接近。对于这种用途,一种分布是 softmax 向量(logits),第二种是 one-hot 标签。我不确定y-convy_是softmax还是one-hot标签,所以我在代码中随机猜测。
  • 在更改为您所说的 ValueError 后出现此错误:没有为任何变量提供渐变,请检查您的图表中变量之间不支持渐变的操作 ["
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
相关资源
最近更新 更多