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

Class BertForTokenClassification

bert/modeling_bert.py:1389–1469  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1387 BERT_START_DOCSTRING,
1388)
1389class BertForTokenClassification(BertPreTrainedModel):
1390 def __init__(self, config):
1391 super().__init__(config)
1392 self.num_labels = config.num_labels
1393
1394 self.bert = BertModel(config)
1395 self.dropout = nn.Dropout(config.hidden_dropout_prob)
1396 self.classifier = nn.Linear(config.hidden_size, config.num_labels)
1397
1398 self.init_weights()
1399
1400 @add_start_docstrings_to_callable(BERT_INPUTS_DOCSTRING.format("(batch_size, sequence_length)"))
1401 @add_code_sample_docstrings(tokenizer_class=_TOKENIZER_FOR_DOC, checkpoint="bert-base-uncased")
1402 def forward(
1403 self,
1404 input_ids=None,
1405 attention_mask=None,
1406 token_type_ids=None,
1407 position_ids=None,
1408 head_mask=None,
1409 inputs_embeds=None,
1410 labels=None,
1411 output_attentions=None,
1412 output_hidden_states=None,
1413 ):
1414 r"""
1415 labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`, defaults to :obj:`None`):
1416 Labels for computing the token classification loss.
1417 Indices should be in ``[0, ..., config.num_labels - 1]``.
1418
1419 Returns:
1420 :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.BertConfig`) and inputs:
1421 loss (:obj:`torch.FloatTensor` of shape :obj:`(1,)`, `optional`, returned when ``labels`` is provided) :
1422 Classification loss.
1423 scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, config.num_labels)`)
1424 Classification scores (before SoftMax).
1425 hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``):
1426 Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer)
1427 of shape :obj:`(batch_size, sequence_length, hidden_size)`.
1428
1429 Hidden-states of the model at the output of each layer plus the initial embedding outputs.
1430 attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``):
1431 Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape
1432 :obj:`(batch_size, num_heads, sequence_length, sequence_length)`.
1433
1434 Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
1435 heads.
1436 """
1437
1438 outputs = self.bert(
1439 input_ids,
1440 attention_mask=attention_mask,
1441 token_type_ids=token_type_ids,
1442 position_ids=position_ids,
1443 head_mask=head_mask,
1444 inputs_embeds=inputs_embeds,
1445 output_attentions=output_attentions,
1446 output_hidden_states=output_hidden_states,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected