Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Events
Videos
Audiobooks
Packt Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Getting Started with Google BERT
Getting Started with Google BERT

Getting Started with Google BERT: Build and train state-of-the-art natural language processing models using BERT

Arrow left icon
Profile Icon Sudharsan Ravichandiran
Arrow right icon
$45.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2 (50 Ratings)
Paperback Jan 2021 352 pages 1st Edition
eBook
$27.89 $30.99
Paperback
$45.99
Arrow left icon
Profile Icon Sudharsan Ravichandiran
Arrow right icon
$45.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2 (50 Ratings)
Paperback Jan 2021 352 pages 1st Edition
eBook
$27.89 $30.99
Paperback
$45.99
eBook
$27.89 $30.99
Paperback
$45.99

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Getting Started with Google BERT

A Primer on Transformers

The transformer is one of the most popular state-of-the-art deep learning architectures that is mostly used for natural language processing (NLP) tasks. Ever since the advent of the transformer, it has replaced RNN and LSTM for various tasks. Several new NLP models, such as BERT, GPT, and T5, are based on the transformer architecture. In this chapter, we will look into the transformer in detail and understand how it works.

We will begin the chapter by getting a basic idea of the transformer. Then, we will learn how the transformer uses encoder-decoder architecture for a language translation task. Following this, we will inspect how the encoder of the transformer works in detail by exploring each of the encoder components. After understanding the encoder, we will deep dive into the decoder and look into each of the decoder components in detail. At the end of the chapter, we will put the encoder and decoder together and see how the transformer works as a whole.

In this chapter, we will learn the following topics:

  • Introduction to the transformer
  • Understanding the encoder of the transformer
  • Understanding the decoder of the transformer
  • Putting the encoder and decoder together
  • Training the transformer

Introduction to the transformer

RNN and LSTM networks are widely used in sequential tasks such as next word prediction, machine translation, text generation, and more. However one of the major challenges with the recurrent model is capturing the long-term dependency.

To overcome this limitation of RNNs, a new architecture called Transformer was introduced in the paper Attention Is All You Need. The transformer is currently the state-of-the-art model for several NLP tasks. The advent of the transformer created a major breakthrough in the field of NLP and also paved the way for new revolutionary architectures such as BERT, GPT-3, T5, and more.

The transformer model is based entirely on the attention mechanism and completely gets rid of recurrence. The transformer uses a special type of attention mechanism called self-attention. We will learn about this in detail in the upcoming sections.

Let's understand how the transformer works with a language translation task. The transformer consists of an encoder-decoder architecture. We feed the input sentence (source sentence) to the encoder. The encoder learns the representation of the input sentence and sends the representation to the decoder. The decoder receives the representation learned by the encoder as input and generates the output sentence (target sentence).

Suppose we need to convert a sentence from English to French. As shown in the following figure, we feed the English sentence as input to the encoder. The encoder learns the representation of the given English sentence and feeds the representation to the decoder. The decoder takes the encoder's representation as input and generates the French sentence as output:

Figure 1.1 – Encoder and decoder of the transformer

Okay, but what's exactly going on here? How does the encoder and decoder in the transformer convert the English sentence (source sentence) to the French sentence (target sentence)? What's going on inside the encoder and decoder? Let's find this out in the next sections. First, we will look into the encoder in detail, and then we will see the decoder.

Understanding the encoder of the transformer

The transformer consists of a stack of number of encoders. The output of one encoder is sent as input to the encoder above it. As shown in the following figure, we have a stack of number of encoders. Each encoder sends its output to the encoder above it. The final encoder returns the representation of the given source sentence as output. We feed the source sentence as input to the encoder and get the representation of the source sentence as output:

Figure 1.2 – A stack of N number of encoders

Note that in the transformer paper Attention Is All You Need, the authors have used , meaning that they stacked up six encoders one above the another. However, we can try out different values of . For simplicity and better understanding, let's keep :

Figure 1.3 – A stack of encoders

Okay, the question is how exactly does the encoder work? How is it generating the representation for the given source sentence (input sentence)? To understand this, let's tap into the encoder and see its components. The following figure shows the components of the encoder:

Figure 1.4 – Encoder with its components

From the preceding figure, we can understand that all the encoder blocks are identical. We can also observe that each encoder block consists of two sublayers:

  • Multi-head attention
  • Feedforward network

Now, let's get into the details and learn how exactly these two sublayers works. To understand how multi-head attention works, first we need to understand the self-attention mechanism. So, in the next section, we will learn how the self-attention mechanism works.

Self-attention mechanism

Let's understand the self-attention mechanism with an example. Consider the following sentence:

 A dog ate the food because it was hungry

In the preceding sentence, the pronoun it could mean either dog or food. By reading the sentence, we can easily understand that the pronoun it implies the dog and not food. But how does our model understand that in the given sentence, the pronoun it implies the dog and not food? Here is where the self-attention mechanism helps us.

In the given sentence, A dog ate the food because it was hungry, first, our model computes the representation of the word A, next it computes the representation of the word dog, then it computes the representation of the word ate, and so on. While computing the representation of each word, it relates each word to all other words in the sentence to understand more about the word.

For instance, while computing the representation of the word it, our model relates the word it to all the words in the sentence to understand more about the word it.

As shown in the following figure, in order to compute the representation of the word it, our model relates the word it to all the words in the sentence. By relating the word it to all the words in the sentence, our model can understand that the word it is related to the word dog and not food. As we can observe, the line connecting the word it to dog is thicker compared to the other lines, which indicates that the word it is related to the word dog and not food in the given sentence:

Figure 1.5 – Self-attention example

Okay, but how exactly does this work? Now that we have a basic idea of what the self-attention mechanism is, let's understand more about it in detail.

Suppose our input sentence (source sentence) is I am good. First, we get the embeddings for each word in our sentence. Note that the embeddings are just the vector representation of the word and the values of the embeddings will be learned during training.

Let be the embedding of the word I, be the embedding of the word am, and be the embedding of the word good. Consider the following:

  • The embedding of the word I is .
  • The embedding of the word am is .
  • The embedding of the word good is .

Then, we can represent our input sentence I am good using the input matrix (embedding matrix or input embedding) as shown here:

Figure 1.6 – Input matrix
Note that the values used in the preceding matrix are arbitrary just to give us a better understanding.

From the preceding input matrix, , we can understand that the first row of the matrix implies the embedding of the word I, the second row implies the embedding of the word am, and the third row implies the embedding of the word good. Thus, the dimension of the input matrix will be [sentence length x embedding dimension]. The number of words in our sentence (sentence length) is 3. Let the embedding dimension be 512; then, our input matrix (input embedding) dimension will be [3 x 512].

Now, from the input matrix, , we create three new matrices: a query matrix, , key matrix, , and value matrix, . Wait. What are these three new matrices? And why do we need them? They are used in the self-attention mechanism. We will see how exactly these three matrices are used in a while.

Okay, how we can create the query, key, and value matrices? To create these, we introduce three new weight matrices, called . We create the query, , key, , and value, , matrices by multiplying the input matrix, , by , , and , respectively.

Note that the weight matrices, ,, and , are randomly initialized and their optimal values will be learned during training. As we learn the optimal weights, we will obtain more accurate query, key, and value matrices.

As shown in the following figure, multiplying the input matrix, , by the weight matrices, ,, and , we obtain the query, key, and value matrices:

Figure 1.7 – Creating query, key, and value matrices

From the preceding figure, we can understand the following:

  • The first row in the query, key, and value matrices – , , and – implies the query, key, and value vectors of the word I.
  • The second row in the query, key, and value matrices – , , and implies the query, key, and value vectors of the word am.
  • The third row in the query, key, and value matrices – , , and implies the query, key, and value vectors of the word good.

Note that the dimensionality of the query, key, value vectors are 64. Thus, the dimension of our query, key, and value matrices is [sentence length x 64]. Since we have three words in the sentence, the dimensions of the query, key, and value matrices are [3 x 64].

But still, the ultimate question is why are we computing this? What is the use of query, key, and value matrices? How is this going to help us? This is exactly what we will discuss in detail in the next section.

Understanding the self-attention mechanism

We learned how to compute query, , key, , and value, , matrices and we also learned that they are obtained from the input matrix, . Now, let's see how the query, key, and value matrices are used in the self-attention mechanism.

We learned that in order to compute a representation of a word, the self-attention mechanism relates the word to all the words in the given sentence. Consider the sentence I am good. To compute the representation of the word I, we relate the word I to all the words in the sentence, as shown in the following figure:

Figure 1.8 – Self-attention example

But why do we need to do this? Understanding how a word is related to all the words in the sentence helps us to learn better representation. Now, let's learn how the self-attention mechanism relates a word to all the words in the sentence using the query, key, and value matrices. The self-attention mechanism includes four steps; let's take a look at them one by one.

Step 1

The first step in the self-attention mechanism is to compute the dot product between the query matrix, , and the key matrix, :

Figure 1.9 – Query and key matrices

The following shows the result of the dot product between the query matrix, , and the key matrix, :

Figure 1.10 – Computing the dot product between the query and key matrices

But what is the use of computing the dot product between the query and key matrices? What exactly does signify? Let's understand this by looking at the result of in detail.

Let's look into the first row of the matrix as shown in the following figure. We can observe that we are computing the dot product between the query vector (I), and all the key vectors – (I), (am), and (good). Computing the dot product between two vectors tells us how similar they are.

Thus, computing the dot product between the query vector () and the key vectors tells us how similar the query vector (I) is to all the key vectors – (I), (am), and (good). By looking at the first row of the matrix, we can understand that the word I is more related to itself than the words am and good since the dot product value is higher for compared to and :

Figure 1.11 – Computing the dot product between the query vector (q1) and the key vectors (k1, k2, and k3)
Note that the values used in this chapter are arbitrary just to give us a better understanding.

Now, let's look into the second row of the matrix . As shown in the following figure, we can observe that we are computing the dot product between the query vector, (am), and all the key vectors – (I), (am), and (good). This tells us how similar the query vector (am) is to the key vectors – (I), (am), and (good).

By looking at the second row of the matrix, we can understand that the word am is more related to itself than the words I and good since the dot product value is higher for compared to and :

Figure 1.12 – Computing dot product between the query vector (q2) and the key vectors (k1, k2, and k3)

Similarly, let's look into the third row of the matrix. As shown in the following figure, we can observe that we are computing the dot product between the query vector (good) and all the key vectors – (I), (am), and (good). This tells us how similar the query vector (good) is to all the key vectors – (I), (am), and (good).

By looking at the third row of the matrix, we can understand that the word good is more related to itself than the words I and am in the sentence since the dot product value is higher for compared to and :

Figure 1.13 – Computing the dot product between the query vector (q3) and the key vectors (k1, k2, and k3)

Thus, we can say that computing the dot product between the query matrix, , and the key matrix, , essentially gives us the similarity score, which helps us to understand how similar each word in the sentence is to all other words.

Step 2

The next step in the self-attention mechanism is to divide the matrix by the square root of the dimension of the key vector. But why do we have to do that? This is useful in obtaining stable gradients.

Let be the dimension of the key vector. Then, we divide by . The dimension of the key vector is 64. So, taking the square root of it, we will obtain 8. Hence, we divide Q.KT by 8 as shown in the following figure:

Figure 1.14 – Dividing Q.KT by the square root of dk

Step 3

By looking at the preceding similarity scores, we can understand that they are in the unnormalized form. So, we normalize them using the softmax function. Applying softmax function helps in bringing the score to the range of 0 to 1 and the sum of the scores equals to 1, as shown in the following figure:

Figure 1.15 – Applying the softmax function

We can call the preceding matrix a score matrix. With the help of these scores, we can understand how each word in the sentence is related to all the words in the sentence. For instance, look at the first row in the preceding score matrix; it tells us that the word I is related to itself by 90%, to the word am by 7%, and to the word good by 3%.

Step 4

Okay, what's next? We computed the dot product between the query and key matrices, obtained the scores, and then normalized the scores with the softmax function. Now, the final step in the self-attention mechanism is to compute the attention matrix, .

The attention matrix contains the attention values for each word in the sentence. We can compute the attention matrix, , by multiplying the score matrix, , by the value matrix, , as shown in the following figure:

Figure 1.16 – Computing the attention matrix

Say we have the following:

Figure 1.17 – Result of the attention matrix

The attention matrix, , is computed by taking the sum of the value vectors weighted by the scores. Let's understand this by looking at it row by row. First, let's see how for the first row, , the self-attention of the word I is computed:

Figure 1.18 – Self-attention of the word I

From the preceding figure, we can understand that , the self-attention of the word I is computed as the sum of the value vectors weighted by the scores. Thus, the value of will contain 90% of the values from the value vector (I), 7% of the values from the value vector (am), and 3% of values from the value vector (good).

But how is this useful? To answer this question, let's take a little detour to the example sentence we saw earlier, A dog ate the food because it was hungry. Here, the word it indicates dog. To compute the self-attention of the word it, we follow the same preceding steps. Suppose we have the following:

Figure 1.19 – Self-attention of the word it

From the preceding figure, we can understand that the self-attention value of the word it contains 100% of the values from the value vector (dog). This helps the model to understand that the word it actually refers to dog and not food. Thus, by using a self-attention mechanism, we can understand how a word is related to all other words in the sentence.

Now, coming back to our example, , the self-attention of the word am is computed as the sum of the value vectors weighted by the scores, as shown in the following figure:

Figure 1.20 – Self-attention of the word am

As we can observe from the preceding figure, the value of will contain 2.5% of the values from the value vector (I), 95% of the values from the value vector (am), and 2.5% of the values from the value vector (good).

Similarly, , the self-attention of the word good is computed as the sum of the value vectors weighted by the scores, as shown in the following figure:

Figure 1.21 – Self-attention of the word good

This implies that the value of will contain 21% of the values from the value vector v1(I), 3% of the values from the value vector v2(am), and 76% of values from the value vector v3(good).

Thus, the attention matrix, , consists of self-attention values of all the words in the sentence and it is computed as follows:

To get a better understanding of the self-attention mechanism, the steps involved are summarized as follows:

  1. First, we compute the dot product between the query matrix and the key matrix, , and get the similarity scores.
  2. Next, we divide by the square root of the dimension of the key vector, .
  3. Then, we apply the softmax function to normalize the scores and obtain the score matrix, .
  4. At the end, we compute the attention matrix, , by multiplying the score matrix by the value matrix, .

The self-attention mechanism is graphically shown as follows:

Figure 1.22 – Self-attention mechanism

The self-attention mechanism is also called scaled dot product attention, since here we are computing the dot product (between the query and key vectors) and scaling the values (with ).

Now that we have understood how the self-attention mechanism works, in the next section, we will learn about the multi-head attention mechanism.

Multi-head attention mechanism

Instead of having a single attention head, we can use multiple attention heads. That is, in the previous section, we learned how to compute the attention matrix, . Instead of computing a single attention matrix, , we can compute multiple attention matrices. But what is the use of computing multiple attention matrices?

Let's understand this with an example. Consider the phrase All is well. Say we need to compute the self-attention of the word well. After computing the similarity score, suppose we have the following:

Figure 1.23 – Self-attention of the word well

As we can observe from the preceding figure, the self-attention value of the word well is the sum of the value vectors weighted by the scores. If you look at the preceding figure closely, the attention value of the actual word well is dominated by the other word All. That is, since we are multiplying the value vector of the word All by 0.6 and the value vector of the actual word well by only 0.4, it implies that will contain 60% of the values from the value vector of the word All and only 40% of the values from the value vector of the actual word well. Thus, here the attention value of the actual word well is dominated by the other word All.

This will be useful only in circumstances where the meaning of the actual word is ambiguous. That is, consider the following sentence:

A dog ate the food because it was hungry

Say we are computing the self-attention for the word it. After computing the similarity score, suppose we have the following:

Figure 1.24 – Self-attention of the word it

As we can observe from the preceding equation, here the attention value of the word it is just the value vector of the word dog. Here, the attention value of the actual word it is dominated by the word dog. But this is fine here since the meaning of the word it is ambiguous as it may refer to either dog or food.

Thus, if the value vector of other words dominates the actual word in cases as shown in the preceding example, where the actual word is ambiguous, then this dominance is useful; otherwise, it will cause an issue in understanding the right meaning of the word. So, in order to make sure that our results are accurate, instead of computing a single attention matrix, we will compute multiple attention matrices and then concatenate their results. The idea behind using multi-head attention is that instead of using a single attention head, if we use multiple attention heads, then our attention matrix will be more accurate. Let's explore this in more detail.

Let's suppose we are computing two attention matrices, and . First, let's compute the attention matrix .

We learned that to compute the attention matrix, we create three new matrices, called query, key, and value matrices. To create the query, , key, , and value, , matrices, we introduce three new weight matrices, called . We create the query, key, and value matrices by multiplying the input matrix, , by ,, and , respectively.

Now, the attention matrix can be computed as follows:

Now, let's compute the second attention matrix, .

To compute the attention matrix , we create another set of query, Q2, key, K2, and value, V2, matrices. We introduce three new weight matrices, called , and we create the query, key, and value matrices by multiplying the input matrix, , by ,, and , respectively.

