class_docstr: **Biaffine Dependency Parser** 实现。
  论文参考 `Deep Biaffine Attention for Neural Dependency Parsing (Dozat and Manning, 2016) <https://arxiv.org/abs/1611.01734>`_ 。
  
  :param embed: 单词词典，支持以下几种输入类型：
  
          - ``tuple(num_embedings, embedding_dim)``，即 embedding 的大小和每个词的维度，此时将随机初始化一个 :class:`torch.nn.Embedding` 实例；
          - :class:`torch.nn.Embedding` 或 **fastNLP** 的 ``Embedding`` 对象，此时就以传入的对象作为 embedding；
          - :class:`numpy.ndarray` ，将使用传入的 ndarray 作为 Embedding 初始化；
          - :class:`torch.Tensor`，此时将使用传入的值作为 Embedding 初始化；
  
  :param pos_vocab_size: part-of-speech 词典大小
  :param pos_emb_dim: part-of-speech 向量维度
  :param num_label: 边的类别个数
  :param rnn_layers: rnn encoder 的层数
  :param rnn_hidden_size: rnn encoder 的隐状态维度
  :param arc_mlp_size: 边预测的 MLP 维度
  :param label_mlp_size: 类别预测的 MLP 维度
  :param dropout: dropout 概率
  :param encoder: encoder 类别，可选 ``['lstm', 'var-lstm', 'transformer']``。
  :param use_greedy_infer: 是否在 inference 时使用 :meth:`贪心算法 <GraphParser.greedy_decoder>` ，若为 ``False`` ，
      将使用更加精确但相对缓慢的 :meth:`MST算法 <GraphParser.mst_decoder>` 。
class_docstr_tok: ["*","*","Biaffine","Dependency","Parser","*","*","\u5b9e\u73b0\u3002","\u8bba\u6587\u53c2\u8003","`","Deep","Biaffine","Attention","for","Neural","Dependency","Parsing","(","Dozat","and","Manning",",","2016",")","<","https",":","\/\/arxiv.org\/abs\/1611.01734",">","`","_","\u3002",":","param","embed",":","\u5355\u8bcd\u8bcd\u5178\uff0c\u652f\u6301\u4ee5\u4e0b\u51e0\u79cd\u8f93\u5165\u7c7b\u578b\uff1a","-","``","tuple","(","num_embedings",",","embedding_dim",")","``","\uff0c\u5373","embedding","\u7684\u5927\u5c0f\u548c\u6bcf\u4e2a\u8bcd\u7684\u7ef4\u5ea6\uff0c\u6b64\u65f6\u5c06\u968f\u673a\u521d\u59cb\u5316\u4e00\u4e2a",":","class",":","`","torch.nn.Embedding","`","\u5b9e\u4f8b\uff1b","-",":","class",":","`","torch.nn.Embedding","`","\u6216","*","*","fastNLP","*","*","\u7684","``","Embedding","``","\u5bf9\u8c61\uff0c\u6b64\u65f6\u5c31\u4ee5\u4f20\u5165\u7684\u5bf9\u8c61\u4f5c\u4e3a","embedding\uff1b","-",":","class",":","`","numpy.ndarray","`","\uff0c\u5c06\u4f7f\u7528\u4f20\u5165\u7684","ndarray","\u4f5c\u4e3a","Embedding","\u521d\u59cb\u5316\uff1b","-",":","class",":","`","torch.Tensor","`","\uff0c\u6b64\u65f6\u5c06\u4f7f\u7528\u4f20\u5165\u7684\u503c\u4f5c\u4e3a","Embedding","\u521d\u59cb\u5316\uff1b",":","param","pos_vocab_size",":","part-of-speech","\u8bcd\u5178\u5927\u5c0f",":","param","pos_emb_dim",":","part-of-speech","\u5411\u91cf\u7ef4\u5ea6",":","param","num_label",":","\u8fb9\u7684\u7c7b\u522b\u4e2a\u6570",":","param","rnn_layers",":","rnn","encoder","\u7684\u5c42\u6570",":","param","rnn_hidden_size",":","rnn","encoder","\u7684\u9690\u72b6\u6001\u7ef4\u5ea6",":","param","arc_mlp_size",":","\u8fb9\u9884\u6d4b\u7684","MLP","\u7ef4\u5ea6",":","param","label_mlp_size",":","\u7c7b\u522b\u9884\u6d4b\u7684","MLP","\u7ef4\u5ea6",":","param","dropout",":","dropout","\u6982\u7387",":","param","encoder",":","encoder","\u7c7b\u522b\uff0c\u53ef\u9009","``","[","'lstm","'",",","'var-lstm","'",",","'transformer","'","]","``","\u3002",":","param","use_greedy_infer",":","\u662f\u5426\u5728","inference","\u65f6\u4f7f\u7528",":","meth",":","`","\u8d2a\u5fc3\u7b97\u6cd5","<","GraphParser.greedy_decoder",">","`","\uff0c\u82e5\u4e3a","``","False","``","\uff0c","\u5c06\u4f7f\u7528\u66f4\u52a0\u7cbe\u786e\u4f46\u76f8\u5bf9\u7f13\u6162\u7684",":","meth",":","`","MST\u7b97\u6cd5","<","GraphParser.mst_decoder",">","`","\u3002"]
docstr: 计算 parser 的 loss
  
  :param pred1: 边预测 logits，形状为 ``[batch_size, seq_len, seq_len]``
  :param pred2: **label** 预测 logits，形状为 ``[batch_size, seq_len, num_label]``
  :param target1: 真实边的标注，形状为 ``[batch_size, seq_len]``
  :param target2: 真实类别的标注，形状为 ``[batch_size, seq_len]``
  :param seq_len: 真实目标的长度，形状为 ``[batch_size, seq_len]``
  :return: 计算出的 loss。
