original_docstring: Compute 'Scaled Dot-Product Attention
docstring: Compute 'Scaled Dot-Product Attention
docstring_tokens: Compute ' Scaled Dot - Product Attention
code: def attention(query, key, value, mask=None, dropout=None):
      "Compute 'Scaled Dot-Product Attention'"
      d_k = query.size(-1)
      scores = torch.matmul(query, key.transpose(-2, -1)) / math.sqrt(d_k)
      if mask is not None:
          scores = scores.masked_fill(mask == 0, -1e9)
      prob_attn = F.log_softmax(scores, dim=-1)
      if dropout is not None:
          prob_attn = dropout(prob_attn)
      return torch.matmul(scores, value), prob_attn
code_tokens: def attention ( query , key , value , mask = None , dropout = None ) : d_k = query . size ( - 1 ) scores = torch . matmul ( query , key . transpose ( - 2 , - 1 ) ) / math . sqrt ( d_k ) if mask is not None : scores = scores . masked_fill ( mask == 0 , - 1e9 ) prob_attn = F . log_softmax ( scores , dim = - 1 ) if dropout is not None : prob_attn = dropout ( prob_attn ) return torch . matmul ( scores , value ) , prob_attn
short_docstring: Compute 'Scaled Dot-Product Attention
short_docstring_tokens: Compute ' Scaled Dot - Product Attention
comment: "Compute 'Scaled Dot-Product Attention'"