← Back to list

How AI Chooses Words: Probability, Softmax, and Temperature

When you ask an AI model like OpenAI ChatGPT or Google Gemini a question, it feels like the model is “thinking.”

Rohit Gupta · 2026-05-25 04:16 · 0 claps · 5.6 min read
#text-generation #large-language-models #deep-learning #temperature #neural-networks
Open on Medium ↗
Wiki topics: LLM · Large Language Models ML · Machine Learning EDU · Education & Learning 📐 · Mathematics

How AI Chooses Words: Probability, Softmax, and Temperature

When you ask an AI model like OpenAI ChatGPT or Google Gemini a question, it feels like the model is “thinking.”

It sounds intelligent. Sometimes even creative.

But under the hood, something much simpler is happening.

The model is doing math.

More specifically:

  • calculating probabilities
  • ranking possible next words
  • sampling from those probabilities

That’s it.

Large Language Models (LLMs) do not “understand” language the way humans do. They predict the next token based on patterns learned during training.

And that raises a fascinating question:

Given thousands of possible next words, how does the model decide which one to pick?

The answer involves:

  • probability distributions
  • logits
  • the softmax function
  • temperature scaling
  • sampling strategies

These concepts power nearly every modern LLM today:

  • ChatGPT
  • Claude
  • Gemini
  • Llama Models by Meta AI

Let’s break it down step by step.

The Probability Foundation Behind Language Models

At its core, an LLM is a probability engine.

It receives:

  • previous words
  • sentence context
  • token history

Then it predicts:

  • probabilities for the next possible token

What Is a Probability Distribution?

A probability distribution tells us:

how likely each possible outcome is.

Simple example:

Input:

The sky is ___

Possible predictions:

[embed]

The probabilities must follow two rules:

Rule 1: Probabilities Cannot Be Negative

Rule 2: Total Probability Must Sum to 1

This creates a valid probability distribution.

Think of it like distributing 100% confidence across all possible next words.

Why This Matters in LLMs

Modern LLMs may predict from:

  • 30,000 tokens
  • 50,000 tokens
  • even 100,000+ vocabulary entries

The model assigns a score to every possible token.

Then it converts those scores into probabilities.

That conversion step is where softmax enters the story.

The Problem With Raw Model Outputs

Neural networks do not directly output probabilities.

They output raw numerical scores called:

logits

Example logits:

[embed]

These numbers have problems:

  • they can be negative
  • they don’t sum to 1
  • they are not interpretable probabilities

You cannot directly sample from them.

We need a mathematical function that:

  1. converts scores into positive values
  2. normalizes them into probabilities
  3. preserves ranking information

That function is softmax.

Softmax Function Explained Step by Step

The softmax equation looks intimidating at first.

But it’s actually simple once you break it down.

The formula:

Let’s decode this carefully.

Step 1: Understanding the Exponential Function

The exponential function:

does two important things.

It Makes Everything Positive

Even negative logits become positive.

Example:

[embed]

This solves the negative probability problem.

It Amplifies Differences

Exponentials grow fast.

Example:

[embed]

A small difference in logits becomes a huge probability gap.

This creates the “winner-take-most” behavior in language models.

Step 2: Normalize the Values

After exponentiation, we divide by the total sum.

That ensures:

Now we have valid probabilities.

A Complete Numerical Example

Suppose the model predicts these logits:

[embed]

Step 1: Apply Exponential

Compute:

[embed]

Step 2: Compute Total Sum

Total:

7.39 + 2.72 + 1.10 = 11.21

Step 3: Normalize

Now divide each value by the total.

For “cat”:

For “dog”:

For “pizza”:

Final probabilities:

[embed]

Now the model can sample from these probabilities.

Why Softmax Is the Right Function

Softmax works beautifully because it satisfies all requirements for probabilistic prediction.

1. Outputs Are Always Positive

Because exponentials are always positive:

No negative probabilities.

2. Probabilities Sum to 1

Normalization guarantees:

This creates a valid probability distribution.

3. Larger Scores Become Much More Likely

Exponentials magnify differences aggressively.

This helps the model strongly favor better predictions.

Without exponentials, probabilities would often become too flat.

Temperature: The Creativity Control Knob

