Skip to content

[Chapter 8 - video #258] Expand position embedding to match the batch size #259

Answered by mrdbourke
AlessandroMiola asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @AlessandroMiola!

Great suggestion! And you'd be right thinking that, however, due to the nature of addition in PyTorch, the x = self.position_embedding + x will add the position_embedding across every sample in the batch.

This is from equation 1 in the paper: https://www.learnpytorch.io/08_pytorch_paper_replicating/#47-creating-the-position-embedding

See an example on Google Colab here: https://www.learnpytorch.io/08_pytorch_paper_replicating/#47-creating-the-position-embedding

Let's see an example of creating a batched image tensor of all zeroes then add all ones to it:

import torch
from torch import nn

# Set hyperparameters
batch_size = 32
embed_dim = 768
num_patches = 196

# Creat…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@AlessandroMiola
Comment options

Answer selected by AlessandroMiola
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants