Skip to content

Attention Mechanism

James Clow edited this page Feb 26, 2023 · 2 revisions

In GPT-J, the attention mechanism is used to selectively attend to different parts of the input sequence based on their relevance to the current context. This is particularly useful in processing questions, where the relevant parts of the input sequence may depend on the specific details of the question.

The attention mechanism in GPT-J is based on multi-head attention, which means that the input sequence is divided into multiple "heads", each of which attends to a different part of the sequence. The attention mechanism consists of three main steps:

  1. Query, key, and value matrices: The input sequence is transformed into three matrices: the query matrix, the key matrix, and the value matrix. The query matrix represents the current context or question, while the key and value matrices represent the input sequence. Each matrix is obtained by multiplying the input sequence with learned weight matrices.

  2. Dot-product attention: For each head, the attention mechanism computes a similarity score between the query vector and the key vectors by taking the dot product between them. The similarity scores are then normalized using a softmax function to obtain attention weights for each key vector. The attention weights represent the relevance of each part of the input sequence to the current context or question. Finally, the value vectors are weighted by the attention weights and summed to obtain the output vector for each head.

  3. Contatination and Linear Transformation: The output vectors from each head are concatenated and transformed using a linear layer to obtain the final output vector. This vector represents the attended representation of the input sequence with respect to the current context or question.

In the context of processing a question, the query matrix represents the encoded form of the question, while the key and value matrices represent the encoded form of the input sequence (e.g., a document or paragraph). The attention mechanism computes the relevance of each part of the input sequence to the question by comparing the query vector with the key vectors. The output vector represents a summary of the input sequence that is relevant to the question. This attended representation can then be used to generate an answer to the question.

Clone this wiki locally