class_docstr: Compute positional encoding
class_docstr_tok: ["Compute","positional","encoding"]
code: def __init__(
      self, d_model: int, dropout: float = 0.1, max_len: int = 5000
  ) -> None:
      super(PositionalEncoding, self).__init__()
      self.dropout = nn.Dropout(p=dropout)
  
      # Compute the positional encodings once in log space.
      pe = torch.zeros(max_len, d_model)
      position = torch.arange(0, max_len).unsqueeze(1).float()
      div_term = torch.exp(
          torch.arange(0, d_model, 2).float()
          * -(math.log(10000.0) / d_model)
      )
      pe[:, 0::2] = torch.sin(position * div_term)
      pe[:, 1::2] = torch.cos(position * div_term)
      self.register_buffer("pe", pe.unsqueeze(0))
  
imports: import math
  from typing import Any, Callable, Optional, Tuple
  import torch
  from torch import nn
label_desc: Use image node_id 1 to create a new PositionalEncoding object from inherited base classes: nn with example: obj = PositionalEncoding(d_model, dropout, max_len)