Temperature modifies softmax behavior.

The updated formula becomes:

Where:

  • T = temperature

This changes the sharpness of the probability distribution.

What Happens at Different Temperatures?

High Temperature (T > 1)

Higher temperature makes probabilities flatter.

The model becomes:

  • more random
  • more creative
  • less predictable

Example:

  • storytelling
  • brainstorming
  • poetry generation

Low Temperature (T < 1)

Lower temperature sharpens probabilities.

The model becomes:

  • more confident
  • more deterministic
  • less creative

Useful for:

  • coding
  • factual QA
  • math problems

Extreme Case: Temperature Approaches Zero

As:

T→0

softmax approaches:

Meaning:

  • always choose the highest-probability token

No randomness at all.

Temperature in Real LLM Outputs

Let’s see this in practice.

Prompt:

Write a futuristic city description.

Temperature = 0.2

Output:

The city contained advanced transportation systems and automated infrastructure.

Predictable. Safe. Structured.

Temperature = 1.0

Output:

Neon trains floated between towering glass structures while AI-controlled drones filled the skies.

More creative.

Temperature = 1.8

Output:

Electric oceans shimmered beneath upside-down skyscrapers as synthetic birds translated human dreams into music.

Much more imaginative. Sometimes too chaotic.

How Companies Use Temperature Today

Modern LLM systems tune temperature depending on the task.

[embed]

This is why coding assistants often feel more deterministic than storytelling models.

Advanced Decoding Techniques Used in Modern LLMs

Softmax alone is not enough.

Modern models combine it with smarter sampling methods.

Top-k Sampling

Instead of considering all tokens:

  • keep only the top k candidates

Example:

  • top 50 tokens only

This removes extremely unlikely outputs.

Top-p (Nucleus Sampling)

Instead of fixed token count:

  • keep tokens whose cumulative probability exceeds p

Example:

  • keep tokens until total probability reaches 95%

This adapts dynamically to context.

Top-p is widely used in production LLMs today.

Greedy Decoding

Always choose:

  • highest probability token

Fast but repetitive.

Beam Search

Keeps multiple candidate sequences simultaneously.

Useful for:

  • translation
  • structured generation

Less common in conversational LLMs.

Sampling

Actually sample from probabilities.

This introduces:

  • diversity
  • creativity
  • variation

Most chat models use sampling-based decoding.

Why This Matters for Modern AI

Every response from an LLM ultimately comes from probability calculations.

The model does not “know” facts the way humans do.

Instead:

  • neural networks compute logits
  • softmax converts them into probabilities
  • sampling selects tokens
  • the process repeats token by token

That’s how paragraphs emerge.

One probability distribution at a time.

The Bigger Picture

This explains both:

  • the power of LLMs
  • and their limitations

Because these systems predict statistically likely continuations, they can:

  • sound fluent
  • mimic reasoning
  • generate coherent text

But they can also:

  • hallucinate
  • become inconsistent
  • produce confident mistakes

The model optimizes for:

likely next tokens

not truth itself.

That distinction matters enormously.

Final Thoughts

AI “choosing” words sounds mysterious.

But underneath the magic sits elegant mathematics.

Large Language Models transform:

  • raw neural network scores → probability distributions → sampled tokens → coherent language

Softmax acts as the bridge between:

  • arbitrary neural outputs, and
  • meaningful probabilistic decisions

Temperature shapes creativity.

Sampling strategies shape diversity.

And together, these mechanisms power nearly every modern LLM you use today.

The next time ChatGPT or Gemini or other LLMs writes a sentence, remember:

It didn’t “think” of the next word.

It calculated the probability of it.


메타데이터
post_id
c44e80b4c62d
slug
how-ai-chooses-words-probability-softmax-and-temperature-c44e80b4c62d
url
https://medium.com/@rohit.gupta1604004/how-ai-chooses-words-probability-softmax-and-temperature-c44e80b4c62d
canonical_url
https://medium.com/@rohit.gupta1604004/how-ai-chooses-words-probability-softmax-and-temperature-c44e80b4c62d
author_url
https://medium.com/@rohit.gupta1604004
status
ok
fetched_at
2026-06-09 15:37:30