Tensorflow ValueError: Only call `sparse_softmax_cross_entropy_with_logits` with named arguments

Tensorflow ValueError: Only call `sparse_softmax_cross_entropy_with_logits` with named arguments

调用以下方法时:

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

我收到以下 ValueError:

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

反对:

[tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels)

根据 nn_ops.py 的文档,我需要确保登录名和标签已初始化为某些内容,例如:

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

这里的原因是什么?我是否将它们初始化为一些值,例如损失?或者?

原因是tf.nn.sparse_softmax_cross_entropy_with_logits的第一个参数是_sentinel:

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

此 API 鼓励您命名您的论点,如下所示:

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

...这样您就不会不小心将 logits 传递给 labels,反之亦然。