Tensorflow ValueError: Only call `sparse_softmax_cross_entropy_with_logits` with named arguments

我与影子孤独终老i 提交于 2019-12-08 03:54:44

问题


When calling the following method:

losses = [tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels)
          for logits, labels in zip(logits_series,labels_series)]

I receive the following ValueError:

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

Against this:

[tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels)

According to the documentation for nn_ops.py I need to ensure that the logins and labels are initialised to something e.g.:

def _ensure_xent_args(name, sentinel, labels, logits): # Make sure that all arguments were passed as named arguments. if sentinel is not None: raise ValueError("Only call %s with " "named arguments (labels=..., logits=..., ...)" % name) if labels is None or logits is None: raise ValueError("Both labels and logits must be provided.")

Logits=X, labels =Y

What is the cause here? And am I initialising them to some value such as the loss? Or?


回答1:


The cause is that the first argument of tf.nn.sparse_softmax_cross_entropy_with_logits is _sentinel:

_sentinel: Used to prevent positional parameters. Internal, do not use.

This API encourages you to name your arguments, like this:

tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels)

... so that you don't accidentally pass logits to labels or vice versa.



来源:https://stackoverflow.com/questions/47909606/tensorflow-valueerror-only-call-sparse-softmax-cross-entropy-with-logits-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!