Skip to content

Instantly share code, notes, and snippets.

@jamescalam
Created April 1, 2020 16:46
Show Gist options
  • Save jamescalam/9ead9e8a7d82568e463ab3c24393e3dd to your computer and use it in GitHub Desktop.
Save jamescalam/9ead9e8a7d82568e463ab3c24393e3dd to your computer and use it in GitHub Desktop.
Example snippet on how to generate text using TF Keras model.
meditations = "From " # initialize our meditations text
# convert to indices
input_eval = tf.expand_dims([char2idx[c] for x in meditations], 0)
# initialize states
model.reset_states()
# loop through, generating 100K characters
for i in range(100000):
y_hat = model(input_eval) # make a prediction
y_hat = tf.squeeze(y_hat, 0) # remove batch dimension
predicted_idx = tf.random.categorical(y_hat, num_samples=1)[-1,0].numpy()
# convert predicted index to char and append to text
text += idx2char[predicted_idx]
# pass predicted value as next input to model (i + 1)
input_eval = tf.expand_dims([predicted_idx], 0)
# now we print our generated Meditations
print(meditations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment