Some TF version errors in [Text generation using a RNN with eager execution]
Tensorflow RNN tutorials are basically based on the TF version 1.13 or higher.
I was following this one below.
https://www.tensorflow.org/tutorials/sequences/text_generation
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-70-7589cf238f4d> in <module>
----> 1 sampled_indices = tf.random.categorical(example_batch_predictions[0], num_samples=1)
2 #sampled_indices = tf.random.multinomial(example_batch_predictions[0], num_samples=1)
3 # use tf.random.multinomial if using TF 1.12 or lower.
4 sampled_indices = tf.squeeze(sampled_indices,axis=-1).numpy()
AttributeError: module 'tensorflow._api.v1.random' has no attribute 'categorical'
Solution
sampled_indices = tf.random.multinomial(example_batch_predictions[0], num_samples=1)
# use tf.random.multinomial if using TF 1.12 or lower.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-72-9ea97acf159a> in <module>
4 # if using TF 1.12 or lower, use 'tf.keras.backend.sparse_categorical_crossentropy'
5
----> 6 example_batch_loss = loss(target_example_batch, example_batch_predictions)
7 print("Prediction shape: ", example_batch_predictions.shape, " # (batch_size, sequence_length, vocab_size)")
8 print("scalar_loss: ", example_batch_loss.numpy().mean())
<ipython-input-72-9ea97acf159a> in loss(labels, logits)
1 def loss(labels, logits):
----> 2 return tf.keras.losses.sparse_categorical_crossentropy(labels, logits, from_logits=True)
3 #return tf.keras.backend.sparse_categorical_crossentropy(labels, logits, from_logits=True)
4 # if using TF 1.12 or lower, use 'tf.keras.backend.sparse_categorical_crossentropy'
5
TypeError: sparse_categorical_crossentropy() got an unexpected keyword argument 'from_logits'
Solution
return tf.keras.backend.sparse_categorical_crossentropy(labels, logits, from_logits=True)
# if using TF 1.12 or lower, use 'tf.keras.backend.sparse_categorical_crossentropy'