Model outputs¶

PyTorch models have outputs that are instances of subclasses of 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:

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 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

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¶

class transformers.file_utils.ModelOutput[source]¶

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 dictionary) that will ignore the None attributes. Otherwise behaves like a regular python dictionary.

Warning

You can’t unpack a ModelOutput directly. Use the to_tuple() method to convert it to a tuple before.

pop(k[, d]) → v, remove specified key and return the corresponding[source]¶

value. If key is not found, d is returned if given, otherwise KeyError is raised.

setdefault(k[, d]) → od.get(k,d), also set od[k]=d if k not in od[source]¶
to_tuple() → Tuple[Any][source]¶

Convert self to a tuple containing all the attributes/keys that are not None.

update([E, ]**F) → None. Update D from dict/iterable E and F.[source]¶

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

BaseModelOutput¶

BaseModelOutputWithPooling¶

BaseModelOutputWithCrossAttentions¶

BaseModelOutputWithPoolingAndCrossAttentions¶

BaseModelOutputWithPast¶

BaseModelOutputWithPastAndCrossAttentions¶

Seq2SeqModelOutput¶

CausalLMOutput¶

CausalLMOutputWithCrossAttentions¶

CausalLMOutputWithPast¶

MaskedLMOutput¶

Seq2SeqLMOutput¶

NextSentencePredictorOutput¶

SequenceClassifierOutput¶

Seq2SeqSequenceClassifierOutput¶

MultipleChoiceModelOutput¶

TokenClassifierOutput¶

QuestionAnsweringModelOutput¶

Seq2SeqQuestionAnsweringModelOutput¶

TFBaseModelOutput¶

TFBaseModelOutputWithPooling¶

TFBaseModelOutputWithPast¶

TFSeq2SeqModelOutput¶

TFCausalLMOutput¶

TFCausalLMOutputWithPast¶

TFMaskedLMOutput¶

TFSeq2SeqLMOutput¶

TFNextSentencePredictorOutput¶

TFSequenceClassifierOutput¶

TFSeq2SeqSequenceClassifierOutput¶

TFMultipleChoiceModelOutput¶

TFTokenClassifierOutput¶

TFQuestionAnsweringModelOutput¶

TFSeq2SeqQuestionAnsweringModelOutput¶