MCPcopy Create free account
hub / github.com/NJUNLP/GTS / attention

Function attention

code/NNModel/attention_module.py:8–18  ·  view source on GitHub ↗

Compute 'Scaled Dot Product Attention

(query, key, value, mask=None, dropout=None)

Source from the content-addressed store, hash-verified

6
7
8def attention(query, key, value, mask=None, dropout=None):
9 "Compute 'Scaled Dot Product Attention'"
10 d_k = query.size(-1)
11 scores = torch.matmul(query, key.transpose(-2, -1)) \
12 / math.sqrt(d_k)
13 if mask is not None:
14 scores = scores.masked_fill(mask == 0, -1e9)
15 p_attn = F.softmax(scores, dim = -1)
16 if dropout is not None:
17 p_attn = dropout(p_attn)
18 return torch.matmul(p_attn, value), p_attn
19
20
21def clones(module, N):

Callers 1

forwardMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected