
* Fixed typo: insted to instead * Fixed typo: relase to release * Fixed typo: nighlty to nightly * Fixed typos: versatible, benchamarks, becnhmark to versatile, benchmark, benchmarks * Fixed typo in comment: quantizd to quantized * Fixed typo: architecutre to architecture * Fixed typo: contibution to contribution * Fixed typo: Presequities to Prerequisites * Fixed typo: faste to faster * Fixed typo: extendeding to extending * Fixed typo: segmetantion_maps to segmentation_maps * Fixed typo: Alternativelly to Alternatively * Fixed incorrectly defined variable: output to output_disabled * Fixed typo in library name: tranformers.onnx to transformers.onnx * Fixed missing import: import tensorflow as tf * Fixed incorrectly defined variable: token_tensor to tokens_tensor * Fixed missing import: import torch * Fixed incorrectly defined variable and typo: uromaize to uromanize * Fixed incorrectly defined variable and typo: uromaize to uromanize * Fixed typo in function args: numpy.ndarry to numpy.ndarray * Fixed Inconsistent Library Name: Torchscript to TorchScript * Fixed Inconsistent Class Name: OneformerProcessor to OneFormerProcessor * Fixed Inconsistent Class Named Typo: TFLNetForMultipleChoice to TFXLNetForMultipleChoice * Fixed Inconsistent Library Name Typo: Pytorch to PyTorch * Fixed Inconsistent Function Name Typo: captureWarning to captureWarnings * Fixed Inconsistent Library Name Typo: Pytorch to PyTorch * Fixed Inconsistent Class Name Typo: TrainingArgument to TrainingArguments * Fixed Inconsistent Model Name Typo: Swin2R to Swin2SR * Fixed Inconsistent Model Name Typo: EART to BERT * Fixed Inconsistent Library Name Typo: TensorFLow to TensorFlow * Fixed Broken Link for Speech Emotion Classification with Wav2Vec2 * Fixed minor missing word Typo * Fixed minor missing word Typo * Fixed minor missing word Typo * Fixed minor missing word Typo * Fixed minor missing word Typo * Fixed minor missing word Typo * Fixed minor missing word Typo * Fixed minor missing word Typo * Fixed Punctuation: Two commas * Fixed Punctuation: No Space between XLM-R and is * Fixed Punctuation: No Space between [~accelerate.Accelerator.backward] and method * Added backticks to display model.fit() in codeblock * Added backticks to display openai-community/gpt2 in codeblock * Fixed Minor Typo: will to with * Fixed Minor Typo: is to are * Fixed Minor Typo: in to on * Fixed Minor Typo: inhibits to exhibits * Fixed Minor Typo: they need to it needs * Fixed Minor Typo: cast the load the checkpoints To load the checkpoints * Fixed Inconsistent Class Name Typo: TFCamembertForCasualLM to TFCamembertForCausalLM * Fixed typo in attribute name: outputs.last_hidden_states to outputs.last_hidden_state * Added missing verbosity level: fatal * Fixed Minor Typo: take To takes * Fixed Minor Typo: heuristic To heuristics * Fixed Minor Typo: setting To settings * Fixed Minor Typo: Content To Contents * Fixed Minor Typo: millions To million * Fixed Minor Typo: difference To differences * Fixed Minor Typo: while extract To which extracts * Fixed Minor Typo: Hereby To Here * Fixed Minor Typo: addition To additional * Fixed Minor Typo: supports To supported * Fixed Minor Typo: so that benchmark results TO as a consequence, benchmark * Fixed Minor Typo: a To an * Fixed Minor Typo: a To an * Fixed Minor Typo: Chain-of-though To Chain-of-thought
8.5 KiB
Model outputs
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_state
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.
ModelOutput
autodoc utils.ModelOutput - to_tuple
BaseModelOutput
autodoc modeling_outputs.BaseModelOutput
BaseModelOutputWithPooling
autodoc modeling_outputs.BaseModelOutputWithPooling
BaseModelOutputWithCrossAttentions
autodoc modeling_outputs.BaseModelOutputWithCrossAttentions
BaseModelOutputWithPoolingAndCrossAttentions
autodoc modeling_outputs.BaseModelOutputWithPoolingAndCrossAttentions
BaseModelOutputWithPast
autodoc modeling_outputs.BaseModelOutputWithPast
BaseModelOutputWithPastAndCrossAttentions
autodoc modeling_outputs.BaseModelOutputWithPastAndCrossAttentions
Seq2SeqModelOutput
autodoc modeling_outputs.Seq2SeqModelOutput
CausalLMOutput
autodoc modeling_outputs.CausalLMOutput
CausalLMOutputWithCrossAttentions
autodoc modeling_outputs.CausalLMOutputWithCrossAttentions
CausalLMOutputWithPast
autodoc modeling_outputs.CausalLMOutputWithPast
MaskedLMOutput
autodoc modeling_outputs.MaskedLMOutput
Seq2SeqLMOutput
autodoc modeling_outputs.Seq2SeqLMOutput
NextSentencePredictorOutput
autodoc modeling_outputs.NextSentencePredictorOutput
SequenceClassifierOutput
autodoc modeling_outputs.SequenceClassifierOutput
Seq2SeqSequenceClassifierOutput
autodoc modeling_outputs.Seq2SeqSequenceClassifierOutput
MultipleChoiceModelOutput
autodoc modeling_outputs.MultipleChoiceModelOutput
TokenClassifierOutput
autodoc modeling_outputs.TokenClassifierOutput
QuestionAnsweringModelOutput
autodoc modeling_outputs.QuestionAnsweringModelOutput
Seq2SeqQuestionAnsweringModelOutput
autodoc modeling_outputs.Seq2SeqQuestionAnsweringModelOutput
Seq2SeqSpectrogramOutput
autodoc modeling_outputs.Seq2SeqSpectrogramOutput
SemanticSegmenterOutput
autodoc modeling_outputs.SemanticSegmenterOutput
ImageClassifierOutput
autodoc modeling_outputs.ImageClassifierOutput
ImageClassifierOutputWithNoAttention
autodoc modeling_outputs.ImageClassifierOutputWithNoAttention
DepthEstimatorOutput
autodoc modeling_outputs.DepthEstimatorOutput
Wav2Vec2BaseModelOutput
autodoc modeling_outputs.Wav2Vec2BaseModelOutput
XVectorOutput
autodoc modeling_outputs.XVectorOutput
Seq2SeqTSModelOutput
autodoc modeling_outputs.Seq2SeqTSModelOutput
Seq2SeqTSPredictionOutput
autodoc modeling_outputs.Seq2SeqTSPredictionOutput
SampleTSPredictionOutput
autodoc modeling_outputs.SampleTSPredictionOutput
TFBaseModelOutput
autodoc modeling_tf_outputs.TFBaseModelOutput
TFBaseModelOutputWithPooling
autodoc modeling_tf_outputs.TFBaseModelOutputWithPooling
TFBaseModelOutputWithPoolingAndCrossAttentions
autodoc modeling_tf_outputs.TFBaseModelOutputWithPoolingAndCrossAttentions
TFBaseModelOutputWithPast
autodoc modeling_tf_outputs.TFBaseModelOutputWithPast
TFBaseModelOutputWithPastAndCrossAttentions
autodoc modeling_tf_outputs.TFBaseModelOutputWithPastAndCrossAttentions
TFSeq2SeqModelOutput
autodoc modeling_tf_outputs.TFSeq2SeqModelOutput
TFCausalLMOutput
autodoc modeling_tf_outputs.TFCausalLMOutput
TFCausalLMOutputWithCrossAttentions
autodoc modeling_tf_outputs.TFCausalLMOutputWithCrossAttentions
TFCausalLMOutputWithPast
autodoc modeling_tf_outputs.TFCausalLMOutputWithPast
TFMaskedLMOutput
autodoc modeling_tf_outputs.TFMaskedLMOutput
TFSeq2SeqLMOutput
autodoc modeling_tf_outputs.TFSeq2SeqLMOutput
TFNextSentencePredictorOutput
autodoc modeling_tf_outputs.TFNextSentencePredictorOutput
TFSequenceClassifierOutput
autodoc modeling_tf_outputs.TFSequenceClassifierOutput
TFSeq2SeqSequenceClassifierOutput
autodoc modeling_tf_outputs.TFSeq2SeqSequenceClassifierOutput
TFMultipleChoiceModelOutput
autodoc modeling_tf_outputs.TFMultipleChoiceModelOutput
TFTokenClassifierOutput
autodoc modeling_tf_outputs.TFTokenClassifierOutput
TFQuestionAnsweringModelOutput
autodoc modeling_tf_outputs.TFQuestionAnsweringModelOutput
TFSeq2SeqQuestionAnsweringModelOutput
autodoc modeling_tf_outputs.TFSeq2SeqQuestionAnsweringModelOutput
FlaxBaseModelOutput
autodoc modeling_flax_outputs.FlaxBaseModelOutput
FlaxBaseModelOutputWithPast
autodoc modeling_flax_outputs.FlaxBaseModelOutputWithPast
FlaxBaseModelOutputWithPooling
autodoc modeling_flax_outputs.FlaxBaseModelOutputWithPooling
FlaxBaseModelOutputWithPastAndCrossAttentions
autodoc modeling_flax_outputs.FlaxBaseModelOutputWithPastAndCrossAttentions
FlaxSeq2SeqModelOutput
autodoc modeling_flax_outputs.FlaxSeq2SeqModelOutput
FlaxCausalLMOutputWithCrossAttentions
autodoc modeling_flax_outputs.FlaxCausalLMOutputWithCrossAttentions
FlaxMaskedLMOutput
autodoc modeling_flax_outputs.FlaxMaskedLMOutput
FlaxSeq2SeqLMOutput
autodoc modeling_flax_outputs.FlaxSeq2SeqLMOutput
FlaxNextSentencePredictorOutput
autodoc modeling_flax_outputs.FlaxNextSentencePredictorOutput
FlaxSequenceClassifierOutput
autodoc modeling_flax_outputs.FlaxSequenceClassifierOutput
FlaxSeq2SeqSequenceClassifierOutput
autodoc modeling_flax_outputs.FlaxSeq2SeqSequenceClassifierOutput
FlaxMultipleChoiceModelOutput
autodoc modeling_flax_outputs.FlaxMultipleChoiceModelOutput
FlaxTokenClassifierOutput
autodoc modeling_flax_outputs.FlaxTokenClassifierOutput
FlaxQuestionAnsweringModelOutput
autodoc modeling_flax_outputs.FlaxQuestionAnsweringModelOutput
FlaxSeq2SeqQuestionAnsweringModelOutput
autodoc modeling_flax_outputs.FlaxSeq2SeqQuestionAnsweringModelOutput