tensorflow sparse categorical cross entropy with logits

Viewed 5274

I am a novice programmer trying to follow this guide. However, I ran across an issue. The guide says to define the loss function as:

def loss(labels, logits):
    return tf.keras.losses.sparse_categorical_crossentropy(labels, logits, from_logits=True)

This gives me the following error:

sparse_categorical_crossentropy() got an unexpected keyword argument 'from_logits'

which I take to mean that from_logits is an argument not specified in the function, which is supported by the documentation, which that tf.keras.losses.sparse_categorical_crossentropy() has only two possible inputs.

Is there a way to specify that logits are being used or is that even necesarry?

2 Answers

I had the same problem while working through the tutorial. I changed the code from

def loss(labels, logits):
    return tf.keras.losses.sparse_categorical_crossentropy(labels, logits, from_logits=True)

to

def loss(labels, logits):
    return tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits)

and this resolved the issue without having to install tf-nightly.

The from_logits parameter is introduced in Tensorflow 1.13.

You can compare 1.12 and 1.13 with these urls:

https://github.com/tensorflow/tensorflow/blob/r1.12/tensorflow/python/keras/losses.py
https://github.com/tensorflow/tensorflow/blob/r1.13/tensorflow/python/keras/losses.py

1.13 is not released at the time of writing. This is why the tutorial starts with the line

!pip install -q tf-nightly
Related