The attention matrix can be computed as follows:

Similarly, we can compute number of attention matrices. Suppose we have eight attention matrices, to ; then, we can just concatenate all the attention heads (attention matrices) and multiply the result by a new weight matrix, , and create the final attention matrix as shown:

Now that we have learned how the multi-attention mechanism works, we will learn about another interesting concept, called positional encoding, in the next section.

Learning position with positional encoding

Consider the input sentence I am good. In RNNs, we feed the sentence to the network word by word. That is, first the word I is passed as input, next the word am is passed, and so on. We feed the sentence word by word so that our network understands the sentence completely. But with the transformer network, we don't follow the recurrence mechanism. So, instead of feeding the sentence word by word, we feed all the words in the sentence parallel to the network. Feeding the words in parallel helps in decreasing the training time and also helps in learning the long-term dependency.

However, the problem is since we feed the words parallel to the transformer, how will it understand the meaning of the sentence if the word order is not retained? To understand the sentence, the word order (position of the words in the sentence) is important, right? Yes, the word order is very important as it helps to understand the position of each word in a sentence, which in turn helps to understand the meaning of the sentence.

So, we should give some information about the word order to the transformer so that it can understand the sentence. How can we do that? Let's explore this in more detail now.

For our given sentence, I am good, first, we get the embeddings for each word in our sentence. Let's represent the embedding dimension as . Say the embedding dimension, , is 4. Then, our input matrix dimension will be [sentence length x embedding dimension] = [3 x 4].

We represent our input sentence I am good using the input matrix (embedding matrix). Let the input matrix be the following:

Figure 1.25 – Input matrix

Now, if we pass the preceding input matrix directly to the transformer, it cannot understand the word order. So, instead of feeding the input matrix directly to the transformer, we need to add some information indicating the word order (position of the word) so that our network can understand the meaning of the sentence. To do this, we introduce a technique called positional encoding. Positional encoding, as the name suggests, is an encoding indicating the position of the word in a sentence (word order).

The dimension of the positional encoding matrix, , is the same dimension as the input matrix . Now, before feeding the input matrix (embedding matrix) to the transformer directly, we include the positional encoding. So, we simply add the positional encoding matrix to the embedding matrix and then feed it as input to the network. So, now our input matrix will have not only the embedding of the word but also the position of the word in the sentence:

Figure 1.26 – Adding an input matrix and positional encoding matrix

Now, the ultimate question is how exactly is the positional encoding matrix computed? The authors of the transformer paper Attention Is All You Need have used the sinusoidal function for computing the positional encoding, as shown:

In the preceding equation, implies the position of the word in a sentence, and implies the position of the embedding. Let's understand the preceding equations with an example. By using the preceding equations, we can write the following:

Figure 1.27 – Computing the positional encoding matrix

As we can observe from the preceding matrix, in the positional encoding, we use the sin function when is even and the cos function when is odd. Simplifying the preceding matrix, we can write the following:

Figure 1.28 – Computing the positional encoding matrix

We know that in our input sentence, the word I is at the 0th position, am is at the 1st position, and good is at the 2nd position. Substituting the value, we can write the following:

Figure 1.29 – Computing the positional encoding matrix

Thus, our final positional encoding matrix, , is given as follows:

Figure 1.30 – Positional encoding matrix

After computing the positional encoding we simply perform element-wise addition with the embedding matrix and feed the modified input matrix to the encoder.

Now, let's revisit our encoder architecture. A single encoder block is shown in the following figure. As we can observe, before feeding the input directly to the encoder, first, we get the input embedding (embedding matrix), and then we add the positional encoding to it, and then we feed it as input to the encoder:

Figure 1.31 – A single encoder block

We learned how the positional encoder works; we also learned how the multi-head attention sublayer works in the previous section. In the next section, we will learn how the feedforward network sublayer works in the encoder.

Feedforward network

The feedforward network sublayer in an encoder block is shown in the following figure:

Figure 1.32 – Encoder block

The feedforward network consists of two dense layers with ReLU activations. The parameters of the feedforward network are the same over the different positions of the sentence and different over the encoder blocks. In the next section, we will look into another interesting component of the encoder.

Add and norm component

One more important component in our encoder is the add and norm component. It connects the input and output of a sublayer. That is, as shown in the following figure (dotted lines), we can observe that the add and norm component:

  • Connects the input of the multi-head attention sublayer to its output
  • Connects the input of the feedforward sublayer to its output:
Figure 1.33 – Encoder block with the add and norm component

The add and norm component is basically a residual connection followed by layer normalization. Layer normalization promotes faster training by preventing the values in each layer from changing heavily.

Now that we have learned about all the components of the encoder, let's put all of them together and see how the encoder works as a whole in the next section.

Putting all the encoder components together

The following figure shows the stack of two encoders; only encoder 1 is expanded to reduce the clutter:

Figure 1.34 – A stack of encoders with encoder 1 expanded

From the preceding figure, we can understand the following:

  1. First, we convert our input to an input embedding (embedding matrix), and then add the position encoding to it and feed it as input to the bottom-most encoder (encoder 1).
  2. Encoder 1 takes the input and sends it to the multi-head attention sublayer, which returns the attention matrix as output.
  1. We take the attention matrix and feed it as input to the next sublayer, which is the feedforward network. The feedforward network takes the attention matrix as input and returns the encoder representation as output.
  2. Next, we take the output obtained from encoder 1 and feed it as input to the encoder above it (encoder 2).
  3. Encoder 2 carries the same process and returns the encoder representation of the given input sentence as output.

We can stack number of encoders one above the other; the output (encoder representation) obtained from the final encoder (topmost encoder) will be the representation of the given input sentence. Let's denote the encoder representation obtained from the final encoder (in our example, it is encoder 2) as .

