Giter Club home page Giter Club logo

Comments (2)

adhiraj2001 avatar adhiraj2001 commented on May 22, 2024

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 use d_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.

sonrisa07 avatar sonrisa07 commented on May 22, 2024

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 use d_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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.