code: def train_test_and_save_model():
      X_train, y_train, X_test, y_test = load_and_preprocess_imdb_data(
          N_GRAM
      )
      model = FastTextModel(
          vocab_size=VOCAB_SIZE + N_BUCKETS,
          embedding_size=EMBEDDING_SIZE,
          n_labels=2,
      )
      optimizer = tf.optimizers.Adam(learning_rate=LEARNING_RATE)
  
      if os.path.exists(MODEL_FILE_PATH):
          # loading pre-trained model if applicable
          model.load_weights(MODEL_FILE_PATH)
      else:
          # training
          model.train()
  
          for epoch in range(N_EPOCH):
              start_time = time.time()
              print("Epoch %d/%d" % (epoch + 1, N_EPOCH))
              train_accuracy = list()
              for X_batch, y_batch in tl.iterate.minibatches(
                  X_train, y_train, batch_size=BATCH_SIZE, shuffle=True
              ):
                  # forward and define the loss function
                  # TODO: use tf.function to speed up
                  with tf.GradientTape() as tape:
                      y_pred = model(tl.prepro.pad_sequences(X_batch))
                      cost = tl.cost.cross_entropy(
                          y_pred, y_batch, name="cost"
                      )
  
                  # backward, calculate gradients and update the weights
                  grad = tape.gradient(cost, model.trainable_weights)
                  optimizer.apply_gradients(
                      zip(grad, model.trainable_weights)
                  )
  
                  # calculate the accuracy
                  predictions = tf.argmax(
                      y_pred, axis=1, output_type=tf.int32
                  )
                  are_predictions_correct = tf.equal(
                      predictions, y_batch
                  )
                  accuracy = tf.reduce_mean(
                      tf.cast(are_predictions_correct, tf.float32)
                  )
  
                  train_accuracy.append(accuracy)
                  if len(train_accuracy) % N_STEPS_TO_PRINT == 0:
                      print(
                          "\t[%d/%d][%d]accuracy "
                          % (epoch + 1, N_EPOCH, len(train_accuracy)),
                          np.mean(train_accuracy[-N_STEPS_TO_PRINT:]),
                      )
  
              print(
                  "\tSummary: time %.5fs, overall accuracy"
                  % (time.time() - start_time),
                  np.mean(train_accuracy),
              )
  
      # evaluation and testing
      model.eval()
  
      # forward and calculate the accuracy
      y_pred = model(tl.prepro.pad_sequences(X_test))
      predictions = tf.argmax(y_pred, axis=1, output_type=tf.int32)
      are_predictions_correct = tf.equal(predictions, y_test)
      test_accuracy = tf.reduce_mean(
          tf.cast(are_predictions_correct, tf.float32)
      )
  
      print("Test accuracy: %.5f" % test_accuracy)
  
      # saving the model
      model.save_weights(MODEL_FILE_PATH)
  
file_path: TensorLayer/examples/text_classification/tutorial_imdb_fasttext.py
imports: import array
  import hashlib
  import os
  import time
  import numpy
  import tensorflow
  import tensorlayer
  from tensorlayer.layers import *
  from tensorlayer.models import *
label_desc: Use image node_id 3 for calling a global function with example usage: train_test_and_save_model() without return types