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

Class BertForSequenceClassification

bert/modeling_bert.py:1208–1287  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1206 BERT_START_DOCSTRING,
1207)
1208class BertForSequenceClassification(BertPreTrainedModel):
1209 def __init__(self, config):
1210 super().__init__(config)
1211 self.num_labels = config.num_labels
1212
1213 self.bert = BertModel(config)
1214 self.dropout = nn.Dropout(config.hidden_dropout_prob)
1215 self.classifier = nn.Linear(config.hidden_size, config.num_labels)
1216
1217 self.init_weights()
1218
1219 @add_start_docstrings_to_callable(BERT_INPUTS_DOCSTRING.format("(batch_size, sequence_length)"))
1220 @add_code_sample_docstrings(tokenizer_class=_TOKENIZER_FOR_DOC, checkpoint="bert-base-uncased")
1221 def forward(
1222 self,
1223 input_ids=None,
1224 attention_mask=None,
1225 token_type_ids=None,
1226 position_ids=None,
1227 head_mask=None,
1228 inputs_embeds=None,
1229 labels=None,
1230 output_attentions=None,
1231 output_hidden_states=None,
1232 ):
1233 r"""
1234 labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`, defaults to :obj:`None`):
1235 Labels for computing the sequence classification/regression loss.
1236 Indices should be in :obj:`[0, ..., config.num_labels - 1]`.
1237 If :obj:`config.num_labels == 1` a regression loss is computed (Mean-Square loss),
1238 If :obj:`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
1239
1240 Returns:
1241 :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.BertConfig`) and inputs:
1242 loss (:obj:`torch.FloatTensor` of shape :obj:`(1,)`, `optional`, returned when :obj:`label` is provided):
1243 Classification (or regression if config.num_labels==1) loss.
1244 logits (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, config.num_labels)`):
1245 Classification (or regression if config.num_labels==1) scores (before SoftMax).
1246 hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``):
1247 Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer)
1248 of shape :obj:`(batch_size, sequence_length, hidden_size)`.
1249
1250 Hidden-states of the model at the output of each layer plus the initial embedding outputs.
1251 attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``):
1252 Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape
1253 :obj:`(batch_size, num_heads, sequence_length, sequence_length)`.
1254
1255 Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
1256 heads.
1257 """
1258
1259 outputs = self.bert(
1260 input_ids,
1261 attention_mask=attention_mask,
1262 token_type_ids=token_type_ids,
1263 position_ids=position_ids,
1264 head_mask=head_mask,
1265 inputs_embeds=inputs_embeds,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected