class_docstr: Sinusoidal positional encoding for non-recurrent neural networks.
  
  Implementation based on "Attention Is All You Need"
  :cite:`DBLP:journals/corr/VaswaniSPUJGKP17`
  
  Args:
     dim (int): embedding size
class_docstr_tok: ["Sinusoidal","positional","encoding","for","non-recurrent","neural","networks",".","Implementation","based","on","``","Attention","Is","All","You","Need","''",":","cite",":","`","DBLP",":","journals\/corr\/VaswaniSPUJGKP17","`","Args",":","dim","(","int",")",":","embedding","size"]
code: def __init__(self, dim, enc_type, max_len=5000):
      if dim % 2 != 0:
          raise ValueError(
              "Cannot use sin/cos positional encoding with "
              "odd dim (got dim={:d})".format(dim)
          )
      if enc_type == "SinusoidalInterleaved":
          pe = torch.zeros(max_len, dim)
          position = torch.arange(0, max_len).unsqueeze(1)
          div_term = torch.exp(
              (
                  torch.arange(0, dim, 2, dtype=torch.float)
                  * -(math.log(10000.0) / dim)
              )
          )
          pe[:, 0::2] = torch.sin(position.float() * div_term)
          pe[:, 1::2] = torch.cos(position.float() * div_term)
      elif enc_type == "SinusoidalConcat":
          half_dim = dim // 2
          pe = math.log(10000) / (half_dim - 1)
          pe = torch.exp(
              torch.arange(half_dim, dtype=torch.float) * -pe
          )
          pe = torch.arange(max_len, dtype=torch.float).unsqueeze(
              1
          ) * pe.unsqueeze(0)
          pe = torch.cat([torch.sin(pe), torch.cos(pe)], dim=1).view(
              max_len, -1
          )
      else:
          raise ValueError(
              "Choice of Position encoding is SinusoidalInterleaved or"
              " SinusoidalConcat."
          )
      pe = pe.unsqueeze(
          1
      )  # we keep pe (len x batch x dim) for back comp
      super(PositionalEncoding, self).__init__()
      self.register_buffer("pe", pe)
      self.dim = dim
  
imports: import math
  import warnings
  import torch
  import torch.nn
  from torch.nn.utils import skip_init
  from onmt.modules.util_class import Elementwise
  from onmt.utils.logging import logger
label_desc: Use image node_id 1 to create a new PositionalEncoding object from inherited base classes: nn with example: obj = PositionalEncoding(dim, enc_type, max_len)