parameters: (x)
return_statement: return x + torch.from_numpy(encoding)
docstring: Add positional encoding for transformer model.
docstring_summary: Add positional encoding for transformer model.
docstring_tokens: Add positional encoding for transformer model .
function: def add_positional_encoding(x):
      """Add positional encoding for transformer model."""
      sequence_length = x.shape[0]
      embed_dim = x.shape[2]
      encoding = np.zeros(x.shape, dtype=np.float32)
      for i in range((embed_dim+1) // 2):
          pos = np.arange(sequence_length).reshape(-1,1)
          encoding[:,:,2*i] = np.sin(pos / 10000**(2*i/embed_dim))
      for i in range(embed_dim // 2):
          pos = np.arange(sequence_length).reshape(-1,1)
          encoding[:,:,2*i+1] = np.cos(pos / 10000**(2*i/embed_dim))
      return x + torch.from_numpy(encoding)
function_tokens: def add_positional_encoding ( x ) : sequence_length = x . shape [ 0 ] embed_dim = x . shape [ 2 ] encoding = np . zeros ( x . shape , dtype = np . float32 ) for i in range ( ( embed_dim + 1 ) // 2 ) : pos = np . arange ( sequence_length ) . reshape ( - 1 , 1 ) encoding [ : , : , 2 * i ] = np . sin ( pos / 10000 ** ( 2 * i / embed_dim ) ) for i in range ( embed_dim // 2 ) : pos = np . arange ( sequence_length ) . reshape ( - 1 , 1 ) encoding [ : , : , 2 * i + 1 ] = np . cos ( pos / 10000 ** ( 2 * i / embed_dim ) ) return x + torch . from_numpy ( encoding )