# LLM Next\-Token Prediction

A bilingual \(English/Chinese\) machine that shows how a large language model tokenizes text, embeds it, runs it through Transformer layers and samples the next token with temperature and top\-p

By DUSK

Topic: Computing, AI &amp; Electronics

[Open the interactive Jig](https://ask.jig.com/j/91lRkuxA8ss6)

## Transcript

### 1. Guess the next word · 猜下一个词

A large language model writes by playing one game over and over: guess the next piece of text\. This machine shows that game for the words "The cat sat on the"\. Follow the glowing signal from the words at the front, up through the tower and out to the chart on the right\.

大语言模型写作的方法：一次又一次地猜下一个词。Drag empty space to turn the machine\.

### 2. Tokens · 分词

First a tokenizer cuts the text into tokens: whole words, pieces of words or punctuation\. A space usually rides along at the front of a token\. Each token is then swapped for its number in the vocabulary, so the model only ever sees numbers\.

分词器把文字切成“词元”，每个词元都有一个编号。GPT\-2 has a vocabulary of 50,257 tokens\.

### 3. Embeddings · 嵌入向量

Each token number picks out a long list of numbers called an embedding\. Tokens with similar meanings get similar lists\. A second list that marks each token's position is added, so the model knows the word order\.

每个词元变成一串数字（嵌入向量），再加上位置信息。In GPT\-2 small each list holds 768 numbers; the columns show a few\.

### 4. Transformer layers · 变换器层

The lists climb through a stack of Transformer layers\. In each layer, attention lets every token look back at the tokens before it, never ahead, and mix in what matters\. Then a small network reshapes each list\. Watch the purple arcs reach back from the newest word\.

注意力只能往前看，不能偷看后面的词。GPT\-2 small has 12 layers; today's biggest models have many more\.

### 5. Probabilities · 概率

Only the last token's list is used to choose what comes next\. The output layer turns it into a score for every token in the vocabulary, and softmax turns the scores into probabilities that add up to 100%\. The chart shows the eight most likely\.

输出层给每个词元打分，再换算成加起来等于100%的概率。These percentages are illustrative, not from a real model\.

### 6. Temperature · 温度

Temperature reshapes the chart before a token is drawn\. Below 1 the tallest bar grows and the choice becomes safe and repeatable\. Above 1 the bars even out and surprising words get a real chance\. Slide it and watch the bars change\.

温度低：更稳定；温度高：更有变化。The scores are divided by the temperature before softmax\.

### 7. Top\-P · 核采样

Top\-P keeps only the most likely tokens whose probabilities add up to P, and drops the long tail of unlikely ones\. The kept bars are shared out again to make 100%\. A low P leaves just one or two choices\.

核采样只保留累计概率达到P的词元。Grey bars have been dropped and can no longer be picked\.

### 8. Pick, add, repeat · 自回归生成

Now the model draws one token at random, weighted by the bars, and adds it to the end of the text\. Then the whole machine runs again with one more token\. Press Generate a few times, and change the sliders to see how the text changes\.

选出一个词元，接到句子后面，再预测下一个：这叫自回归。After two new tokens the sentence starts over\.

### 9. One token at a time · 一次一个词元

Every answer a chatbot writes is built this way, one token at a time, each guess feeding the next\. The model does not look up stored sentences\. It predicts what is likely, which is why the same question can get different answers\.

聊天机器人的每个回答，都是这样一个词元一个词元生成的。
