Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some questions about MultiHeadAtttention #94

Open
SteveBetter opened this issue Jun 5, 2022 · 0 comments
Open

some questions about MultiHeadAtttention #94

SteveBetter opened this issue Jun 5, 2022 · 0 comments

Comments

@SteveBetter
Copy link

SteveBetter commented Jun 5, 2022

class MultiHeadedAttention(nn.Module):
def init(self, h, d_model, dropout=0.1):
"Take in model size and number of heads."
super(MultiHeadedAttention, self).init()
assert d_model % h == 0
# We assume d_v always equals d_k
self.d_k = d_model // h
self.h = h
self.linears = clones(nn.Linear(d_model, d_model), 4)
self.attn = None ------------------------------------------------------------------ # this should be deleted?
self.dropout = nn.Dropout(p=dropout)

def forward(self, query, key, value, mask=None):
    "Implements Figure 2"
    if mask is not None:
        # Same mask applied to all h heads.
        mask = mask.unsqueeze(1)
    nbatches = query.size(0)

    # 1) Do all the linear projections in batch from d_model => h x d_k
    query, key, value = [
        lin(x).view(nbatches, -1, self.h, self.d_k).transpose(1, 2)
        for lin, x in zip(self.mul (query, key, value))      -----------------------------# self.mul should be self.linears?
    ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant