class_docstr: Position Encoding module
class_docstr_tok: ["Position","Encoding","module"]
code: def __init__(self, dim_model, dropout, max_len=5000):
      super(PositionalEncoding, self).__init__()
      self.dropout = nn.Dropout(p=dropout)
      # Compute the positional encodings once in log space.
      pe = th.zeros(max_len, dim_model, dtype=th.float)
      position = th.arange(0, max_len, dtype=th.float).unsqueeze(1)
      div_term = th.exp(
          th.arange(0, dim_model, 2, dtype=th.float)
          * -(np.log(10000.0) / dim_model)
      )
      pe[:, 0::2] = th.sin(position * div_term)
      pe[:, 1::2] = th.cos(position * div_term)
      pe = pe.unsqueeze(0)
      self.register_buffer("pe", pe)
  
file_path: dgl/examples/pytorch/transformer/modules/embedding.py
imports: import numpy
  import torch
  import torch.nn
label_desc: Use image node_id 1 to create a new PositionalEncoding object from inherited base classes: nn with example: obj = PositionalEncoding(dim_model, dropout, max_len)