Skip to content

Commit

Permalink
Document model outputs (#5673)
Browse files Browse the repository at this point in the history
* Document model outputs

* Update docs/source/main_classes/output.rst

Co-authored-by: Lysandre Debut <lysandre@huggingface.co>

Co-authored-by: Lysandre Debut <lysandre@huggingface.co>
  • Loading branch information
sgugger and LysandreJik authored Jul 10, 2020
1 parent df983b7 commit 7fad617
Show file tree
Hide file tree
Showing 18 changed files with 267 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ conversion utilities for the following models:
:caption: Package Reference

main_classes/configuration
main_classes/output
main_classes/model
main_classes/tokenizer
main_classes/pipelines
Expand Down
141 changes: 141 additions & 0 deletions docs/source/main_classes/output.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
Model outputs
-------------

PyTorch models have outputs that are instances of subclasses of :class:`~transformers.file_utils.ModelOutput`. Those
are data structures containing all the information returned by the model, but that can also be used as tuples or
dictionaries.

Let's see of this looks on an example:

.. code-block::
from transformers import BertTokenizer, BertForSequenceClassification
import torch
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
labels = torch.tensor([1]).unsqueeze(0) # Batch size 1
outputs = model(**inputs, labels=labels)
The ``outputs`` object is a :class:`~transformers.modeling_outputs.SequenceClassifierOutput`, as we can see in the
documentation of that class below, it means it has an optional ``loss``, a ``logits`` an optional ``hidden_states`` and
an optional ``attentions`` attribute. Here we have the ``loss`` since we passed along ``labels``, but we don't have
``hidden_states`` and ``attentions`` because we didn't pass ``output_hidden_states=True`` or
``output_attentions=True``.

You can access each attribute as you would usually do, and if that attribute has not been returned by the model, you
will get ``None``. Here for instance ``outputs.loss`` is the loss computed by the model, and ``outputs.attentions`` is
``None``.

When considering our ``outputs`` object as tuple, it only considers the attributes that don't have ``None`` values.
Here for instance, it has two elements, ``loss`` then ``logits``, so

.. code-block::
outputs[:2]
will return the tuple ``(outputs.loss, outputs.logits)`` for instance.

When considering our ``outputs`` object as dictionary, it only considers the attributes that don't have ``None``
values. Here for instance, it has two keys that are ``loss`` and ``logits``.

We document here the generic model outputs that are used by more than one model type. Specific output types are
documented on their corresponding model page.

``ModelOutput``
~~~~~~~~~~~~~~~

.. autoclass:: transformers.file_utils.ModelOutput
:members:

``BaseModelOutput``
~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.BaseModelOutput
:members:

``BaseModelOutputWithPooling``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.BaseModelOutputWithPooling
:members:

``BaseModelOutputWithPast``
~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.BaseModelOutputWithPast
:members:

``Seq2SeqModelOutput``
~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.Seq2SeqModelOutput
:members:

``CausalLMOutput``
~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.CausalLMOutput
:members:

``CausalLMOutputWithPast``
~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.CausalLMOutputWithPast
:members:

``MaskedLMOutput``
~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.MaskedLMOutput
:members:

``Seq2SeqLMOutput``
~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.Seq2SeqLMOutput
:members:

``NextSentencePredictorOutput``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.NextSentencePredictorOutput
:members:

``SequenceClassifierOutput``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.SequenceClassifierOutput
:members:

``Seq2SeqSequenceClassifierOutput``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.Seq2SeqSequenceClassifierOutput
:members:

``MultipleChoiceModelOutput``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.MultipleChoiceModelOutput
:members:

``TokenClassifierOutput``
~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.TokenClassifierOutput
:members:

``QuestionAnsweringModelOutput``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.QuestionAnsweringModelOutput
:members:

``Seq2SeqQuestionAnsweringModelOutput``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_outputs.Seq2SeqQuestionAnsweringModelOutput
:members:
7 changes: 7 additions & 0 deletions docs/source/model_doc/albert.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ AlbertTokenizer
create_token_type_ids_from_sequences, save_vocabulary


Albert specific outputs
~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_albert.AlbertForPretrainingOutput
:members:


AlbertModel
~~~~~~~~~~~~~~~~~~~~

Expand Down
7 changes: 7 additions & 0 deletions docs/source/model_doc/bert.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ BertTokenizerFast
:members:


Bert specific outputs
~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_bert.BertForPretrainingOutput
:members:


BertModel
~~~~~~~~~~~~~~~~~~~~

Expand Down
13 changes: 13 additions & 0 deletions docs/source/model_doc/dpr.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ DPRReaderTokenizerFast
:members:


DPR specific outputs
~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_dpr.DPRContextEncoderOutput
:members:

.. autoclass:: transformers.modeling_dpr.DPRQuestionEncoderOutput
:members:

.. autoclass:: transformers.modeling_dpr.DPRReaderOutput
:members:


DPRContextEncoder
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
7 changes: 7 additions & 0 deletions docs/source/model_doc/electra.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ ElectraTokenizerFast
:members:


Electra specific outputs
~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_electra.ElectraForPretrainingOutput
:members:


ElectraModel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
7 changes: 7 additions & 0 deletions docs/source/model_doc/gpt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ OpenAIGPTTokenizerFast
:members:


OpenAI specific outputs
~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_openai.OpenAIGPTDoubleHeadsModelOutput
:members:


OpenAIGPTModel
~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
7 changes: 7 additions & 0 deletions docs/source/model_doc/gpt2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ GPT2TokenizerFast
:members:


GPT2 specific outputs
~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_gpt2.GPT2DoubleHeadsModelOutput
:members:


GPT2Model
~~~~~~~~~~~~~~~~~~~~~

Expand Down
7 changes: 7 additions & 0 deletions docs/source/model_doc/mobilebert.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ MobileBertTokenizerFast
:members:


MobileBert specific outputs
~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_mobilebert.MobileBertForPretrainingOutput
:members:


MobileBertModel
~~~~~~~~~~~~~~~~~~~~

Expand Down
10 changes: 10 additions & 0 deletions docs/source/model_doc/transformerxl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ TransfoXLTokenizerFast
:members:


TransfoXL specific outputs
~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_transfo_xl.TransfoXLModelOutput
:members:

.. autoclass:: transformers.modeling_transfo_xl.TransfoXLLMHeadModelOutput
:members:


TransfoXLModel
~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
8 changes: 8 additions & 0 deletions docs/source/model_doc/xlm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ XLMTokenizer
:members: build_inputs_with_special_tokens, get_special_tokens_mask,
create_token_type_ids_from_sequences, save_vocabulary


XLM specific outputs
~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_xlm.XLMForQuestionAnsweringOutput
:members:


XLMModel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
25 changes: 25 additions & 0 deletions docs/source/model_doc/xlnet.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@ XLNetTokenizer
create_token_type_ids_from_sequences, save_vocabulary


XLNet specific outputs
~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_xlnet.XLNetModelOutput
:members:

.. autoclass:: transformers.modeling_xlnet.XLNetLMHeadModelOutput
:members:

.. autoclass:: transformers.modeling_xlnet.XLNetForSequenceClassificationOutput
:members:

.. autoclass:: transformers.modeling_xlnet.XLNetForMultipleChoiceOutput
:members:

.. autoclass:: transformers.modeling_xlnet.XLNetForTokenClassificationOutput
:members:

.. autoclass:: transformers.modeling_xlnet.XLNetForQuestionAnsweringSimpleOutput
:members:

.. autoclass:: transformers.modeling_xlnet.XLNetForQuestionAnsweringOutput
:members:


XLNetModel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
19 changes: 15 additions & 4 deletions src/transformers/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def docstring_decorator(fn):

RETURN_INTRODUCTION = r"""
Returns:
:class:`~transformers.{output_type}` or :obj:`tuple(torch.FloatTensor)` (if ``return_tuple=True`` is passed or when ``config.return_tuple=True``) comprising various elements depending on the configuration (:class:`~transformers.{config_class}`) and inputs:
:class:`~{full_output_type}` or :obj:`tuple(torch.FloatTensor)` (if ``return_tuple=True`` is passed or when ``config.return_tuple=True``) comprising various elements depending on the configuration (:class:`~transformers.{config_class}`) and inputs:
"""


Expand All @@ -208,7 +208,8 @@ def _prepare_output_docstrings(output_type, config_class):
docstrings = "\n".join(lines[(i + 1) :])

# Add the return introduction
intro = RETURN_INTRODUCTION.format(output_type=output_type.__name__, config_class=config_class)
full_output_type = f"{output_type.__module__}.{output_type.__name__}"
intro = RETURN_INTRODUCTION.format(full_output_type=full_output_type, config_class=config_class)
return intro + docstrings


Expand Down Expand Up @@ -857,14 +858,24 @@ def wrapper(*args, **kwargs):

class ModelOutput:
"""
Base class for all model outputs as dataclass. Has a ``__getitem__`` (to make it behave like a ``namedtuple``) that
will ignore ``None`` in the attributes.
Base class for all model outputs as dataclass. Has a ``__getitem__`` that allows indexing by integer or slice (like
a tuple) or strings (like a dictionnary) that will ignore the ``None`` attributes.
"""

def to_tuple(self):
"""
Converts :obj:`self` to a tuple.
Return: A tuple containing all non-:obj:`None` attributes of the :obj:`self`.
"""
return tuple(getattr(self, f) for f in self.__dataclass_fields__.keys() if getattr(self, f, None) is not None)

def to_dict(self):
"""
Converts :obj:`self` to a Python dictionary.
Return: A dictionary containing all non-:obj:`None` attributes of the :obj:`self`.
"""
return {f: getattr(self, f) for f in self.__dataclass_fields__.keys() if getattr(self, f, None) is not None}

def __getitem__(self, i):
Expand Down
4 changes: 2 additions & 2 deletions src/transformers/modeling_albert.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,9 @@ class AlbertForPretrainingOutput(ModelOutput):
Output type of :class:`~transformers.AlbertForPretrainingModel`.
Args:
loss (`optional`, returned when ``labels`` is provided) ``torch.FloatTensor`` of shape ``(1,)``:
loss (`optional`, returned when ``labels`` is provided, ``torch.FloatTensor`` of shape :obj:`(1,)`):
Total loss as the sum of the masked language modeling loss and the next sequence prediction (classification) loss.
prediction_logits (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, config.vocab_size)`)
prediction_logits (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, config.vocab_size)`):
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
sop_logits (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, 2)`):
Prediction scores of the next sequence prediction (classification) head (scores of True/False
Expand Down
4 changes: 2 additions & 2 deletions src/transformers/modeling_bert.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,9 @@ class BertForPretrainingOutput(ModelOutput):
Output type of :class:`~transformers.BertForPretrainingModel`.
Args:
loss (`optional`, returned when ``labels`` is provided) ``torch.FloatTensor`` of shape ``(1,)``:
loss (`optional`, returned when ``labels`` is provided, ``torch.FloatTensor`` of shape :obj:`(1,)`):
Total loss as the sum of the masked language modeling loss and the next sequence prediction (classification) loss.
prediction_logits (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, config.vocab_size)`)
prediction_logits (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, config.vocab_size)`):
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
seq_relationship_logits (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, 2)`):
Prediction scores of the next sequence prediction (classification) head (scores of True/False
Expand Down
4 changes: 2 additions & 2 deletions src/transformers/modeling_electra.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ class ElectraForPretrainingOutput(ModelOutput):
Output type of :class:`~transformers.ElectraForPretrainingModel`.
Args:
loss (`optional`, returned when ``labels`` is provided) ``torch.FloatTensor`` of shape ``(1,)``:
loss (`optional`, returned when ``labels`` is provided, ``torch.FloatTensor`` of shape :obj:`(1,)`):
Total loss of the ELECTRA objective.
logits (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length)`)
logits (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length)`):
Prediction scores of the head (scores for each token before SoftMax).
hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``):
Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer)
Expand Down
Loading

0 comments on commit 7fad617

Please sign in to comment.