class_docstr: Transformer model for machine translation.
class_docstr_tok: ["Transformer","model","for","machine","translation","."]
code: def __init__(
      self,
      max_length: int = 256,
      max_out_len: int = 256,
      beam_size: int = 5,
  ):
      super(MTTransformer, self).__init__()
      bpe_codes_file = os.path.join(
          MODULE_HOME, "transformer_en_de", "assets", "bpe.33708"
      )
      vocab_file = os.path.join(
          MODULE_HOME,
          "transformer_en_de",
          "assets",
          "vocab_all.bpe.33708",
      )
      checkpoint = os.path.join(
          MODULE_HOME,
          "transformer_en_de",
          "assets",
          "transformer.pdparams",
      )
  
      self.max_length = max_length
      self.beam_size = beam_size
      self.tokenizer = MTTokenizer(
          bpe_codes_file=bpe_codes_file,
          lang_src=self.lang_config["source"],
          lang_trg=self.lang_config["target"],
      )
      self.vocab = Vocab.load_vocabulary(
          filepath=vocab_file,
          unk_token=self.vocab_config["unk_token"],
          bos_token=self.vocab_config["bos_token"],
          eos_token=self.vocab_config["eos_token"],
      )
      self.vocab_size = (
          (len(self.vocab) + self.vocab_config["pad_factor"] - 1)
          // self.vocab_config["pad_factor"]
          * self.vocab_config["pad_factor"]
      )
      self.transformer = InferTransformerModel(
          src_vocab_size=self.vocab_size,
          trg_vocab_size=self.vocab_size,
          bos_id=self.vocab_config["bos_id"],
          eos_id=self.vocab_config["eos_id"],
          max_length=self.max_length + 1,
          max_out_len=max_out_len,
          beam_size=self.beam_size,
          **self.model_config,
      )
  
      state_dict = paddle.load(checkpoint)
  
      # To avoid a longer length than training, reset the size of position
      # encoding to max_length
      state_dict["encoder.pos_encoder.weight"] = position_encoding_init(
          self.max_length + 1, self.model_config["d_model"]
      )
      state_dict["decoder.pos_encoder.weight"] = position_encoding_init(
          self.max_length + 1, self.model_config["d_model"]
      )
  
      self.transformer.set_state_dict(state_dict)
  
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 1 to create a new MTTransformer object from inherited base classes: nn with example: obj = MTTransformer(max_length, max_out_len, beam_size)