docstr_tok: ["\u8ba1\u7b97","parser","\u7684","loss",":","param","pred1",":","\u8fb9\u9884\u6d4b","logits\uff0c\u5f62\u72b6\u4e3a","``","[","batch_size",",","seq_len",",","seq_len","]","``",":","param","pred2",":","*","*","label","*","*","\u9884\u6d4b","logits\uff0c\u5f62\u72b6\u4e3a","``","[","batch_size",",","seq_len",",","num_label","]","``",":","param","target1",":","\u771f\u5b9e\u8fb9\u7684\u6807\u6ce8\uff0c\u5f62\u72b6\u4e3a","``","[","batch_size",",","seq_len","]","``",":","param","target2",":","\u771f\u5b9e\u7c7b\u522b\u7684\u6807\u6ce8\uff0c\u5f62\u72b6\u4e3a","``","[","batch_size",",","seq_len","]","``",":","param","seq_len",":","\u771f\u5b9e\u76ee\u6807\u7684\u957f\u5ea6\uff0c\u5f62\u72b6\u4e3a","``","[","batch_size",",","seq_len","]","``",":","return",":","\u8ba1\u7b97\u51fa\u7684","loss\u3002"]
code: def loss(pred1, pred2, target1, target2, seq_len):
      r"""
      计算 parser 的 loss
  
      :param pred1: 边预测 logits，形状为 ``[batch_size, seq_len, seq_len]``
      :param pred2: **label** 预测 logits，形状为 ``[batch_size, seq_len, num_label]``
      :param target1: 真实边的标注，形状为 ``[batch_size, seq_len]``
      :param target2: 真实类别的标注，形状为 ``[batch_size, seq_len]``
      :param seq_len: 真实目标的长度，形状为 ``[batch_size, seq_len]``
      :return: 计算出的 loss。
      """
  
      batch_size, length, _ = pred1.shape
      mask = seq_len_to_mask(seq_len, max_len=length)
      flip_mask = mask.eq(False)
      _arc_pred = pred1.clone()
      _arc_pred = _arc_pred.masked_fill(
          flip_mask.unsqueeze(1), -float("inf")
      )
      arc_logits = F.log_softmax(_arc_pred, dim=2)
      label_logits = F.log_softmax(pred2, dim=2)
      batch_index = torch.arange(
          batch_size, device=arc_logits.device, dtype=torch.long
      ).unsqueeze(1)
      child_index = torch.arange(
          length, device=arc_logits.device, dtype=torch.long
      ).unsqueeze(0)
      arc_loss = arc_logits[batch_index, child_index, target1]
      label_loss = label_logits[batch_index, child_index, target2]
  
      arc_loss = arc_loss.masked_fill(flip_mask, 0)
      label_loss = label_loss.masked_fill(flip_mask, 0)
      arc_nll = -arc_loss.mean()
      label_nll = -label_loss.mean()
      return arc_nll + label_nll
  
imports: from collections import defaultdict
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from ...core.utils import seq_len_to_mask
  from ...embeddings.torch.utils import get_embeddings
  from ...modules.torch.dropout import TimestepDropout
  from ...modules.torch.encoder.transformer import TransformerEncoder
  from ...modules.torch.encoder.variational_rnn import VarLSTM
label_desc: Use image node_id 5 for calling the BiaffineParser obj's underlying member method code with example usage: obj.loss(pred1, pred2, target1, target2, seq_len) and returns: unknown