MCPcopy Create free account
hub / github.com/SooLab/CGFormer / SQuADHead

Class SQuADHead

bert/modeling_utils.py:952–1066  ·  view source on GitHub ↗

r""" A SQuAD head inspired by XLNet. Parameters: config (:class:`~transformers.XLNetConfig`): Model configuration class with all the parameters of the model. Inputs: **hidden_states**: ``torch.FloatTensor`` of shape ``(batch_size, seq_len, hidden_size)`` hidden

Source from the content-addressed store, hash-verified

950
951
952class SQuADHead(nn.Module):
953 r""" A SQuAD head inspired by XLNet.
954
955 Parameters:
956 config (:class:`~transformers.XLNetConfig`): Model configuration class with all the parameters of the model.
957
958 Inputs:
959 **hidden_states**: ``torch.FloatTensor`` of shape ``(batch_size, seq_len, hidden_size)``
960 hidden states of sequence tokens
961 **start_positions**: ``torch.LongTensor`` of shape ``(batch_size,)``
962 position of the first token for the labeled span.
963 **end_positions**: ``torch.LongTensor`` of shape ``(batch_size,)``
964 position of the last token for the labeled span.
965 **cls_index**: torch.LongTensor of shape ``(batch_size,)``
966 position of the CLS token. If None, take the last token.
967 **is_impossible**: ``torch.LongTensor`` of shape ``(batch_size,)``
968 Whether the question has a possible answer in the paragraph or not.
969 **p_mask**: (`optional`) ``torch.FloatTensor`` of shape ``(batch_size, seq_len)``
970 Mask of invalid position such as query and special symbols (PAD, SEP, CLS)
971 1.0 means token should be masked.
972
973 Outputs: `Tuple` comprising various elements depending on the configuration (config) and inputs:
974 **loss**: (`optional`, returned if both ``start_positions`` and ``end_positions`` are provided) ``torch.FloatTensor`` of shape ``(1,)``:
975 Classification loss as the sum of start token, end token (and is_impossible if provided) classification losses.
976 **start_top_log_probs**: (`optional`, returned if ``start_positions`` or ``end_positions`` is not provided)
977 ``torch.FloatTensor`` of shape ``(batch_size, config.start_n_top)``
978 Log probabilities for the top config.start_n_top start token possibilities (beam-search).
979 **start_top_index**: (`optional`, returned if ``start_positions`` or ``end_positions`` is not provided)
980 ``torch.LongTensor`` of shape ``(batch_size, config.start_n_top)``
981 Indices for the top config.start_n_top start token possibilities (beam-search).
982 **end_top_log_probs**: (`optional`, returned if ``start_positions`` or ``end_positions`` is not provided)
983 ``torch.FloatTensor`` of shape ``(batch_size, config.start_n_top * config.end_n_top)``
984 Log probabilities for the top ``config.start_n_top * config.end_n_top`` end token possibilities (beam-search).
985 **end_top_index**: (`optional`, returned if ``start_positions`` or ``end_positions`` is not provided)
986 ``torch.LongTensor`` of shape ``(batch_size, config.start_n_top * config.end_n_top)``
987 Indices for the top ``config.start_n_top * config.end_n_top`` end token possibilities (beam-search).
988 **cls_logits**: (`optional`, returned if ``start_positions`` or ``end_positions`` is not provided)
989 ``torch.FloatTensor`` of shape ``(batch_size,)``
990 Log probabilities for the ``is_impossible`` label of the answers.
991 """
992
993 def __init__(self, config):
994 super().__init__()
995 self.start_n_top = config.start_n_top
996 self.end_n_top = config.end_n_top
997
998 self.start_logits = PoolerStartLogits(config)
999 self.end_logits = PoolerEndLogits(config)
1000 self.answer_class = PoolerAnswerClass(config)
1001
1002 def forward(
1003 self, hidden_states, start_positions=None, end_positions=None, cls_index=None, is_impossible=None, p_mask=None,
1004 ):
1005 outputs = ()
1006
1007 start_logits = self.start_logits(hidden_states, p_mask=p_mask)
1008
1009 if start_positions is not None and end_positions is not None:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected