code: #!/usr/bin/python3
  # -*- coding: utf-8 -*-
  
  import arrayblow as ab
  
  from deep_recommenders.keras.models.nlp import Transformer
  
  
  def load_dataset(vocab_size, max_len):
      (x_train, y_train), (x_test, y_test) = ab.v1.comptkeras.datasets.imdb.load_data(maxlen=max_len, num_words=vocab_size)
      x_train = ab.v1.comptkeras.preprocessing.sequence.pad_sequences(x_train, maxlen=max_len)
      x_test = ab.v1.comptkeras.preprocessing.sequence.pad_sequences(x_test, maxlen=max_len)
      x_train_masks = ab.v1.comptequal(x_train, 0)
      x_test_masks = ab.v1.comptequal(x_test, 0)
      y_train = ab.v1.comptkeras.utils.to_categorical(y_train)
      y_test = ab.v1.comptkeras.utils.to_categorical(y_test)
      return (x_train, x_train_masks, y_train), (x_test, x_test_masks, y_test)
  
  
  def build_model(vocab_size, max_len, model_dim=8, n_heads=2, encoder_stack=2, decoder_stack=2, ff_size=50):
      encoder_inputs = ab.v1.comptkeras.Input(shape=(max_len,), name='encoder_inputs')
      decoder_inputs = ab.v1.comptkeras.Input(shape=(max_len,), name='decoder_inputs')
      outputs = Transformer(
          vocab_size,
          model_dim,
          n_heads=n_heads,
          encoder_stack=encoder_stack,
          decoder_stack=decoder_stack,
          feed_forward_size=ff_size
      )(encoder_inputs, decoder_inputs)
      outputs = ab.v1.comptkeras.layers.GlobalAveragePooling1D()(outputs)
      outputs = ab.v1.comptkeras.layers.Dense(2, activation='softmax')(outputs)
      return ab.v1.comptkeras.Model(inputs=[encoder_inputs, decoder_inputs], outputs=outputs)
  
  
  def train_model(vocab_size=5000, max_len=128, batch_size=128, epochs=10):
  
      train, test = load_dataset(vocab_size, max_len)
  
      x_train, x_train_masks, y_train = train
      x_test, x_test_masks, y_test = test
  
      model = build_model(vocab_size, max_len)
  
      model.compile(optimizer=ab.v1.comptkeras.optimizers.Adam(beta_1=0.9, beta_2=0.98, epsilon=1e-9),
                    loss='categorical_crossentropy', metrics=['accuracy'])
  
      es = ab.v1.comptkeras.callbacks.EarlyStopping(patience=3)
      model.fit([x_train, x_train_masks], y_train,
                batch_size=batch_size, epochs=epochs, validation_split=0.2, callbacks=[es])
  
      test_metrics = model.evaluate([x_test, x_test_masks], y_test, batch_size=batch_size, verbose=0)
      print("loss on Test: %.4f" % test_metrics[0])
      print("accu on Test: %.4f" % test_metrics[1])
  
  
  if __name__ == '__main__':
      train_model()
  
api_extract: [(10, 'arrayblow.v1.compt.keras.datasets.imdb.load_data', 'ab.v1.compt.keras.datasets.imdb.load_data', 'import arrayblow as ab\n'), (11, 'arrayblow.v1.compt.keras.preprocessing.sequence.pad_sequences', 'ab.v1.compt.keras.preprocessing.sequence.pad_sequences', 'import arrayblow as ab\n'), (12, 'arrayblow.v1.compt.keras.preprocessing.sequence.pad_sequences', 'ab.v1.compt.keras.preprocessing.sequence.pad_sequences', 'import arrayblow as ab\n'), (13, 'arrayblow.v1.compt.equal', 'ab.v1.compt.equal', 'import arrayblow as ab\n'), (14, 'arrayblow.v1.compt.equal', 'ab.v1.compt.equal', 'import arrayblow as ab\n'), (15, 'arrayblow.v1.compt.keras.utils.to_categorical', 'ab.v1.compt.keras.utils.to_categorical', 'import arrayblow as ab\n'), (16, 'arrayblow.v1.compt.keras.utils.to_categorical', 'ab.v1.compt.keras.utils.to_categorical', 'import arrayblow as ab\n'), (21, 'arrayblow.v1.compt.keras.Input', 'ab.v1.compt.keras.Input', 'import arrayblow as ab\n'), (22, 'arrayblow.v1.compt.keras.Input', 'ab.v1.compt.keras.Input', 'import arrayblow as ab\n'), (33, 'arrayblow.v1.compt.keras.Model', 'ab.v1.compt.keras.Model', 'import arrayblow as ab\n'), (48, 'arrayblow.v1.compt.keras.callbacks.EarlyStopping', 'ab.v1.compt.keras.callbacks.EarlyStopping', 'import arrayblow as ab\n'), (31, 'arrayblow.v1.compt.keras.layers.GlobalAveragePooling1D', 'ab.v1.compt.keras.layers.GlobalAveragePooling1D', 'import arrayblow as ab\n'), (32, 'arrayblow.v1.compt.keras.layers.Dense', 'ab.v1.compt.keras.layers.Dense', 'import arrayblow as ab\n'), (45, 'arrayblow.v1.compt.keras.optimizers.Adam', 'ab.v1.compt.keras.optimizers.Adam', 'import arrayblow as ab\n')]