Comments (2)
As far as I understand, your doubt is that why Q, K, V is not going through n_head
linear transformations to extract Q_i, Q_i and V_i corresponding to each head ?
=>
The answer to that is to avoid significant growth of computational cost and parametrization cost, we set d_q = d_k = d_v = d_model / n_head
. [1]
This is what the split()
function in MultiHeadAttention does, and then concat()
function essentially is a weighted combination of these heads, just like in the paper.
You can see a similar implementation in PyTorch source code as well. [2]
Anyone else reading this, please correct me if I am wrong or if there are some others benefits/reasons of using this implementation.
EDIT:
It's clearly mentioned in the paper as well:
In this work we employ
h = 8
parallel attention layers, or heads. For each of these we used_k = d_v = d_model / h = 64
. Due to the reduced dimension of each head, the total computational cost is similar to that of single-head attention with full dimensionality.
For the most faithful implementations of research papers, you should also check out labml.ai annotated pytorch implementations repository. [3]
[1] https://d2l.ai/chapter_attention-mechanisms-and-transformers/multihead-attention.html
[2] https://pytorch.org/docs/stable/_modules/torch/nn/modules/activation.html#MultiheadAttention
[3] http://nlp.seas.harvard.edu/annotated-transformer/
from transformer.
As far as I understand, your doubt is that why Q, K, V is not going through
n_head
linear transformations to extract Q_i, Q_i and V_i corresponding to each head ? => The answer to that is to avoid significant growth of computational cost and parametrization cost, we setd_q = d_k = d_v = d_model / n_head
. [1] This is what thesplit()
function in MultiHeadAttention does, and thenconcat()
function essentially is a weighted combination of these heads, just like in the paper.You can see a similar implementation in PyTorch source code as well. [2]
Anyone else reading this, please correct me if I am wrong or if there are some others benefits/reasons of using this implementation.
EDIT: It's clearly mentioned in the paper as well:
In this work we employ
h = 8
parallel attention layers, or heads. For each of these we used_k = d_v = d_model / h = 64
. Due to the reduced dimension of each head, the total computational cost is similar to that of single-head attention with full dimensionality.For the most faithful implementations of research papers, you should also check out labml.ai annotated pytorch implementations repository. [3]
[1] https://d2l.ai/chapter_attention-mechanisms-and-transformers/multihead-attention.html [2] https://pytorch.org/docs/stable/_modules/torch/nn/modules/activation.html#MultiheadAttention [3] http://nlp.seas.harvard.edu/annotated-transformer/
Thank you for your comment, but it doesn't address my question. For instance, consider a sequence, and we need to produce its embedding matrix, named X. Then, it is sent to every head and multiplied by W_q, W_k, and W_v, respectively. Now, each head generates its corresponding Q, K, and V.
However, before entering each linear layer in every head, the paper's multi-head attention illustration shows Q, K, and V instead of X, X, and X correspondingly.
from transformer.
Related Issues (17)
- Masked attention HOT 3
- Potential bug in the pad mask HOT 5
- how to get dataset HOT 4
- The experimental results have a large gap with the one in README HOT 2
- Question about implementation in the multi-head attention part HOT 3
- How to convert TorchText 0.9 to the latest version HOT 10
- how to resolve the issue ”No module named 'torch._C'“
- src_pad_idx and trg_pad_idx
- About MultiHeadAttention's split method HOT 3
- shaollow copy HOT 1
- batch.trg[j] out of index. HOT 1
- Questions regarding the implementation HOT 9
- [Bug] LayerNorm should not contain learnable parameters HOT 4
- LayerNorm implement HOT 4
- [Bug] Dropout should comes before residual connection and layer norm HOT 1
- Reporting a bug during test time HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from transformer.