From f0c8fb94115edd485ea2520cb72bc5c73eb18fbb Mon Sep 17 00:00:00 2001 From: yachty66 Date: Sat, 11 Nov 2023 00:28:23 -0800 Subject: [PATCH] added final readme --- README.md | 36 ++++++++++++------------ experiments.py => example.py | 2 +- gpt_pdf_md/README.md | 36 ++++++++++++------------ gpt_pdf_md/setup.py | 2 +- output.md | 54 +++++++++++++++++------------------- 5 files changed, 61 insertions(+), 69 deletions(-) rename experiments.py => example.py (91%) diff --git a/README.md b/README.md index ca1ef70..6125efa 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,26 @@ # gpt_pdf_md -gpt_pdf_md is a Python package which uses GPT-4V and other tools to convert pdf into Markdown files. current limitation of raw gpt-4v is that that it does not support pdf documents in the api and if prompted to convert text which contains figures to markdown, figures are not getting not converted correctly because the image url in the markdown is missing. IT TURNS OUT gpt_pdf_md IS EVEN COMING CLOSE TO OCR QUALITY OF MATHPIX! +`gpt_pdf_md` is a Python package that leverages GPT-4V and other tools to convert PDF files into Markdown. The current limitation of raw GPT-4V is that it does not support PDF documents in the API. Additionally, when prompted to convert text containing figures to Markdown, the figures are not converted correctly due to missing image URLs in the Markdown. However, `gpt_pdf_md` is coming close to the OCR quality of Mathpix! ## Features - Extracts figures from PDF files using the `pdffigures2` Scala library. -- Converts PDF pages to images and uploads them to Google Cloud Bucket. -- Utilizes GPT-4V Vision to generate Markdown content from pdf an than inserts image urls into markdown. +- Converts PDF pages to images and uploads them to a Google Cloud Bucket. +- Utilizes GPT-4V Vision to generate Markdown content from a PDF and then inserts image URLs into the Markdown. ## Additional Dependencies -This package requires the `pdffigures2` Scala library to extract figures from PDF files. You need to have all necessary dependencies installed for the library https://github.com/allenai/pdffigures2. (this can be quite a hassle because parts of the library are written in scala so you need to have the right version of java and scala installed - we are looking for an alternative, more easy going way to extract images from a pdf, if youn have any ideas, feel free open an [issue](https://github.com/yachty66/gpt_vision_plus/issues) on that) +This package requires the `pdffigures2` Scala library to extract figures from PDF files. You need to have all necessary dependencies installed for the library. You can find more information [here](https://github.com/allenai/pdffigures2). Please note that this can be quite a hassle because parts of the library are written in Scala, so you need to have the correct versions of Java and Scala installed. We are looking for an alternative, more straightforward way to extract images from a PDF. If you have any ideas, feel free to open an [issue](https://github.com/yachty66/gpt_pdf_md/issues). ## Installation -Once you have `pdffigures2` setup you can install gpt_pdf_md via pip: +Once you have `pdffigures2` set up, you can install `gpt_pdf_md` via pip: ```bash pip install gpt-pdf-md ``` -Configure the required environment variables in your .env file without spaces or unnecessary quotes: +Configure the required environment variables in your `.env` file without spaces or unnecessary quotes: ```env OPENAI_API_KEY=open_ai_key @@ -28,11 +28,11 @@ GOOGLE_ID=google_project_id GOOGLE_BUCKET=google_bucket_name ``` -NOTE: the project requires a public google bucket where the images which later are getting rendered in the markdown are getting uploaded to. +NOTE: This project requires a public Google bucket where the images, which are later rendered in the Markdown, are uploaded. ## Usage -To process a PDF and generate Markdown content its important that the python file is in the same directory than the `pdffigures2` folder. You can use the gpt_pdf_md as following: +To process a PDF and generate Markdown content, it's important that the Python file is in the same directory as the `pdffigures2` folder. You can use `gpt_pdf_md` as follows: ```python from gpt_pdf_md.reader import process_pdf @@ -46,21 +46,21 @@ GOOGLE_ID = os.getenv('GOOGLE_ID') GOOGLE_BUCKET = os.getenv('GOOGLE_BUCKET') absolute_path = os.path.dirname(os.path.abspath(__file__)) -#absolute path to pdf file +# Absolute path to the PDF file PDF = absolute_path + "/example.pdf" -#absolute padth to pdffigures2 +# Absolute path to pdffigures2 PDFFIGURES2_PATH = absolute_path + "/pdffigures2/" process_pdf(PDF, PDFFIGURES2_PATH, OPENAI_API_KEY, GOOGLE_ID, GOOGLE_BUCKET) ``` -This will process the specified PDF and output a Markdown file with the extracted information in the same directory. An example is the `output.md` file which is the converted result of `example.pdf` +This will process the specified PDF and output a Markdown file with the extracted information in the same directory. An example is the `output.md` file, which is the converted result of `example.pdf` created by running the `example.py` script. -## Next steps +## Next Steps -- [ ] try rust [vortex](https://github.com/omkar-mohanty/vortex) for pdf image extraction -- [ ] use gpt-4 128k for final formatting of markdown -- [ ] clearer readme to make it easier for everyone to use the python package -- [ ] error handling +- [ ] Try Rust [vortex](https://github.com/omkar-mohanty/vortex) for PDF image extraction +- [ ] Use GPT-4 128k for final formatting of Markdown +- [ ] Create a clearer README to make it easier for everyone to use the Python package +- [ ] Improve error handling ## Contributing & Support @@ -68,6 +68,4 @@ We welcome contributions! Please open an issue or submit a pull request on our G ## License -This project is licensed under the terms of the [MIT License](gpt_pdf_md/LICENSE). - - +This project is licensed under the terms of the [MIT License](gpt_pdf_md/LICENSE). \ No newline at end of file diff --git a/experiments.py b/example.py similarity index 91% rename from experiments.py rename to example.py index 0080df3..c087e13 100644 --- a/experiments.py +++ b/example.py @@ -1,4 +1,4 @@ -from gptpdfreader.reader import process_pdf +from gpt_pdf_md.reader import process_pdf import os from dotenv import load_dotenv diff --git a/gpt_pdf_md/README.md b/gpt_pdf_md/README.md index ca1ef70..6125efa 100644 --- a/gpt_pdf_md/README.md +++ b/gpt_pdf_md/README.md @@ -1,26 +1,26 @@ # gpt_pdf_md -gpt_pdf_md is a Python package which uses GPT-4V and other tools to convert pdf into Markdown files. current limitation of raw gpt-4v is that that it does not support pdf documents in the api and if prompted to convert text which contains figures to markdown, figures are not getting not converted correctly because the image url in the markdown is missing. IT TURNS OUT gpt_pdf_md IS EVEN COMING CLOSE TO OCR QUALITY OF MATHPIX! +`gpt_pdf_md` is a Python package that leverages GPT-4V and other tools to convert PDF files into Markdown. The current limitation of raw GPT-4V is that it does not support PDF documents in the API. Additionally, when prompted to convert text containing figures to Markdown, the figures are not converted correctly due to missing image URLs in the Markdown. However, `gpt_pdf_md` is coming close to the OCR quality of Mathpix! ## Features - Extracts figures from PDF files using the `pdffigures2` Scala library. -- Converts PDF pages to images and uploads them to Google Cloud Bucket. -- Utilizes GPT-4V Vision to generate Markdown content from pdf an than inserts image urls into markdown. +- Converts PDF pages to images and uploads them to a Google Cloud Bucket. +- Utilizes GPT-4V Vision to generate Markdown content from a PDF and then inserts image URLs into the Markdown. ## Additional Dependencies -This package requires the `pdffigures2` Scala library to extract figures from PDF files. You need to have all necessary dependencies installed for the library https://github.com/allenai/pdffigures2. (this can be quite a hassle because parts of the library are written in scala so you need to have the right version of java and scala installed - we are looking for an alternative, more easy going way to extract images from a pdf, if youn have any ideas, feel free open an [issue](https://github.com/yachty66/gpt_vision_plus/issues) on that) +This package requires the `pdffigures2` Scala library to extract figures from PDF files. You need to have all necessary dependencies installed for the library. You can find more information [here](https://github.com/allenai/pdffigures2). Please note that this can be quite a hassle because parts of the library are written in Scala, so you need to have the correct versions of Java and Scala installed. We are looking for an alternative, more straightforward way to extract images from a PDF. If you have any ideas, feel free to open an [issue](https://github.com/yachty66/gpt_pdf_md/issues). ## Installation -Once you have `pdffigures2` setup you can install gpt_pdf_md via pip: +Once you have `pdffigures2` set up, you can install `gpt_pdf_md` via pip: ```bash pip install gpt-pdf-md ``` -Configure the required environment variables in your .env file without spaces or unnecessary quotes: +Configure the required environment variables in your `.env` file without spaces or unnecessary quotes: ```env OPENAI_API_KEY=open_ai_key @@ -28,11 +28,11 @@ GOOGLE_ID=google_project_id GOOGLE_BUCKET=google_bucket_name ``` -NOTE: the project requires a public google bucket where the images which later are getting rendered in the markdown are getting uploaded to. +NOTE: This project requires a public Google bucket where the images, which are later rendered in the Markdown, are uploaded. ## Usage -To process a PDF and generate Markdown content its important that the python file is in the same directory than the `pdffigures2` folder. You can use the gpt_pdf_md as following: +To process a PDF and generate Markdown content, it's important that the Python file is in the same directory as the `pdffigures2` folder. You can use `gpt_pdf_md` as follows: ```python from gpt_pdf_md.reader import process_pdf @@ -46,21 +46,21 @@ GOOGLE_ID = os.getenv('GOOGLE_ID') GOOGLE_BUCKET = os.getenv('GOOGLE_BUCKET') absolute_path = os.path.dirname(os.path.abspath(__file__)) -#absolute path to pdf file +# Absolute path to the PDF file PDF = absolute_path + "/example.pdf" -#absolute padth to pdffigures2 +# Absolute path to pdffigures2 PDFFIGURES2_PATH = absolute_path + "/pdffigures2/" process_pdf(PDF, PDFFIGURES2_PATH, OPENAI_API_KEY, GOOGLE_ID, GOOGLE_BUCKET) ``` -This will process the specified PDF and output a Markdown file with the extracted information in the same directory. An example is the `output.md` file which is the converted result of `example.pdf` +This will process the specified PDF and output a Markdown file with the extracted information in the same directory. An example is the `output.md` file, which is the converted result of `example.pdf` created by running the `example.py` script. -## Next steps +## Next Steps -- [ ] try rust [vortex](https://github.com/omkar-mohanty/vortex) for pdf image extraction -- [ ] use gpt-4 128k for final formatting of markdown -- [ ] clearer readme to make it easier for everyone to use the python package -- [ ] error handling +- [ ] Try Rust [vortex](https://github.com/omkar-mohanty/vortex) for PDF image extraction +- [ ] Use GPT-4 128k for final formatting of Markdown +- [ ] Create a clearer README to make it easier for everyone to use the Python package +- [ ] Improve error handling ## Contributing & Support @@ -68,6 +68,4 @@ We welcome contributions! Please open an issue or submit a pull request on our G ## License -This project is licensed under the terms of the [MIT License](gpt_pdf_md/LICENSE). - - +This project is licensed under the terms of the [MIT License](gpt_pdf_md/LICENSE). \ No newline at end of file diff --git a/gpt_pdf_md/setup.py b/gpt_pdf_md/setup.py index a4d915c..d50c728 100644 --- a/gpt_pdf_md/setup.py +++ b/gpt_pdf_md/setup.py @@ -5,7 +5,7 @@ setup( name='gpt_pdf_md', - version='0.1', + version='0.2', packages=find_packages(), description='A Python package that utilizes GPT-4V and other tools to convert PDFs into Markdown files.', long_description=open('README.md').read(), diff --git a/output.md b/output.md index 14d6646..75e6dd7 100644 --- a/output.md +++ b/output.md @@ -20,7 +20,7 @@ Llion Jones* Google Research llion@google.com -Aidan N. Gomez* † +Aidan N. Gomez† University of Toronto aidan@cs.toronto.edu @@ -28,63 +28,59 @@ Lukasz Kaiser* Google Brain lukaszkaiser@google.com -Illia Polosukhin* † +Illia Polosukhin† illiia.polosukhin@gmail.com --- -## Abstract - -The dominant sequence transduction models are based on complex recurrent or convolutional neural networks that include an encoder and a decoder. The best performing models also connect the encoder and decoder through an attention mechanism. We propose a new simple network architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two machine translation tasks show these models to be superior in quality while being more parallelizable and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task, improving over the existing best results, including ensembles, by over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training costs of the best models from the literature. We show that the Transformer generalizes well to other tasks by applying it successfully to English constituency parsing both with large and limited training data. - ---- - -*Equal contribution. Listing order is random. Jakob proposed replacing RNNs with self-attention and started the effort to evaluate this idea. Ashish, with Illia, designed and implemented the first Transformer models and has been crucially involved in every aspect of this work. Noam proposed scaled dot-product attention, nearly-attention and the parameter-free position representation and became the other person involved in multi-head detail. Niki designed, implemented, tuned and evaluated countless model variants in our original codebase and tensor2tensor. Llion also experimented with novel model variants, was responsible for our initial codebase, and efficient inference and visualizations. Lukasz and Aidan spent countless long days designing various parts of our rescaling tensor2tensor, replacing our earlier codebase, greatly improving results and massively accelerating implementation. +\* Equal contribution. Listing order is random. Jakob proposed replacing RNNs with self-attention and started the effort to evaluate this idea. Ashish, with Illia, designed and implemented the first Transformer models and has been crucially involved in every aspect of this work. Noam proposed scaled dot-product attention, nearly halved attention and the parameter-free position representation and became the other person involved in multi-head detail. Niki designed, implemented, tuned and evaluated countless model variants in our original codebase and tensor2tensor. Llion also experimented with novel model variants, was responsible for our initial codebase, and efficient inference and visualizations. Lukasz and Aidan spent countless long days designing various parts of our research, tensor2tensor, replacing our earlier codebase, greatly improving results and massively accelerating implementation. † Work performed while at Google Brain. ‡ Work performed while at Google Research. -31st Conference on Neural Information Processing Systems (NIPS 2017), Long Beach, CA, USA.# 1 Introduction +31st Conference on Neural Information Processing Systems (NIPS 2017), Long Beach, CA, USA.# Introduction -Recurrent neural networks, long short-term memory (LSTM) and gated recurrent (GRU) neural networks in particular, have been firmly established as state of the art approaches in sequence modeling and transduction problems such as language modeling and machine translation. Numerous efforts have since continued to push the boundaries of recurrent language models and encoder-decoder architectures. +Recurrent neural networks, long short-term memory (LSTM) [13] and gated recurrent (GRU) [7] neural networks in particular, have been firmly established as state of the art approaches in sequence modeling and transduction problems such as language modeling and machine translation [35][15]. Numerous efforts have since continued to push the boundaries of recurrent language models and encoder-decoder architectures [38][24][15]. -Recurrent models typically factor computation along the symbol positions of the input and output sequences. Aligning the positions to steps in computation time, they generate a sequence of hidden states $h_t$, as a function of the previous hidden state $h_{t-1}$ and the input for position t. This inherently sequential nature precludes parallelization within training examples, which becomes critical at longer sequence lengths, as memory constraints limit batching across examples. Recent work has achieved significant improvements in computational efficiency through factorization tricks and conditional computation, while also improving model performance in case of the latter. The fundamental constraint of sequential computation, however, remains. +Recurrent models typically factor computation along the symbol positions of the input and output sequences. Aligning the positions to steps in computation time, they generate a sequence of hidden states $h_t$, as a function of the previous hidden state $h_{t-1}$ and the input for position t. This inherently sequential nature precludes parallelization within training examples, which becomes critical at longer sequence lengths, as memory constraints limit batching across examples. Recent work has achieved significant improvements in computational efficiency through factorization tricks [21] and conditional computation [32], while also improving model performance in case of the latter. The fundamental constraint of sequential computation, however, remains. -Attention mechanisms have become an integral part of compelling sequence modeling and transduction models in various tasks, allowing modeling of dependencies without regard to their distance in the input or output sequences. In all but a few cases, however, such attention mechanisms are used in conjunction with a recurrent network. +Attention mechanisms have become an integral part of compelling sequence modeling and transduction models in various tasks, allowing modeling of dependencies without regard to their distance in the input or output sequences [2][19]. In all but a few cases [27], however, such attention mechanisms are used in conjunction with a recurrent network. In this work we propose the Transformer, a model architecture eschewing recurrence and instead relying entirely on an attention mechanism to draw global dependencies between input and output. The Transformer allows for significantly more parallelization and can reach a new state of the art in translation quality after being trained for as little as twelve hours on eight P100 GPUs. -# 2 Background +# Background + +The goal of reducing sequential computation also forms the foundation of the Extended Neural GPU [16], ByteNet [18] and ConvS2S [9], all of which use convolutional neural networks as basic building blocks, computing hidden representations in parallel for all input and output positions. In these models, the number of operations required to relate signals from two arbitrary input or output positions grows in the distance between positions, linearly for ConvS2S and logarithmically for ByteNet. This makes it more difficult to learn dependencies between distant positions [12]. In the Transformer this is reduced to a constant number of operations, albeit at the cost of reduced effective resolution due to averaging attention-weighted positions, an effect we counteract with Multi-Head Attention as described in section 3.2. -The goal of reducing sequential computation also forms the foundation of the Extended Neural GPU, ByteNet and ConvS2S, all of which use convolutional neural networks as basic building blocks, computing hidden representations in parallel for all input and output positions. In these models, the number of operations required to relate signals from two arbitrary input or output positions grows in the distance between positions, linearly for ConvS2S and logarithmically for ByteNet. This makes it more difficult to learn dependencies between distant positions. In the Transformer this is reduced to a constant number of operations, albeit at the cost of reduced effective resolution due to averaging attention-weighted positions, an effect we counteract with Multi-Head Attention as described in section 3.2. +Self-attention, sometimes called intra-attention is an attention mechanism relating different positions of a single sequence in order to compute a representation of the sequence. Self-attention has been used successfully in a variety of tasks including reading comprehension, abstractive summarization, textual entailment and learning task-independent sentence representations [4][27][28][22]. -Self-attention, sometimes called intra-attention is an attention mechanism relating different positions of a single sequence in order to compute a representation of the sequence. Self-attention has been used successfully in a variety of tasks including reading comprehension, abstractive summarization, textual entailment and learning task-independent sentence representations. End-to-end memory networks are based on a recurrent attention mechanism instead of sequence-aligned recurrence and have been shown to perform well on simple-language question answering and language modeling tasks. +End-to-end memory networks are based on a recurrent attention mechanism instead of sequence-aligned recurrence and have been shown to perform well on simple-language question answering and language modeling tasks [34]. -To the best of our knowledge, however, the Transformer is the first transduction model relying entirely on self-attention to compute representations of its input and output without using sequence-aligned RNNs or convolution. In the following sections, we will describe the Transformer, motivate self-attention and discuss its advantages over models such as LSTM and GRU. +To the best of our knowledge, however, the Transformer is the first transduction model relying entirely on self-attention to compute representations of its input and output without using sequence-aligned RNNs or convolution. In the following sections, we will describe the Transformer, motivate self-attention and discuss its advantages over models such as [17][18] and [9]. -# 3 Model Architecture +# Model Architecture -Most competitive neural sequence transduction models have an encoder-decoder structure. Here, the encoder maps an input sequence of symbol representations $(x_1, ..., x_n)$ to a sequence of continuous representations $z = (z_1, ..., z_n)$. Given $z$, the decoder then generates an output sequence $(y_1, ..., y_m)$ of symbols one element at a time. At each step the model is auto-regressive, consuming the previously generated symbols as additional input when generating the next.![Figure 1: The Transformer - model architecture.](https://storage.googleapis.com/mathreader/main-Figure1-1.png) +Most competitive neural sequence transduction models have an encoder-decoder structure [5][2][35]. Here, the encoder maps an input sequence of symbol representations $(x_1, ..., x_n)$ to a sequence of continuous representations $z = (z_1, ..., z_n)$. Given $z$, the decoder then generates an output sequence $(y_1, ..., y_m)$ of symbols one element at a time. At each step the model is auto-regressive [10], consuming the previously generated symbols as additional input when generating the next.![Figure 1: The Transformer - model architecture.](https://storage.googleapis.com/mathreader/main-Figure1-1.png) The Transformer follows this overall architecture using stacked self-attention and point-wise, fully connected layers for both the encoder and decoder, shown in the left and right halves of Figure 1 respectively. ### 3.1 Encoder and Decoder Stacks -#### Encoder: -The encoder is composed of a stack of \( N = 6 \) identical layers. Each layer has two sub-layers. The first is a multi-head self-attention mechanism, and the second is a simple, position-wise fully connected feed-forward network. We employ a residual connection [[11]](https://arxiv.org/abs/1512.03385) around each of the two sub-layers, followed by layer normalization [[29]](https://arxiv.org/abs/1607.06450). That is, the output of each sub-layer is \( \text{LayerNorm}(x + \text{Sublayer}(x)) \), where Sublayer(x) is the function implemented by the sub-layer itself. To facilitate these residual connections, all sub-layers in the model, as well as the embedding layers, produce outputs of dimension \( d_{\text{model}} = 512 \). +**Encoder**: The encoder is composed of a stack of \( N = 6 \) identical layers. Each layer has two sub-layers. The first is a multi-head self-attention mechanism, and the second is a simple, position-wise fully connected feed-forward network. We employ a residual connection [11] around each of the two sub-layers, followed by layer normalization [2]. That is, the output of each sub-layer is LayerNorm(\(x + \text{Sublayer}(x)\)), where Sublayer(x) is the function implemented by the sub-layer itself. To facilitate these residual connections, all sub-layers in the model, as well as the embedding layers, produce outputs of dimension \(d_{\text{model}} = 512\). -#### Decoder: -The decoder is also composed of a stack of \( N = 6 \) identical layers. In addition to the two sub-layers in each encoder layer, the decoder inserts a third sub-layer, which performs multi-head attention over the output of the encoder stack. Similar to the encoder, we employ residual connections around each of the sub-layers, followed by layer normalization. We also modify the self-attention sub-layer in the decoder stack to prevent positions from attending to subsequent positions. This masking, combined with the fact that the output embeddings are offset by one position, ensures that the predictions for position \( i \) can depend only on the known outputs at positions less than \( i \). +**Decoder**: The decoder is also composed of a stack of \( N = 6 \) identical layers. In addition to the two sub-layers in each encoder layer, the decoder inserts a third sub-layer, which performs multi-head attention over the output of the encoder stack. Similar to the encoder, we employ residual connections around each of the sub-layers, followed by layer normalization. We also modify the self-attention sub-layer in the decoder stack to prevent positions from attending to subsequent positions. This masking, combined with the fact that the output embeddings are offset by one position, ensures that the predictions for position \(i\) can depend only on the known outputs at positions less than \(i\). ### 3.2 Attention -An attention function can be described as mapping a query and a set of key-value pairs to an output, where the query, keys, values, and output are all vectors. The output is computed as a weighted sum![Figure 2: (left) Scaled Dot-Product Attention. (right) Multi-Head Attention consists of several attention layers running in parallel.](https://storage.googleapis.com/mathreader/main-Figure2-1.png) +An attention function can be described as mapping a query and a set of key-value pairs to an output, where the query, keys, values, and output are all vectors. The output is computed as a weighted sum### Figure 2 Description + +![Figure 2: (left) Scaled Dot-Product Attention. (right) Multi-Head Attention consists of several attention layers running in parallel.](https://storage.googleapis.com/mathreader/main-Figure2-1.png) -of the values, where the weight assigned to each value is computed by a compatibility function of the query with the corresponding key. +*Figure 2* shows two diagrams. On the left is the "Scaled Dot-Product Attention" mechanism, and on the right is the "Multi-Head Attention" mechanism, which consists of several attention layers running in parallel. -#### 3.2.1 Scaled Dot-Product Attention +### 3.2.1 Scaled Dot-Product Attention -We call our particular attention "Scaled Dot-Product Attention" (Figure 2). The input consists of queries and keys of dimension $d_k$, and values of dimension $d_v$. We compute the dot products of the queries with all keys, divide each by $\sqrt{d_k}$, and apply a softmax function to obtain the weights on the values. +We call our particular attention "Scaled Dot-Product Attention". The input consists of queries and keys of dimension $d_k$, and values of dimension $d_v$. We compute the dot products of the queries with all keys, divide each by $\sqrt{d_k}$, and apply a softmax function to obtain the weights on the values. In practice, we compute the attention function on a set of queries simultaneously, packed together into a matrix $Q$. The keys and values are also packed together into matrices $K$ and $V$. We compute the matrix of outputs as: @@ -96,8 +92,8 @@ The two most commonly used attention functions are additive attention [2], and d While for small values of $d_k$, the two mechanisms perform similarly, additive attention outperforms dot product attention without scaling for larger values of $d_k$ [3]. We suspect that for large values of $d_k$, the dot products grow large in magnitude, pushing the softmax function into regions where it has extremely small gradients [4]. To counteract this effect, we scale the dot products by $\frac{1}{\sqrt{d_k}}$. -#### 3.2.2 Multi-Head Attention +### 3.2.2 Multi-Head Attention -Instead of performing a single attention function with $d_{\text{model}}$-dimensional keys, values and queries, we found it beneficial to linearly project the queries, keys and values h times with different, learned linear projections to $d_k$, $d_v$, and $d_{\text{model}}$ dimensions, respectively. On each of these projected versions of queries, keys and values we then perform the attention function in parallel, yielding $d_{\text{model}}$-dimensional +Instead of performing a single attention function with $d_{\text{model}}$-dimensional keys, values and queries, we found it beneficial to linearly project the queries, keys and values $h$ times with different, learned linear projections to $d_k$, $d_v$, and $d_{\text{model}}$ dimensions, respectively. On each of these projected versions of queries, keys and values we then perform the attention function in parallel, yielding $d_{\text{model}}$-dimensional output vectors. To illustrate why the dot products get large, assume that the components of $q$ and $k$ are independent random variables with mean 0 and variance 1. Then their dot product, $q \cdot k = \sum_{i=1}^{d_k} q_i k_i$, has mean 0 and variance $d_k$. \ No newline at end of file