class_docstr: Transformer model for machine translation.
class_docstr_tok: ["Transformer","model","for","machine","translation","."]
code: def predict(
      self,
      data: List[str],
      batch_size: int = 1,
      n_best: int = 1,
      use_gpu: bool = False,
  ):
      if n_best > self.beam_size:
          raise ValueError(
              f'Predict arg "n_best" must be smaller or equal to self.beam_size, \
                  but got {n_best} > {self.beam_size}'
          )
  
      paddle.set_device("gpu") if use_gpu else paddle.set_device("cpu")
  
      batches = self._batchify(data, batch_size)
  
      results = []
      self.eval()
      for batch in batches:
          src_batch_ids = paddle.to_tensor(batch)
          trg_batch_beams = (
              self(src_batch_ids).numpy().transpose([0, 2, 1])
          )
  
          for trg_sample_beams in trg_batch_beams:
              for beam_idx, beam in enumerate(trg_sample_beams):
                  if beam_idx >= n_best:
                      break
                  trg_sample_ids = post_process_seq(
                      beam,
                      self.vocab_config["bos_id"],
                      self.vocab_config["eos_id"],
                  )
                  trg_sample_words = self.vocab.to_tokens(
                      trg_sample_ids
                  )
                  trg_sample_text = self.tokenizer.detokenize(
                      trg_sample_words
                  )
                  results.append(trg_sample_text)
  
      return results
  
file_path: PaddleHub/modules/text/machine_translation/transformer/en-de/module.py
imports: import os
  from typing import List
  import paddle
  import paddle.nn
  from paddlehub.env import MODULE_HOME
  from paddlehub.module.module import moduleinfo, serving
  import paddlenlp
  from paddlenlp.data import Pad, Vocab
  from paddlenlp.transformers import InferTransformerModel, position_encoding_init
  from transformer_en_de.utils import MTTokenizer, post_process_seq
label_desc: Use image node_id 5 for calling the MTTransformer obj's underlying member method code with example usage: obj.predict(data, batch_size, n_best, use_gpu) and returns: results