All models have outputs that are instances of subclasses of [~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 how this looks in an example:
from transformers import BertTokenizer, BertForSequenceClassification
import torch
tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased")
model = BertForSequenceClassification.from_pretrained("google-bert/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 [~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
.
When passing output_hidden_states=True
you may expect the outputs.hidden_states[-1]
to match outputs.last_hidden_states
exactly.
However, this is not always the case. Some models apply normalization or subsequent process to the last hidden state when it's returned.
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
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.
[[autodoc]] utils.ModelOutput - to_tuple
[[autodoc]] modeling_outputs.BaseModelOutput
[[autodoc]] modeling_outputs.BaseModelOutputWithPooling
[[autodoc]] modeling_outputs.BaseModelOutputWithCrossAttentions
[[autodoc]] modeling_outputs.BaseModelOutputWithPoolingAndCrossAttentions
[[autodoc]] modeling_outputs.BaseModelOutputWithPast
[[autodoc]] modeling_outputs.BaseModelOutputWithPastAndCrossAttentions
[[autodoc]] modeling_outputs.Seq2SeqModelOutput
[[autodoc]] modeling_outputs.CausalLMOutput
[[autodoc]] modeling_outputs.CausalLMOutputWithCrossAttentions
[[autodoc]] modeling_outputs.CausalLMOutputWithPast
[[autodoc]] modeling_outputs.MaskedLMOutput
[[autodoc]] modeling_outputs.Seq2SeqLMOutput
[[autodoc]] modeling_outputs.NextSentencePredictorOutput
[[autodoc]] modeling_outputs.SequenceClassifierOutput
[[autodoc]] modeling_outputs.Seq2SeqSequenceClassifierOutput
[[autodoc]] modeling_outputs.MultipleChoiceModelOutput
[[autodoc]] modeling_outputs.TokenClassifierOutput
[[autodoc]] modeling_outputs.QuestionAnsweringModelOutput
[[autodoc]] modeling_outputs.Seq2SeqQuestionAnsweringModelOutput
[[autodoc]] modeling_outputs.Seq2SeqSpectrogramOutput
[[autodoc]] modeling_outputs.SemanticSegmenterOutput
[[autodoc]] modeling_outputs.ImageClassifierOutput
[[autodoc]] modeling_outputs.ImageClassifierOutputWithNoAttention
[[autodoc]] modeling_outputs.DepthEstimatorOutput
[[autodoc]] modeling_outputs.Wav2Vec2BaseModelOutput
[[autodoc]] modeling_outputs.XVectorOutput
[[autodoc]] modeling_outputs.Seq2SeqTSModelOutput
[[autodoc]] modeling_outputs.Seq2SeqTSPredictionOutput
[[autodoc]] modeling_outputs.SampleTSPredictionOutput
[[autodoc]] modeling_tf_outputs.TFBaseModelOutput
[[autodoc]] modeling_tf_outputs.TFBaseModelOutputWithPooling
[[autodoc]] modeling_tf_outputs.TFBaseModelOutputWithPoolingAndCrossAttentions
[[autodoc]] modeling_tf_outputs.TFBaseModelOutputWithPast
[[autodoc]] modeling_tf_outputs.TFBaseModelOutputWithPastAndCrossAttentions
[[autodoc]] modeling_tf_outputs.TFSeq2SeqModelOutput
[[autodoc]] modeling_tf_outputs.TFCausalLMOutput
[[autodoc]] modeling_tf_outputs.TFCausalLMOutputWithCrossAttentions
[[autodoc]] modeling_tf_outputs.TFCausalLMOutputWithPast
[[autodoc]] modeling_tf_outputs.TFMaskedLMOutput
[[autodoc]] modeling_tf_outputs.TFSeq2SeqLMOutput
[[autodoc]] modeling_tf_outputs.TFNextSentencePredictorOutput
[[autodoc]] modeling_tf_outputs.TFSequenceClassifierOutput
[[autodoc]] modeling_tf_outputs.TFSeq2SeqSequenceClassifierOutput
[[autodoc]] modeling_tf_outputs.TFMultipleChoiceModelOutput
[[autodoc]] modeling_tf_outputs.TFTokenClassifierOutput
[[autodoc]] modeling_tf_outputs.TFQuestionAnsweringModelOutput
[[autodoc]] modeling_tf_outputs.TFSeq2SeqQuestionAnsweringModelOutput
[[autodoc]] modeling_flax_outputs.FlaxBaseModelOutput
[[autodoc]] modeling_flax_outputs.FlaxBaseModelOutputWithPast
[[autodoc]] modeling_flax_outputs.FlaxBaseModelOutputWithPooling
[[autodoc]] modeling_flax_outputs.FlaxBaseModelOutputWithPastAndCrossAttentions
[[autodoc]] modeling_flax_outputs.FlaxSeq2SeqModelOutput
[[autodoc]] modeling_flax_outputs.FlaxCausalLMOutputWithCrossAttentions
[[autodoc]] modeling_flax_outputs.FlaxMaskedLMOutput
[[autodoc]] modeling_flax_outputs.FlaxSeq2SeqLMOutput
[[autodoc]] modeling_flax_outputs.FlaxNextSentencePredictorOutput
[[autodoc]] modeling_flax_outputs.FlaxSequenceClassifierOutput
[[autodoc]] modeling_flax_outputs.FlaxSeq2SeqSequenceClassifierOutput
[[autodoc]] modeling_flax_outputs.FlaxMultipleChoiceModelOutput
[[autodoc]] modeling_flax_outputs.FlaxTokenClassifierOutput
[[autodoc]] modeling_flax_outputs.FlaxQuestionAnsweringModelOutput
[[autodoc]] modeling_flax_outputs.FlaxSeq2SeqQuestionAnsweringModelOutput