r""" labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`, defaults to :obj:`None`): Labels for computing the sequence classification/regression loss. Indices should be in :obj:`[0, ..., config.num_labels - 1]`. If :obj:`config.num_lab
(
self,
input_ids=None,
attention_mask=None,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
labels=None,
output_attentions=None,
output_hidden_states=None,
)
| 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, |
| 1266 | output_attentions=output_attentions, |
| 1267 | output_hidden_states=output_hidden_states, |
| 1268 | ) |
| 1269 | |
| 1270 | pooled_output = outputs[1] |
| 1271 | |
| 1272 | pooled_output = self.dropout(pooled_output) |
| 1273 | logits = self.classifier(pooled_output) |
| 1274 | |
| 1275 | outputs = (logits,) + outputs[2:] # add hidden states and attention if they are here |
| 1276 | |
| 1277 | if labels is not None: |
| 1278 | if self.num_labels == 1: |
nothing calls this directly
no outgoing calls
no test coverage detected