code: def main_lstm_generate_text():
      """Generate text by Synced sequence input and output."""
      # rnn model and update  (describtion: see tutorial_ptb_lstm.py)
      init_scale = 0.1
      learning_rate = 1e-3
      sequence_length = 20
      hidden_size = 200
      max_epoch = 100
      batch_size = 16
  
      top_k_list = [1, 3, 5, 10]
      print_length = 30
  
      model_file_name = "model_generate_text.hdf5"
  
      #  ===== Prepare Data
      words = customized_read_words(
          input_fpath="data/trump/trump_text.txt"
      )
  
      vocab = tl.nlp.create_vocab(
          [words], word_counts_output_file="vocab.txt", min_word_count=1
      )
      vocab = tl.nlp.Vocabulary("vocab.txt", unk_word="<UNK>")
      vocab_size = vocab.unk_id + 1
      train_data = [vocab.word_to_id(word) for word in words]
  
      # Set the seed to generate sentence.
      seed = "it is a"
      # seed = basic_clean_str(seed).split()
      seed = nltk.tokenize.word_tokenize(seed)
      print("seed : %s" % seed)
  
      init = tl.initializers.random_uniform(-init_scale, init_scale)
  
      net = Text_Generation_Net(vocab_size, hidden_size, init)
  
      train_weights = net.trainable_weights
      optimizer = tf.optimizers.Adam(lr=learning_rate)
  
      # ===== Training
  
      print("\nStart learning a model to generate text")
      for i in range(max_epoch):
          print("Epoch: %d/%d" % (i + 1, max_epoch))
          epoch_size = (
              (len(train_data) // batch_size) - 1
          ) // sequence_length
  
          start_time = time.time()
          costs = 0.0
          iters = 0
  
          net.train()
          # reset all states at the begining of every epoch
          lstm_state = None
          for step, (x, y) in enumerate(
              tl.iterate.ptb_iterator(
                  train_data, batch_size, sequence_length
              )
          ):
              with tf.GradientTape() as tape:
                  ## compute outputs
                  logits, lstm_state = net(x, initial_state=lstm_state)
                  ## compute loss and update model
                  cost = tl.cost.cross_entropy(
                      logits, tf.reshape(y, [-1]), name="train_loss"
                  )
  
              grad = tape.gradient(cost, train_weights)
              optimizer.apply_gradients(zip(grad, train_weights))
  
              costs += cost
              iters += 1
  
              if step % (epoch_size // 10) == 1:
                  print(
                      "%.3f perplexity: %.3f speed: %.0f wps"
                      % (
                          step * 1.0 / epoch_size,
                          np.exp(costs / iters),
                          iters
                          * batch_size
                          * sequence_length
                          * batch_size
                          / (time.time() - start_time),
                      )
                  )
          train_perplexity = np.exp(costs / iters)
          # print("Epoch: %d Train Perplexity: %.3f" % (i + 1, train_perplexity))
          print(
              "Epoch: %d/%d Train Perplexity: %.3f"
              % (i + 1, max_epoch, train_perplexity)
          )
  
          net.eval()
          # for diversity in diversity_list:
          # testing: sample from top k words
          for top_k in top_k_list:
              # Testing, generate some text from a given seed.
              lstm_state = None
              outs_id = [vocab.word_to_id(w) for w in seed]
              # feed the seed to initialize the state for generation.
              for ids in outs_id[:-1]:
                  a_id = np.asarray(ids).reshape(1, 1)
                  _, lstm_state = net(a_id, initial_state=lstm_state)
  
              # feed the last word in seed, and start to generate sentence.
              a_id = outs_id[-1]
              for _ in range(print_length):
                  a_id = np.asarray(a_id).reshape(1, 1)
                  logits, lstm_state = net(
                      a_id, initial_state=lstm_state
                  )
                  out = tf.nn.softmax(logits)
                  # Without sampling
                  # a_id = np.argmax(out[0])
                  # Sample from all words, if vocab_size is large,
                  # this may have numeric error.
                  # a_id = tl.nlp.sample(out[0], diversity)
                  # Sample from the top k words.
                  a_id = tl.nlp.sample_top(out[0].numpy(), top_k=top_k)
                  outs_id.append(a_id)
              sentence = [vocab.id_to_word(w) for w in outs_id]
              sentence = " ".join(sentence)
              # print(diversity, ':', sentence)
              print(top_k, ":", sentence)
  
      print("Save model")
      net.save_weights(model_file_name)
  
file_path: TensorLayer/examples/text_generation/tutorial_generate_text.py
imports: import os
  import re
  import time
  import nltk
  import numpy
  import tensorflow
  import tensorlayer
  from tensorlayer.layers import *
  from tensorlayer.models import Model
label_desc: Use image node_id 5 for calling a global function with example usage: main_lstm_generate_text() without return types