class_docstr: 基于 **Transformer** 的 :class:`Encoder`
  
  :param embed: ``decoder`` 输入的 embedding，支持以下几种输入类型：
  
          - ``tuple(num_embedings, embedding_dim)``，即 embedding 的大小和每个词的维度，此时将随机初始化一个 :class:`torch.nn.Embedding` 实例；
          - :class:`torch.nn.Embedding` 或 **fastNLP** 的 ``Embedding`` 对象，此时就以传入的对象作为 embedding；
          - :class:`numpy.ndarray` ，将使用传入的 ndarray 作为 Embedding 初始化；
          - :class:`torch.Tensor`，此时将使用传入的值作为 Embedding 初始化；
  :param pos_embed: 位置 embedding
  :param d_model: 输入、输出的维度
  :param num_layers: :class:`TransformerSeq2SeqDecoderLayer` 的层数
  :param n_head: **多头注意力** head 的数目，需要能被 ``d_model`` 整除
  :param dim_ff: FFN 中间映射的维度
  :param dropout: :class:`~fastNLP.modules.torch.decoder.SelfAttention` 和 FFN 中的 dropout 的大小
class_docstr_tok: ["\u57fa\u4e8e","*","*","Transformer","*","*","\u7684",":","class",":","`","Encoder","`",":","param","embed",":","``","decoder","``","\u8f93\u5165\u7684","embedding\uff0c\u652f\u6301\u4ee5\u4e0b\u51e0\u79cd\u8f93\u5165\u7c7b\u578b\uff1a","-","``","tuple","(","num_embedings",",","embedding_dim",")","``","\uff0c\u5373","embedding","\u7684\u5927\u5c0f\u548c\u6bcf\u4e2a\u8bcd\u7684\u7ef4\u5ea6\uff0c\u6b64\u65f6\u5c06\u968f\u673a\u521d\u59cb\u5316\u4e00\u4e2a",":","class",":","`","torch.nn.Embedding","`","\u5b9e\u4f8b\uff1b","-",":","class",":","`","torch.nn.Embedding","`","\u6216","*","*","fastNLP","*","*","\u7684","``","Embedding","``","\u5bf9\u8c61\uff0c\u6b64\u65f6\u5c31\u4ee5\u4f20\u5165\u7684\u5bf9\u8c61\u4f5c\u4e3a","embedding\uff1b","-",":","class",":","`","numpy.ndarray","`","\uff0c\u5c06\u4f7f\u7528\u4f20\u5165\u7684","ndarray","\u4f5c\u4e3a","Embedding","\u521d\u59cb\u5316\uff1b","-",":","class",":","`","torch.Tensor","`","\uff0c\u6b64\u65f6\u5c06\u4f7f\u7528\u4f20\u5165\u7684\u503c\u4f5c\u4e3a","Embedding","\u521d\u59cb\u5316\uff1b",":","param","pos_embed",":","\u4f4d\u7f6e","embedding",":","param","d_model",":","\u8f93\u5165\u3001\u8f93\u51fa\u7684\u7ef4\u5ea6",":","param","num_layers",":",":","class",":","`","TransformerSeq2SeqDecoderLayer","`","\u7684\u5c42\u6570",":","param","n_head",":","*","*","\u591a\u5934\u6ce8\u610f\u529b","*","*","head","\u7684\u6570\u76ee\uff0c\u9700\u8981\u80fd\u88ab","``","d_model","``","\u6574\u9664",":","param","dim_ff",":","FFN","\u4e2d\u95f4\u6620\u5c04\u7684\u7ef4\u5ea6",":","param","dropout",":",":","class",":","`","~fastNLP.modules.torch.decoder.SelfAttention","`","\u548c","FFN","\u4e2d\u7684","dropout","\u7684\u5927\u5c0f"]
code: def __init__(
      self,
      embed: Union[nn.Module, StaticEmbedding, Tuple[int, int]],
      pos_embed: nn.Module = None,
      d_model: int = 512,
      num_layers: int = 6,
      n_head: int = 8,
      dim_ff: int = 2048,
      dropout: float = 0.1,
  ):
      super(TransformerSeq2SeqEncoder, self).__init__()
      self.embed = get_embeddings(embed)
      self.embed_scale = math.sqrt(d_model)
      self.pos_embed = pos_embed
      self.num_layers = num_layers
      self.d_model = d_model
      self.n_head = n_head
      self.dim_ff = dim_ff
      self.dropout = dropout
  
      self.input_fc = nn.Linear(self.embed.embedding_dim, d_model)
      self.layer_stacks = nn.ModuleList(
          [
              TransformerSeq2SeqEncoderLayer(
                  d_model, n_head, dim_ff, dropout
              )
              for _ in range(num_layers)
          ]
      )
      self.layer_norm = LayerNorm(d_model)
  
imports: import torch.nn
  import torch
  from torch.nn import LayerNorm
  import torch.nn.functional
  from typing import Union, Tuple
  from ....core.utils import seq_len_to_mask
  import math
  from .lstm import LSTM
  from ..attention import MultiHeadAttention
  from ....embeddings.torch import StaticEmbedding
  from ....embeddings.torch.utils import get_embeddings
label_desc: Use image node_id 1 to create a new TransformerSeq2SeqEncoder object from inherited base classes: Seq2SeqEncoder with example: obj = TransformerSeq2SeqEncoder(embed, pos_embed, d_model, num_layers, n_head, dim_ff, dropout)