class_docstr: Multi-Head Attention layer.
  
  Args:
      n_head (int): The number of heads.
      n_feat (int): The number of features.
      dropout_rate (float): Dropout rate.
class_docstr_tok: ["Multi-Head","Attention","layer",".","Args",":","n_head","(","int",")",":","The","number","of","heads",".","n_feat","(","int",")",":","The","number","of","features",".","dropout_rate","(","float",")",":","Dropout","rate","."]
docstr: Compute attention context vector.
  
  Args:
      value (torch.Tensor): Transformed value (#batch, n_head, time2, d_k).
      scores (torch.Tensor): Attention score (#batch, n_head, time1, time2).
      mask (torch.Tensor): Mask (#batch, 1, time2) or (#batch, time1, time2).
  
  Returns:
      torch.Tensor: Transformed value (#batch, time1, d_model)
          weighted by the attention score (#batch, time1, time2).
docstr_tok: ["Compute","attention","context","vector",".","Args",":","value","(","torch.Tensor",")",":","Transformed","value","(","#","batch",",","n_head",",","time2",",","d_k",")",".","scores","(","torch.Tensor",")",":","Attention","score","(","#","batch",",","n_head",",","time1",",","time2",")",".","mask","(","torch.Tensor",")",":","Mask","(","#","batch",",","1",",","time2",")","or","(","#","batch",",","time1",",","time2",")",".","Returns",":","torch.Tensor",":","Transformed","value","(","#","batch",",","time1",",","d_model",")","weighted","by","the","attention","score","(","#","batch",",","time1",",","time2",")","."]
code: def forward_attention(self, value, scores, mask):
      """Compute attention context vector.
  
      Args:
          value (torch.Tensor): Transformed value (#batch, n_head, time2, d_k).
          scores (torch.Tensor): Attention score (#batch, n_head, time1, time2).
          mask (torch.Tensor): Mask (#batch, 1, time2) or (#batch, time1, time2).
  
      Returns:
          torch.Tensor: Transformed value (#batch, time1, d_model)
              weighted by the attention score (#batch, time1, time2).
  
      """
      n_batch = value.size(0)
      if mask is not None:
          mask = mask.unsqueeze(1).eq(0)  # (batch, 1, *, time2)
          min_value = torch.finfo(scores.dtype).min
          scores = scores.masked_fill(mask, min_value)
          self.attn = torch.softmax(scores, dim=-1).masked_fill(
              mask, 0.0
          )  # (batch, head, time1, time2)
      else:
          self.attn = torch.softmax(
              scores, dim=-1
          )  # (batch, head, time1, time2)
  
      p_attn = self.dropout(self.attn)
      x = torch.matmul(p_attn, value)  # (batch, head, time1, d_k)
      x = (
          x.transpose(1, 2)
          .contiguous()
          .view(n_batch, -1, self.h * self.d_k)
      )  # (batch, time1, d_model)
  
      return self.linear_out(x)
  
file_path: espnet/espnet/nets/pytorch_backend/transformer/attention.py
imports: import math
  import torch
  from torch import nn
label_desc: Use image node_id 3 for calling the MultiHeadedAttention obj's underlying member method code with example usage: obj.forward_attention(value, scores, mask) and returns: self