# Print All Tokens Over a Certain Probability Threshold

**URL:** <https://discuss.huggingface.co/t/print-all-tokens-over-a-certain-probability-threshold/329>\
**Category:** Research\
**Created:** [July 16, 2020, 8:12pm UTC](https://discuss.huggingface.co/t/print-all-tokens-over-a-certain-probability-threshold/329 "2020-07-16T20:12:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanderbush](https://avatars.discourse-cdn.com/v4/letter/z/13edae/32.png) [@zanderbush](https://discuss.huggingface.co/u/zanderbush)\
**Post date:** [July 16, 2020, 8:12pm UTC](https://discuss.huggingface.co/t/print-all-tokens-over-a-certain-probability-threshold/329/1 "2020-07-16T20:12:49Z")

</div>

I am curious to know how I would do this using GPT-2. Thank you for your time!

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](https://discuss.huggingface.co/u/sgugger)\
**Post date:** [July 20, 2020, 2:19pm UTC](https://discuss.huggingface.co/t/print-all-tokens-over-a-certain-probability-threshold/329/2 "2020-07-20T14:19:41Z")

</div>

Hi there, here is a quick way to do this for the last token on a given sentence in PyTorch:

```auto
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import torch
import torch.nn.functional as F

# Load model and tokenizer
model = GPT2LMHeadModel.from_pretrained('gpt2')
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')

# Input example
input_txt = "Hello, my name is Sylvain."
inputs = tokenizer(input_txt, return_tensors='pt')
outputs = model(**inputs)

# If you are not on a source install, replace outputs.logits by outputs[0]
predictions = F.softmax(outputs.logits, dim=-1)

thresh = 1e-2
vocab_size = predictions.shape[-1]

# Predictions has one sentence (index 0) and we look at the last token predicted (-1)
idxs = torch.arange(0, vocab_size)[predictions[0][-1] >= thresh]
print(tokenizer.convert_ids_to_tokens(idxs))

```

---

<div class="post-metadata">

**Author:** ![zanderbush](https://avatars.discourse-cdn.com/v4/letter/z/13edae/32.png) [@zanderbush](https://discuss.huggingface.co/u/zanderbush)\
**Post date:** [July 21, 2020, 12:57am UTC](https://discuss.huggingface.co/t/print-all-tokens-over-a-certain-probability-threshold/329/4 "2020-07-21T00:57:53Z")

</div>

I can’t thank you enough for your detailed response! I apologize if I am asking too much of this forum, but given I have this question I am sure others would benefit from an answer as well.

While on this topic, I wonder what steps would need to be taken to expand this function for the output to include phrases in addition to words.

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](https://discuss.huggingface.co/u/sgugger)\
**Post date:** [July 21, 2020, 4:44pm UTC](https://discuss.huggingface.co/t/print-all-tokens-over-a-certain-probability-threshold/329/5 "2020-07-21T16:44:52Z")

</div>

I don’t think the generate method can return the probabilities, so you might have to tweak the generate function to return them.
