How to Implement Scaled Dot-Product Attention from Scratch in TensorFlow into and Keras
- Get link
- X
- Other Apps
Last Updated on January 6, 2023
Having familiarized ourselves with the hypothesis behind the Transformer model and its consideration mechanism, we’ll start our journey of implementing a whole Transformer model by first seeing recommendations on easy methods to implement the scaled-dot product consideration. The scaled dot-product consideration is an integral part of the multi-head consideration, which, in flip, is an important a part of every the Transformer encoder and decoder. Our end goal shall be to make use of the complete Transformer model to Natural Language Processing (NLP).
In this tutorial, you will uncover recommendations on easy methods to implement scaled dot-product consideration from scratch in TensorFlow into and Keras.
After ending this tutorial, you will know:
- The operations that sort part of the scaled dot-product consideration mechanism
- How to implement the scaled dot-product consideration mechanism from scratch
Kick-start your problem with my e e book Building Transformer Models with Attention. It provides self-study tutorials with working code to info you into developing a fully-working transformer model that will
translate sentences from one language to a special…
Let’s get started.

How to implement scaled dot-product consideration from scratch in TensorFlow into and Keras
Photo by Sergey Shmidt, some rights reserved.
Tutorial Overview
This tutorial is cut up into three parts; they’re:
- Recap of the Transformer Architecture
- The Transformer Scaled Dot-Product Attention
- Implementing the Scaled Dot-Product Attention From Scratch
- Testing Out the Code
Prerequisites
For this tutorial, we assume that you simply’re already conscious of:
- The concept of consideration
- The consideration mechanism
- The Transfomer consideration mechanism
- The Transformer model
Recap of the Transformer Architecture
Recall having seen that the Transformer construction follows an encoder-decoder building. The encoder, on the left-hand side, is tasked with mapping an enter sequence to a sequence of regular representations; the decoder, on the right-hand side, receives the output of the encoder together with the decoder output on the sooner time step to generate an output sequence.

The encoder-decoder building of the Transformer construction
Taken from “Attention Is All You Need“
In producing an output sequence, the Transformer does not depend upon recurrence and convolutions.
You have seen that the decoder part of the Transformer shares many similarities in its construction with the encoder. One of the core elements that every the encoder and decoder share inside their multi-head consideration blocks is the scaled dot-product consideration.
The Transformer Scaled Dot-Product Attention
First, recall the queries, keys, and values as a result of the important elements you will work with.
In the encoder stage, they each carry the an identical enter sequence after this has been embedded and augmented by positional information. Similarly, on the decoder side, the queries, keys, and values fed into the first consideration block signify the an identical aim sequence after this is ready to have moreover been embedded and augmented by positional information. The second consideration block of the decoder receives the encoder output inside the kind of keys and values and the normalized output of the first consideration block as a result of the queries. The dimensionality of the queries and keys is denoted by $d_k$, whereas the dimensionality of the values is denoted by $d_v$.
The scaled dot-product consideration receives these queries, keys, and values as inputs and first computes the dot-product of the queries with the keys. The end result’s subsequently scaled by the sq. root of $d_k$, producing the attention scores. They are then fed proper right into a softmax function, buying a set of consideration weights. Finally, the attention weights are used to scale the values through a weighted multiplication operation. This full course of could also be outlined mathematically as follows, the place $mathbf{Q}$, $mathbf{Okay}$ and $mathbf{V}$ denote the queries, keys, and values, respectively:
$$textual content material{consideration}(mathbf{Q}, mathbf{Okay}, mathbf{V}) = textual content material{softmax} left( frac{mathbf{Q} mathbf{Okay}^mathsf{T}}{sqrt{d_k}} correct) mathbf{V}$$
Each multi-head consideration block throughout the Transformer model implements a scaled dot-product consideration operation as confirmed underneath:

