r""" Return: :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.BertConfig`) and inputs: last_hidden_state (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, hidden_size)`): Sequence of
(
self,
input_ids=None,
attention_mask=None,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
encoder_hidden_states=None,
encoder_attention_mask=None,
output_attentions=None,
output_hidden_states=None,
)
| 668 | @add_start_docstrings_to_callable(BERT_INPUTS_DOCSTRING.format("(batch_size, sequence_length)")) |
| 669 | @add_code_sample_docstrings(tokenizer_class=_TOKENIZER_FOR_DOC, checkpoint="bert-base-uncased") |
| 670 | def forward( |
| 671 | self, |
| 672 | input_ids=None, |
| 673 | attention_mask=None, |
| 674 | token_type_ids=None, |
| 675 | position_ids=None, |
| 676 | head_mask=None, |
| 677 | inputs_embeds=None, |
| 678 | encoder_hidden_states=None, |
| 679 | encoder_attention_mask=None, |
| 680 | output_attentions=None, |
| 681 | output_hidden_states=None, |
| 682 | ): |
| 683 | r""" |
| 684 | Return: |
| 685 | :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.BertConfig`) and inputs: |
| 686 | last_hidden_state (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, hidden_size)`): |
| 687 | Sequence of hidden-states at the output of the last layer of the model. |
| 688 | pooler_output (:obj:`torch.FloatTensor`: of shape :obj:`(batch_size, hidden_size)`): |
| 689 | Last layer hidden-state of the first token of the sequence (classification token) |
| 690 | further processed by a Linear layer and a Tanh activation function. The Linear |
| 691 | layer weights are trained from the next sentence prediction (classification) |
| 692 | objective during pre-training. |
| 693 | |
| 694 | This output is usually *not* a good summary |
| 695 | of the semantic content of the input, you're often better with averaging or pooling |
| 696 | the sequence of hidden-states for the whole input sequence. |
| 697 | hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``): |
| 698 | Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) |
| 699 | of shape :obj:`(batch_size, sequence_length, hidden_size)`. |
| 700 | |
| 701 | Hidden-states of the model at the output of each layer plus the initial embedding outputs. |
| 702 | attentions (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``): |
| 703 | Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape |
| 704 | :obj:`(batch_size, num_heads, sequence_length, sequence_length)`. |
| 705 | |
| 706 | Attentions weights after the attention softmax, used to compute the weighted average in the self-attention |
| 707 | heads. |
| 708 | """ |
| 709 | output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions |
| 710 | output_hidden_states = ( |
| 711 | output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states |
| 712 | ) |
| 713 | |
| 714 | if input_ids is not None and inputs_embeds is not None: |
| 715 | raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") |
| 716 | elif input_ids is not None: |
| 717 | input_shape = input_ids.size() |
| 718 | elif inputs_embeds is not None: |
| 719 | input_shape = inputs_embeds.size()[:-1] |
| 720 | else: |
| 721 | raise ValueError("You have to specify either input_ids or inputs_embeds") |
| 722 | |
| 723 | device = input_ids.device if input_ids is not None else inputs_embeds.device |
| 724 | |
| 725 | if attention_mask is None: |
| 726 | attention_mask = torch.ones(input_shape, device=device) |
| 727 | if token_type_ids is None: |
nothing calls this directly
no test coverage detected