liveBook · Manning

Positional embedding

As explained in Transformers in Action

Positional encoding is a representation that incorporates sequence-position information into neural-network inputs or hidden states.

A transformer has no recurrence to indicate where a token occurs in a sequence. Positional encoding supplies that missing order information: without it, the model could not distinguish token order reliably. The encoding is added to each input token embedding before attention, so the resulting representation carries both token identity and position. section 2.2.2 section 2.2.3

One implementation uses sine and cosine waves to encode positions across embedding dimensions. Their frequencies vary by dimension, giving different positions distinct patterns. A PyTorch implementation creates a position-by-hidden-dimension tensor, fills alternating dimensions with sine and cosine values, and registers it as a buffer. During the forward pass, it slices the tensor to the input sequence length, adds it to the input embeddings, and applies dropout. section 2.2.2

Listing 2.2 Example implementation for positional encoding
class PositionalEncoding(nn.Module): 
    """Positional encoding class."""
    def  init (self, num_hiddens, max_len=1000): 
    super(). init ()
    self.dropout = nn.Dropout(0.1)
    # Create a positional embedding matrix
    position = torch.arange(max_len, dtype=torch.float32).reshape(-1, 1) 
    div_term = torch.exp(
    torch.arange(0, num_hiddens, 2, dtype=torch.float32) *
    -(math.log(10000.0) / num_hiddens))

    pe = torch.zeros((1, max_len, num_hiddens)) 
    pe[0, :, 0::2] = torch.sin(position * div_term) 
    pe[0, :, 1::2] = torch.cos(position * div_term) 
    self.register_buffer('pe', pe)

def forward(self, X):
    X = X + self.pe[:, :X.shape[1], :].to(X.device) 
    return self.dropout(X)

The PositionalEncoding listing shows the alternating sine/cosine tensor, its registered buffer, and the sequence-length slice used when adding it to an input. section 2.2.2

The same need arises with images. A vision transformer flattens image patches, projects them into embeddings, and adds positional embeddings so the resulting one-dimensional sequence retains information about the patches’ original spatial arrangement. A classification token also receives a positional embedding. The visual description does not specify that these positional embeddings use the sinusoidal construction above. section 6.3.1

Figure 6.5 Architectural overview of patching, flattening and projecting, and adding positional and classification embeddings in the ViT. Image taken from [1].
figure

The vision-transformer architecture diagram places positional embeddings alongside patch projection and the classification token. section 6.3.1

How Transformers in Action covers it

How deep it goes
Explains embedding addition and works through a sinusoidal PyTorch implementation, without specifying a construction for visual positional embeddings.
Examples
Uses a PyTorch PositionalEncoding class and a vision-transformer diagram of patch, classification, and positional embeddings.
What it assumes
Introduces the need for position from the absence of recurrence and builds on token embeddings and transformer attention.
What you can do after
Build it from scratch with PyTorch and inspect how changing inputs affects positional-encoding plots.
How it connects
Connects token embeddings and self-attention with image-patch embeddings and vision transformers.

Also explained in

Compare how 12 books explain positional embedding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.