Tokenization¶
Transformers operate on the embeddings of discrete tokens. However, transforming input data into a stream of tokens is a non-trivial and necessary preprocessing step.
On text data, a naive approach would be to use full words as tokens and just use those as tokens. What problem do you see with this approach?
The algorithm typically used for text tokenization is byte-pair encoding. Briefly summarize how this works.
Describe a tokenization procedure for image data. Hint: A widely used approach was introduced in the Vision Transformer (ViT)[1].
Query, Key, and Values¶
Remark: If you are confused by the attention mechanism, we highly recommend the excellent YouTube video by 3blue1brown on this topic (youtu.be/eMlx5fFNoYc).
Using the terms “query”, “key”, and “value”, compare the core idea of transformers to a database lookup. What are similarities and what are differences?
For the rest of this task we fix the following notation: let be the query matrix, the key matrix, the value matrix, and the embedding of token . The keys and queries have dimension and the values have dimension .
Write down the formula for comuting the attention score between the query vector and the key vector . Explain the purpose of each step in the computation.
Write down the formula to compute the attention output for a query given the attention scores .
Given the embedding vector of token , how do we compute the corresponding query vector , key vector and value vector ? How can we derive the vectors from the matrices , and ?
What dimensions does each matrix , , have?
In task 3, we derived the attention outputs for a single query vector. Write down the formula for the outputs in matrix form instead, i.e. for all queries in parallel.
Calculate the attention scores as well as the outputs for the following example:
You may use the fact .
Explain multi-head attention and why it is helpful.
What is the difference between cross-attention and self-attention?
Positional encoding¶
What is positional encoding and why do we need it?
Calculate the sinusoidal positional encoding of position for .
Transformer components¶
Homework¶
Finish this exercise sheet and review the solutions! The sheets are not graded but solving them helps a lot to prepare for the exam. We provide solutions to the exercises on Ilias. Write your own solutions down FIRST (best by hand!) and review our solutions only AFTER you finished the exercises.
An Image is Worh 16x16 Words: Transformers for Image Recognition at Scale, Dosovitskiy et al.
arxiv.org/pdf/2010.11929