opensi·si

All models have outputs that are instances of subclasses of utils.ModelOutput. Those are

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.

Tip With output_hidden_states=True, outputs.hidden_states[-1] isn't always the same tensor as outputs.last_hidden_state. Many vision and audio encoders, such as ViT, DINOv2, CLIP, and Wav2Vec2, return the last layer's output before the final layernorm in hidden_states[-1], and the normalized output in last_hidden_state. Use last_hidden_state for the model's final features. To make hidden_states[-1] match last_hidden_state, set tie_last_hidden_states=True in the model config.

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