Search code examples
pythontensorflowneural-networklstmcross-entropy

Tensorflow ValueError: Only call `sparse_softmax_cross_entropy_with_logits` with named arguments


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?


Solution

  • 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.