liveBook · Manning

Greedy algorithm

As explained in Natural Language Processing in Action, Second Edition

Greedy search generates a sequence by selecting the highest-probability next token at each step without planning over alternative continuations.

A greedy algorithm chooses the best next action without planning beyond one step. In text generation, it selects the highest-probability next token, appends it to the sequence, and repeats. This can trap generation in repetition loops. section 10.2.1

A GPT-2 continuation

Given “NLP is a,” GPT-2 ranks “ new” above “ non.” Repeated choices can produce circular text, such as repeating claims about data structures. Temperature and repetition penalties can help alter this behavior. section 10.2.1

Listing 10.6 Finding the token with maximum probability
>>> encoded_prompt = tokenizer('NLP is a', return_tensors="pt")    #1
>>> encoded_prompt = encoded_prompt["input_ids"]
>>> encoded_prompt = encoded_prompt.to(DEVICE)
>>> output = vanilla_gpt2(input_ids=encoded_prompt)
>>> next_token_logits = output.logits[0, -1, :]
>>> next_token_probs = torch.softmax(next_token_logits, dim=-1)
>>> sorted_ids = torch.argsort(next_token_probs, dim=-1, descending=True)
>>> tokenizer.decode(sorted_ids[0])       #2
' new'
>>> tokenizer.decode(sorted_ids[1])    #3
' non'

Inspect how GPT-2 converts next-token logits into probabilities and ranks candidate tokens. section 10.2.1

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines one-step token selection and shows probability ranking, then notes repetition loops without deriving the algorithm formally.
Examples
Ranks “ new” and “ non” after “NLP is a” and shows repeated data-structure text.
What you can do after
Apply Hugging Face GPT-2 generation and adjust temperature or repetition penalties to change token selection behavior.
How it connects
Connects token selection to temperature, repetition penalties, and byte-pair encoding.

Also explained in

Compare how 12 books explain greedy algorithm →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.