We take this encoder representation, , obtained from the final encoder (encoder 2) and feed it as input to the decoder. The decoder takes the encoder representation as input and tries to generate the target sentence.

Now that we have understood the encoder part of the transformer, in the next section, we will learn how a decoder works in detail.

Understanding the decoder of a transformer

Suppose we want to translate the English sentence (source sentence) I am good to the French sentence (target sentence) Je vais bien. To perform this translation, we feed the source sentence I am good to the encoder. The encoder learns the representation of the source sentence. In the previous section, we learned how exactly the encoder learns the representation of the source sentence. Now, we take this encoder's representation and feed it to the decoder. The decoder takes the encoder representation as input and generates the target sentence Je vais bien, as shown in the following figure:

Figure 1.35 – Encoder and decoder of the transformer

In the encoder section, we learned that, instead of having one encoder, we can have a stack of encoders. Similar to the encoder, we can also have a stack of decoders. For simplicity, let's set . As shown in the following figure, the output of one decoder is sent as the input to the decoder above it. We can also observe that the encoder's representation of the input sentence (encoder's output) is sent to all the decoders. Thus, a decoder receives two inputs: one is from the previous decoder, and the other is the encoder's representation (encoder's output):

Figure 1.36 – A stack of encoders and decoders

Okay, but how exactly does the decoder generate the target sentence? Let's explore that in more detail. At time step , the input to the decoder will be <sos>, which indicates the start of the sentence. The decoder takes <sos> as input and generates the first word in the target sentence, which is Je, as shown in the following figure:

Figure 1.37 – Decoder prediction at time step t = 1

At time step , along with the current input, the decoder takes the newly generated word from the previous time step, , and tries to generate the next word in the sentence. Thus, the decoder takes <sos> and Je (from the previous step) as input and tries to generate the next word in the target sentence, as shown in the following figure:

Figure 1.38 – Decoder prediction at time step t = 2

At time step , along with the current input, the decoder takes the newly generated word from the previous time step, , and tries to generate the next word in the sentence. Thus, the decoder takes <sos>, Je, and vais (from the previous step) as input and tries to generate the next word in the sentence, as shown in the following figure:

Figure 1.39 – Decoder prediction at time step t = 3

Similarly, on every time step, the decoder combines the newly generated word to the input and predicts the next word. Thus, at time step , the decoder takes <sos>, Je, vais, and bien as input and tries to generate the next word in the sentence, as shown in the following figure:

Figure 1.40 – Decoder prediction at time step t = 4

As we can observe from the preceding figure, once the <eos> token, which indicates the end of sentence, is generated, it implies that the decoder has completed generating the target sentence.

In the encoder section, we learned that we convert the input into an embedding matrix and add the positional encoding to it, and then feed it as input to the encoder. Similarly, here, instead of feeding the input directly to the decoder, we convert it into an embedding, add the positional encoding to it, and then feed it to the decoder.

For example, as shown in the following figure, say at time step we convert the input into an embedding (we call it an output embedding because here we are computing the embedding of the words generated by the decoder in previous time steps), add the positional encoding to it, and then send it to the decoder:

Figure 1.41 – Encoder and decoder with positional encoding

Okay, but the ultimate question is how exactly does the decoder work? What's going on inside the decoder? Let's explore this in detail. A single decoder block with all of its components is shown in the following figure:

Figure 1.42 – A decoder block

From the preceding figure, we can observe that the decoder block is similar to the encoder and here we have three sublayers:

  • Masked multi-head attention
  • Multi-head attention
  • Feedforward network

Similar to the encoder block, here we have multi-head attention and feedforward network sublayers. However, here we have two multi-head attention sublayers and one of them is masked. Now that we have a basic idea of the decoder, first, let's look into each component of the decoder in detail, and then we will see how the decoder works as a whole.

Masked multi-head attention

In our English-to-French translation task, say our training dataset looks like the one shown here:

Figure 1.43 – A sample training set

By looking at the preceding dataset, we can understand that we have source and target sentences. In the previous section, we saw how the decoder predicts the target sentence word by word in each time step and that happens only during testing.

During training, since we have the right target sentence, we can just feed the whole target sentence as input to the decoder but with a small modification. We learned that the decoder takes the input <sos> as the first token, and combines the next predicted word to the input on every time step for predicting the target sentence until the <eos> token is reached. So, we can just add the <sos> token to the beginning of our target sentence and send that as an input to the decoder.

Say we are converting the English sentence I am good to the French sentence Je vais bien. We can just add the <sos> token to the beginning of the target sentence and send <sos> Je vais bien as an input to the decoder, and then the decoder predicts the output as Je vais bien <eos>, as shown in the following figure:

Figure 1.44 – Encoder and decoder of the transformer

But how does this work? Isn't this kind of ambiguous? Why do we need to feed the entire target sentence and let the decoder predict the shifted target sentence as output? Let's explore this in more detail.

We learned that instead of feeding the input directly to the decoder, we convert it into an embedding (output embedding matrix) and add positional encoding, and then feed it to the decoder. Let's suppose the following matrix, , is obtained as a result of adding the output embedding matrix and positional encoding:

Figure 1.45 – Input matrix

Now, we feed the preceding matrix, , to the decoder. The first layer in the decoder is the masked-multi head attention. This works similar to the multi-head attention mechanism we learned about with the encoder but in a small difference.

To perform self-attention, we create three new matrices, called query, , key, , and value, . Since we are computing multi-head attention, we create number of query, key, and value matrices. Thus, for a head, , the query, , key, , and value, , matrices can be created by multiplying by the weight matrices, , respectively.

Now, let's see how masked multi-head attention works. Our input sentence to the decoder is <sos> Je vais bien. We learned that the self-attention mechanism relates a word to all the words in the sentence to understand more about each word. But there is a small catch here. During test time, the decoder will only have the words generated until the previous step as input. For example, during testing, say at time step the decoder will only have the input words as [<sos>, Je] and it will not have any other words. So, we have to train our model in the same fashion. Thus, our attention mechanism should relate the words only until the word Je and not the other words. To do this, we can mask all the words on the right that are not predicted by our model yet.

Say we want to predict the word next to the word <sos>. In this case, the model should see only the words up to <sos>, so we mask all the words on the right of <sos>. Say we want to predict the word next to the word Je. In this case, the model should see only the words up to Je, so we mask all the words to the right of Je, and the same applies for other rows, as shown in the following figure:

Figure 1.46 – Masking the values

Masking words like this help the self-attention mechanism to attend only to the words that would be available to the model during testing. Okay, but how exactly we can perform this masking? We know that for a head, , the attention matrix, , is computed as follows:

The first step in computing the attention matrix is computing the dot product between the query and key matrices. The following shows the result of the dot product between the query and the key matrix. Note that the values used here are arbitrary, just to get a good understanding:

Figure 1.47 – Dot product of the query and key matrices

The next step is to divide the matrix by the dimension of key vector . Suppose the following is the result of :

Figure 1.48 – Dividing QiKTi by the square root of dk

Next, we apply the softmax function to the preceding matrix and normalize the scores. But before applying the softmax function, we need to mask the values. For example, look at the first row of our matrix. To predict the word next to the word <sos>, our model should not attend all the words to the right of <sos> (as this will not be available during test time). So, we can mask all the words to the right of <sos> with :

Figure 1.49 – Masking all the words to the right of <sos> with

Now, let's take a look at the second row of the matrix. To predict the word next to the word Je, our model should not attend all the words to the right of Je (as this will not be available during test time). So, we can mask all the words to the right of Je with :

Figure 1.50 – Masking all the words to the right of Je with

Similarly, we can mask all the words to the right of vais with as shown:

Figure 1.51 – Masking all the words in the right of vais with

Now, we can apply the softmax function to the preceding matrix and multiply the result by the value matrix, , and obtain the final attention matrix, . Similarly, we can compute h number of attention matrices, concatenate them, and multiply the result by a new weight matrix, , and create the final attention matrix, , as shown:

Now, we feed this final attention matrix, , to the next sublayer in our decoder, which is another multi-head attention layer. Let's see how that works in detail in the next section.

Multi-head attention

The following figure shows the transformer model with both the encoder and decoder. As we can observe, the multi-head attention sublayer in each decoder receives two inputs: one is from the previous sublayer, masked multi-head attention, and the other is the encoder representation:

Figure 1.52 – Encoder-decoder interaction

Let's represent the encoder representation by and the attention matrix obtained as a result of the masked multi-head attention sublayer by . Since here we have an interaction between the encoder and decoder, this layer is also called an encoder-decoder attention layer.

Now, let's look into the details and learn how exactly this multi-head attention layer works. The first step in the multi-head attention mechanism is creating the query, key, and value matrices. We learned that we can create the query, key, and value matrices by multiplying the input matrix by the weight matrices. But in this layer, we have two input matrices: one is (the encoder representation) and the other is (the attention matrix from the previous sublayer). So, which one should we use?

We create the query matrix, , using the attention matrix, , obtained from the previous sublayer and we create the key and value matrices using the encoder representation, R. Since we are performing the multi-head attention mechanism, for head , we do the following:

  • The query matrix, , is created by multiplying the attention matrix, , by the weight matrix, .
  • The key and value matrices are created by multiplying the encoder representation, , by the weight matrices, and , respectively. This is shown in the following figure:
Figure 1.53 – Creating query, key, and value matrices

But why do we have to do this? Why do we obtain the query matrix from and key and value matrices from ? The query matrix essentially holds the representation of our target sentence since it is obtained from and the key and value matrices hold the representation of the source sentence since it is obtained from . But how and why exactly is this useful? Let's understand this by calculating the self-attention step by step.

The first step in self-attention is to compute the dot product between the query and key matrices. The query and key matrices are shown in the following figure. As we can observe, since the query matrix is obtained from , it holds the representation of the target sentence, and since the key matrix is obtained from , it holds the representation of the input sentence. Note that the values used here are arbitrary, just to get a good understanding:

Figure 1.54 – Query and key matrices

The following shows the result of the dot product between the query and key matrices:

Figure 1.55 – Dot product between the query and key matrices

By looking at the preceding matrix, , we can understand the following:

  • From the first row of the matrix, we can observe that we are computing the dot product between the query vector (<sos>) and all the key vectors – (I), (am), and (good). Thus, the first row indicates how similar the target word <sos> is to all the words in the source sentence (I, am, and good).
  • Similarly, from the second row of the matrix, we can observe that we are computing the dot product between the query vector (Je) and all the key vectors – (I), (am), and (good). Thus, the second row indicates how similar the target word Je is to all the words in the source sentence (I, am, and good).
  • The same applies to all other rows. Thus, computing helps us to understand how similar our query matrix (target sentence representation) is to the key matrix (source sentence representation).

The next step in the multi-head attention matrix is to divide by . Then, we apply the softmax function and obtain the score matrix, .

Next, we multiply the score matrix by the value matrix, , that is, , and obtain the attention matrix, , as shown:

Figure 1.56 – Computing the attention matrix

Say we have the following:

Figure 1.57 – Result of attention matrix

The attention matrix, , of the target sentence is computed by taking the sum of value vectors weighted by the scores. To get some clarity, let's see how the self-attention value of the word Je, , is computed:

Figure 1.58 – Self-attention of the word Je

As shown, , the self-attention of the word Je is computed as the sum of the value vectors weighted by the scores. Thus, the value of will contain 98% of the values from the value vector (I) and 2% of the values from the value vector (am). This basically helps the model to understand that the target word Je means the source word am.

Similarly, we can compute number of attention matrices, concatenate them, and multiply the result by a new weight matrix, , and create the final attention matrix, as shown:

Now, we feed this final attention matrix to the next sublayer in our decoder, which is a feedforward network. Let's see how that works in the next section.

Feedforward network

The next sublayer in the decoder is the feedforward network, as shown in the following figure:

Figure 1.59 – A decoder block

The feedforward layer in the decoder works exactly the same as what we learned in the encoder. Next, we will look into the add and norm component.

Add and norm component

Just like we learned with the encoder, the add and norm component connects the input and output of a sublayer, as shown in the following figure:

Figure 1.60 – A decoder block with an add and norm component

Next, we will look at the linear and softmax layers.

Linear and softmax layers

Once the decoder learns the representation of the target sentence, we feed the output obtained from the topmost decoder to the linear and softmax layers, as shown in the following figure:

Figure 1.61 – Linear and softmax layers

The linear layer generates the logits whose size is equal to our vocabulary size. Suppose our vocabulary consists of only the following three words:

Now, the logits returned by the linear layer will be a vector of size 3. Next, we convert the logits into a probability using the softmax function, and then the decoder outputs the word whose index has a high probability value. Let's understand this with an example.

Suppose the input to the decoder are the words <sos> and Je. Now, the decoder needs to predict the next word in our target sentence. So, we take the output of the topmost decoder and feed it to the linear layer. The linear layer generates the logits vector whose size is our vocabulary size. Let the logits returned by the linear layer be the following:

Now, we apply the softmax function to the logits generated by the linear layer and obtain the probability:

From the preceding matrix, we can understand that the probability is high at index 2. So, we look for the word that is at index 2 in our vocabulary. Since the word vais is at index 2, our decoder will predict the next word in our target sentence as vais. In this way, the decoder predicts the next word in the target sentence.

Now that we have understood all the decoder components, let's put them all together and see how they work as a whole in the next section.

Putting all the decoder components together

The following figure shows the stack of two decoders; only decoder 1 is expanded to reduce the clutter:

Figure 1.62 – A stack of two decoders with decoder 1 expanded

From the preceding figure, we can understand the following:

  1. First, we convert the input to the decoder into an embedding matrix and then add the position encoding to it and feed it as input to the bottom-most decoder (decoder 1).
  2. The decoder takes the input and sends it to the masked multi-head attention layer, which returns the attention matrix, , as output.
  3. Now, we take the attention matrix, , and also the encoder representation, , and feed them as input to the multi-head attention layer (encoder-decoder attention layer), which again outputs the new attention matrix.
  4. We take the attention matrix obtained from the encoder-decoder attention layer and feed it as input to the next sublayer, which is the feedforward network. The feedforward network takes the attention matrix as input and returns the decoder representation as output.
  5. Next, we take the output obtained from decoder 1 and feed it as input to the decoder above it (decoder 2).
  6. Decoder 2 carries the same process and returns the decoder representation of the target sentence as output.

We can stack number of decoders one above the other; the output (decoder representation) obtained from the final decoder will be the representation of the target sentence. Next, we feed the decoder representation of the target sentence to the linear and softmax layers and get the predicted word.

Now that we have learned how the encoder and decoder work in detail, let's put the encoder and decoder together and see how the transformer model works as a whole in the next section.

Putting the encoder and decoder together

To give more clarity, the complete transformer architecture with the encoder and decoder is shown in the following figure:

Figure 1.63 – Encoder and decoder of the transformer

In the preceding figure, Nx denotes that we can stack number of encoders and decoders. As we can observe, once we feed the input sentence (source sentence), the encoder learns the representation and sends the representation to the decoder, which in turn generates the output sentence (target sentence).

Training the transformer

We can train the transformer network by minimizing the loss function. Okay, but what loss function should we use? We learned that the decoder predicts the probability distribution over the vocabulary and we select the word that has the highest probability as output. So, we have to minimize the difference between the predicted probability distribution and the actual probability distribution. First, how can we find the difference between the two distributions? We can use cross-entropy for that. Thus, we can define our loss function as a cross-entropy loss and try to minimize the difference between the predicted and actual probability distribution. We train the network by minimizing the loss function and we use Adam as an optimizer.

One additional point we need to note down is that to prevent overfitting, we apply dropout to the output of each sublayer and we also apply dropout to the sum of the embeddings and the positional encoding.

Thus, in this chapter, we learned how the transformer works in detail. In the next chapter, we will get started with BERT.

Summary

We started off the chapter by understanding what the transformer model is and how it uses encoder-decoder architecture. We looked into the encoder section of the transformer and learned about different sublayers used in encoders, such as multi-head attention and feedforward networks.

We learned that the self-attention mechanism relates a word to all the words in the sentence to better understand the word. To compute self-attention, we used three different matrices, called the query, key, and value matrices. Following this, we learned how to compute positional encoding and how it is used to capture the word order in a sentence. Next, we learned how the feedforward network works in the encoder and then we explored the add and norm component.

After understanding the encoder, we understood how the decoder works. We explored three sublayers used in the decoder in detail, which are the masked multi-head attention, encoder-decoder attention, and feedforward network. Following this, we understood how the transformer works with the encoder and decoder, and then at the end of the chapter, we learned how we train the network.

In the next chapter, we will learn what BERT is and how it uses the transformer for learning contextual embeddings in detail.

Questions

Let's put our newly acquired knowledge to the test. Try answering the following questions:

  1. What are the steps involved in the self-attention mechanism?
  2. What is scaled dot product attention?
  3. How do we create the query, key, and value matrices?
  4. Why do we need positional encoding?
  5. What are the sublayers of the decoder?
  6. What are the inputs to the encoder-decoder attention layer of the decoder?

Further reading

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Explore the encoder and decoder of the transformer model
  • Become well-versed with BERT along with ALBERT, RoBERTa, and DistilBERT
  • Discover how to pre-train and fine-tune BERT models for several NLP tasks

Description

BERT (bidirectional encoder representations from transformer) has revolutionized the world of natural language processing (NLP) with promising results. This book is an introductory guide that will help you get to grips with Google's BERT architecture. With a detailed explanation of the transformer architecture, this book will help you understand how the transformer’s encoder and decoder work. You’ll explore the BERT architecture by learning how the BERT model is pre-trained and how to use pre-trained BERT for downstream tasks by fine-tuning it for NLP tasks such as sentiment analysis and text summarization with the Hugging Face transformers library. As you advance, you’ll learn about different variants of BERT such as ALBERT, RoBERTa, and ELECTRA, and look at SpanBERT, which is used for NLP tasks like question answering. You'll also cover simpler and faster BERT variants based on knowledge distillation such as DistilBERT and TinyBERT. The book takes you through MBERT, XLM, and XLM-R in detail and then introduces you to sentence-BERT, which is used for obtaining sentence representation. Finally, you'll discover domain-specific BERT models such as BioBERT and ClinicalBERT, and discover an interesting variant called VideoBERT. By the end of this BERT book, you’ll be well-versed with using BERT and its variants for performing practical NLP tasks.

Who is this book for?

This book is for NLP professionals and data scientists looking to simplify NLP tasks to enable efficient language understanding using BERT. A basic understanding of NLP concepts and deep learning is required to get the best out of this book.

What you will learn

  • Understand the transformer model from the ground up
  • Find out how BERT works and pre-train it using masked language model (MLM) and next sentence prediction (NSP) tasks
  • Get hands-on with BERT by learning to generate contextual word and sentence embeddings
  • Fine-tune BERT for downstream tasks
  • Get to grips with ALBERT, RoBERTa, ELECTRA, and SpanBERT models
  • Get the hang of the BERT models based on knowledge distillation
  • Understand cross-lingual models such as XLM and XLM-R
  • Explore Sentence-BERT, VideoBERT, and BART
Estimated delivery fee Deliver to Russia

Economy delivery 10 - 13 business days

$6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 22, 2021
Length: 352 pages
Edition : 1st
Language : English
ISBN-13 : 9781838821593
Vendor :
Google
Category :
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Russia

Economy delivery 10 - 13 business days

$6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Jan 22, 2021
Length: 352 pages
Edition : 1st
Language : English
ISBN-13 : 9781838821593
Vendor :
Google
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 208.97
Mastering Transformers
$57.99
Getting Started with Google BERT
$45.99
Transformers for Natural Language Processing
$104.99
Total $ 208.97 Stars icon

Table of Contents

14 Chapters
Section 1 - Starting Off with BERT Chevron down icon Chevron up icon
A Primer on Transformers Chevron down icon Chevron up icon
Understanding the BERT Model Chevron down icon Chevron up icon
Getting Hands-On with BERT Chevron down icon Chevron up icon
Section 2 - Exploring BERT Variants Chevron down icon Chevron up icon
BERT Variants I - ALBERT, RoBERTa, ELECTRA, and SpanBERT Chevron down icon Chevron up icon
BERT Variants II - Based on Knowledge Distillation Chevron down icon Chevron up icon
Section 3 - Applications of BERT Chevron down icon Chevron up icon
Exploring BERTSUM for Text Summarization Chevron down icon Chevron up icon
Applying BERT to Other Languages Chevron down icon Chevron up icon
Exploring Sentence and Domain-Specific BERT Chevron down icon Chevron up icon
Working with VideoBERT, BART, and More Chevron down icon Chevron up icon
Assessments Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2
(50 Ratings)
5 star 72%
4 star 4%
3 star 6%
2 star 8%
1 star 10%
Filter icon Filter
Top Reviews

Filter reviews by




Amazon Customer Feb 07, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book acts as a great resource for learning BERT. It covers so many different types of BERT and helps you to learn how to apply BERT for interesting use cases. It’s a perfect getting started guide for BERT. The writing is so simple, clear, to the point. The way one topic connects to another is so interesting. I can’t close the book after reading one chapter, the book keeps you so engaging.
Amazon Verified review Amazon
ani Feb 05, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book explains transformers and BERT in very detail. I’m awestruck with the way the author have explained the concepts in a seamlessly simple way possible. I really loved the narrative style of the book.I can’t believe someone explained BERT with so much of in-depth detail. The book covers lot of things which I was never aware of and many different types of BERT like tinyBERT, ELECTRA, Multilingual BERT, XLM-R, and many others. If you are not getting this then definitely you are missing out a greatest content on BERT ever.
Amazon Verified review Amazon
Samuel de Zoete May 25, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Makes BERT accessible for Data Scientists without a PhD. The explanations are clear and still enough depth that’s needed when start working with Transformers.
Amazon Verified review Amazon
aditya Karampudi Feb 09, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book starts off with subtle introduction to multiple key concepts and slowly builds on the core methodologies of building NLP based neural networks. Over the years, neural networks have gone through multiple transformations, and yet the application of these architectures is limited because of the nature of data. The language is the flow of words and these sentences do not always follow a structured approach. This makes it hard to train models that can be intelligent to understand the words. The BERT tries to tackle this issue by predicting from both directions- left to right and right to left. The author tried to use multiple examples to illustrate the way NN are modeled and I thoroughly enjoyed reading this book. I recommend this book for every NLP and Deep Learning enthusiast.
Amazon Verified review Amazon
Ashwini Mar 05, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I liked this book about BERT. This is one of the great books on BERT that I have come across. The author takes detailed accounts of introduction and applications of BERT before explaining things in detail. I loved the fact that there are frameworks explaining how each and every topic works in BERT. The book is a bit mathy but great for people who want to understand things in details.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
Modal Close icon
Modal Close icon