Getting logits from T5 Hugging Face model using forward() method without labels

Viewed 51

For my use case, I need to obtain the logits from T5's forward() method without inputting labels. I know that forward() and .generate() are different (see here). I have also seen this post in which the logits were obtained but labels had to be generated first. Is it possible to obtain the logits from the forward() method without inputting the labels?

1 Answers

Yes, you can do it. Huggingface library does not requires labels strictly as input to the forward method. See for example here for Bert: https://github.com/huggingface/transformers/blob/v4.21.1/src/transformers/models/bert/modeling_bert.py#L1079

Labels is an optional parameter. Also, later in the forward method, they check if labels are None or not. If they are None, they do not compute the loss and just return the output.

See here: https://github.com/huggingface/transformers/blob/v4.21.1/src/transformers/models/bert/modeling_bert.py#L1135

And the forward returns a dictionary that also contains logits.

https://github.com/huggingface/transformers/blob/v4.21.1/src/transformers/models/bert/modeling_bert.py#L1147

But please do look at the specific model's code you are using.

Related