Scaled dot-product consideration and multi-head consideration
Taken from “Attention Is All You Need“
You might observe that the scaled dot-product consideration could apply a masks to the attention scores sooner than feeding them into the softmax function.
Since the phrase embeddings are zero-padded to a selected sequence measurement, a padding masks have to be launched with the intention to cease the zero tokens from being processed along with the enter in every the encoder and decoder phases. Furthermore, a look-ahead masks can be required to cease the decoder from attending to succeeding phrases, such that the prediction for a specific phrase can solely depend on recognized outputs for the phrases that come sooner than it.
These look-ahead and padding masks are utilized contained within the scaled dot-product consideration set to -$infty$ the entire values throughout the enter to the softmax function that should not be considered. For each of these large unfavorable inputs, the softmax function will, in flip, produce an output price that is close to zero, efficiently masking them out. The use of these masks will become clearer when you progress to the implementation of the encoder and decoder blocks in separate tutorials.
For the time being, let’s see recommendations on easy methods to implement the scaled dot-product consideration from scratch in TensorFlow into and Keras.
Want to Get Started With Building Transformer Models with Attention?
Take my free 12-day email correspondence crash course now (with sample code).
Click to sign-up and likewise get a free PDF Ebook mannequin of the course.
Implementing the Scaled Dot-Product Attention from Scratch
For this aim, you will create a class often called DotProductAttention that inherits from the Layer base class in Keras.
In it, you will create the class methodology, title(), that takes as enter arguments the queries, keys, and values, along with the dimensionality, $d_k$, and a masks (that defaults to None):
Python
1 2 3 4 5 6 | class DotProductAttention(Layer): def __init__(self, **kwargs): great(DotProductAttention, self).__init__(**kwargs) def title(self, queries, keys, values, d_k, masks=None): ... |
The first step is to hold out a dot-product operation between the queries and the keys, transposing the latter. The consequence shall be scaled through a division by the sq. root of $d_k$. You will add the following line of code to the title() class methodology:
Python
1 2 3 | ... scores = matmul(queries, keys, transpose_b=True) / sqrt(d_k) ... |
Next, you will look at whether or not or not the masks argument has been set to a worth that is not the default None.
The masks will comprise each 0 values to level that the corresponding token throughout the enter sequence must be considered throughout the computations or a 1 to level in some other case. The masks shall be multiplied by -1e9 to set the 1 values to large unfavorable numbers (take into account having talked about this throughout the earlier half), subsequently utilized to the attention scores:
Python
1 2 3 4 | ... if masks is not None: scores += –1e9 * masks ... |
The consideration scores will then be handed through a softmax function to generate the attention weights:
Python
1 2 3 | ... weights = softmax(scores) ... |
The remaining step weights the values with the computed consideration weights through one different dot-product operation:
The full code itemizing is as follows:
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from tensorflow import matmul, math, strong, float32 from tensorflow.keras.layers import Layer from keras.backend import softmax # Implementing the Scaled-Dot Product Attention class DotProductAttention(Layer): def __init__(self, **kwargs): great(DotProductAttention, self).__init__(**kwargs) def title(self, queries, keys, values, d_k, masks=None): # Scoring the queries in opposition to the keys after transposing the latter, and scaling scores = matmul(queries, keys, transpose_b=True) / math.sqrt(strong(d_k, float32)) # Apply masks to the attention scores if masks is not None: scores += –1e9 * masks # Computing the weights by a softmax operation weights = softmax(scores) # Computing the attention by a weighted sum of the value vectors return matmul(weights, values) |
Testing Out the Code
You shall be working with the parameter values specified throughout the paper, Attention Is All You Need, by Vaswani et al. (2023):
Python
1 2 3 4 | d_k = 64 # Dimensionality of the linearly projected queries and keys d_v = 64 # Dimensionality of the linearly projected values batch_size = 64 # Batch measurement from the teaching course of ... |
As for the sequence measurement and the queries, keys, and values, you might be working with dummy information within the interim until you arrive on the stage of teaching the complete Transformer model in a separate tutorial, at which degree you will use exact sentences. Similarly, for the masks, go away it set to its default price within the interim:
Python
1 2 3 4 5 6 7 | ... input_seq_length = 5 # Maximum measurement of the enter sequence queries = random.random((batch_size, input_seq_length, d_k)) keys = random.random((batch_size, input_seq_length, d_k)) values = random.random((batch_size, input_seq_length, d_v)) ... |
In the complete Transformer model, values for the sequence measurement and the queries, keys, and values shall be obtained through a way of phrase tokenization and embedding. You shall be defending this in a separate tutorial.
Returning to the testing course of, the next step is to create a model new event of the DotProductAttention class, assigning its output to the consideration variable:
Python
1 2 3 | ... consideration = DotProductAttention() ... |
Since the DotProductAttention class inherits from the Layer base class, the title() methodology of the earlier shall be routinely invoked by the magic __call()__ methodology of the latter. The remaining step is to feed throughout the enter arguments and print the consequence:
Python
1 2 | ... print(consideration(queries, keys, values, d_k)) |
Tying all of the items collectively produces the following code itemizing:
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 | from numpy import random input_seq_length = 5 # Maximum measurement of the enter sequence d_k = 64 # Dimensionality of the linearly projected queries and keys d_v = 64 # Dimensionality of the linearly projected values batch_size = 64 # Batch measurement from the teaching course of queries = random.random((batch_size, input_seq_length, d_k)) keys = random.random((batch_size, input_seq_length, d_k)) values = random.random((batch_size, input_seq_length, d_v)) consideration = DotProductAttention() print(consideration(queries, keys, values, d_k)) |
Running this code produces an output of kind (batch measurement, sequence measurement, values dimensionality). Note that you’re going to likely see a definite output on account of random initialization of the queries, keys, and values.
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | tf.Tensor( [[[0.60413814 0.52436507 0.46551135 … 0.5260341 0.33879933 0.43999898] [0.60433316 0.52383804 0.465411 … 0.5262608 0.33915892 0.43782598] [0.62321603 0.5349194 0.46824688 … 0.531323 0.34432083 0.43554053] [0.60013235 0.54162943 0.47391182 … 0.53600514 0.33722004 0.4192218 ] [0.6295709 0.53511244 0.46552944 … 0.5317217 0.3462567 0.43129003]] … [[0.20291057 0.18463902 0.641182 … 0.4706118 0.4194418 0.39908117] [0.19932748 0.18717204 0.64831126 … 0.48373622 0.3995132 0.37968236] [0.20611541 0.18079443 0.6374859 … 0.48258874 0.41704425 0.4016996 ] [0.19703123 0.18210654 0.6400498 … 0.47037745 0.4257752 0.3962079 ] [0.19237372 0.18474475 0.64944196 … 0.49497223 0.38804317 0.36352912]]], kind=(64, 5, 64), dtype=float32) |
Further Reading
This half provides further property on the topic for those who’re making an attempt to go deeper.
Books
Papers
Summary
In this tutorial, you discovered recommendations on easy methods to implement scaled dot-product consideration from scratch in TensorFlow into and Keras.
Specifically, you realized:
- The operations that sort part of the scaled dot-product consideration mechanism
- How to implement the scaled dot-product consideration mechanism from scratch
Do you have gotten any questions?
Ask your questions throughout the suggestions underneath, and I’ll do my best to answer.
Learn Transformers and Attention!
Teach your deep learning model to study a sentence
…using transformer fashions with consideration
Discover how in my new Ebook:
Building Transformer Models with Attention
It provides self-study tutorials with working code to info you into developing a fully-working transformer fashions that will
translate sentences from one language to a special…
Give magical power of understanding human language for
Your Projects
See What’s Inside

TensorFlow into 2 Tutorial: Get Started in Deep Learning…

How to Implement Multi-Head Attention from Scratch…

How to Develop an Encoder-Decoder Model with…

The Transformer Attention Mechanism

Building Transformer Models with Attention Crash…

Handwritten Digit Recognition Using Convolutional…
- Get link
- X
- Other Apps
Comments
Post a Comment