MODEL EXPLORER / LANGUAGE MILESTONES
Learn from both sides of a masked token
What changes when the encoder can see both left and right context?
RECORDED NUMERICAL EXAMPLE
Use both sides of context, predict selected targets.
Recorded MLM and NSP objectives on synthetic token IDs. The encoder can attend across both segments. Target selection defines the loss; unselected token logits are ignored by MLM. No language understanding is claimed.
Toy token IDs: CLS=1, SEP=2 and MASK=3. Segments are A,A,A,A,B,B. Only selected positions contribute to MLM loss. This controlled example always replaces selected words with MASK; original BERT selected 15% of tokens with an 80/10/10 mask/random/unchanged rule. The NSP labels here are illustrative, without a real sentence corpus.
Min 0.1108 · Max 0.246
Recorded cell values
| Row / col | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| 0 | 0.1762 | 0.1773 | 0.1755 | 0.1628 | 0.1533 | 0.1548 |
| 1 | 0.1878 | 0.1853 | 0.176 | 0.1721 | 0.1396 | 0.1393 |
| 2 | 0.1864 | 0.1689 | 0.1717 | 0.1783 | 0.1455 | 0.1491 |
| 3 | 0.1508 | 0.1534 | 0.1452 | 0.246 | 0.1633 | 0.1413 |
| 4 | 0.1388 | 0.1878 | 0.1529 | 0.2176 | 0.1686 | 0.1342 |
| 5 | 0.1861 | 0.2298 | 0.1958 | 0.1589 | 0.1186 | 0.1108 |
Each heatmap uses its own scale. Mint is positive, rust is negative, and the lightest color is zero. Table values are rounded to four significant digits.
Bars share one scale within this example. Values are rounded to four significant digits.
- Masked-token cross entropy
- 2.802
- Sentence-pair cross entropy
- 0.7525
- Combined pretraining objective
- 3.554
Verified by the local recipe
- A later token can change earlier token logits through bidirectional attention
- MLM cross entropy ignores all unselected token positions
- The MLM decoder shares the token-embedding weight and receives a learning signal
- Packed MLM/NSP logits have the declared shapes and sentence-pair probabilities sum to one
Download the source and run.py to reproduce these checks. Choosing an example here replays recorded values.
THE SAME MODEL, ON YOUR MACHINE
Pick up where
the graph leaves off.
Explore a bidirectional BERT-style encoder, then inspect which token targets and sentence-pair labels contribute to its pretraining objectives.
Use in VS Code ↗1. Save these files in one folder
2. Reproduce the example
Use Python 3.13 in a dedicated environment, matching the tested recipe. These commands are for macOS / Linux shells.
python3.13 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python run.pyExpected output: [1,98]
Input 1: [1,6] · int64 ones
Input 2: [1,6] · int64 ones · CPU / eval · seed 0
3. Open the source in TensorViz
Install the preview, open this folder in local VS Code, and choose Open as Graph above TinyBertPretraining in model.py. Select your Python environment. Save before choosing Validate.
Installation and supported setup ↗
The recipe was run with Python 3.13.13 and Torch 2.7.1. This is an untrained example; a successful shape check does not measure accuracy.
ABOUT THIS EXAMPLE
Explore a bidirectional BERT-style encoder, then inspect which token targets and sentence-pair labels contribute to its pretraining objectives.
Architecture · 2018
BERT pretraining
One post-LN encoder, width eight, two heads, fixed length six and vocabulary sixteen. Token/position/segment embeddings, tied MLM decoder, GELU/LayerNorm MLM transform and a tanh-pooled sentence-pair head.
Source, capture & limitations +
Untrained fixed-length model without padding or a tokenizer; dropout is omitted. The numerical cases always mask chosen tokens to isolate the MLM objective, rather than reproduce the complete 15% and 80/10/10 corruption sampler. NSP uses synthetic labels. The recipe packs two output heads into one 98-value tensor for verification.
Original TensorViz teaching example. PyTorch provides the underlying operators.
No separate redistribution license has been declared for these project examples.
- Content revision
0c48027dcf5a807d- Source SHA-256
fa041d05ccb2a15cb1e139d53180ab5ca4930dd11ba12f3fd3aec170228f0fc5- Captured
- 2026-09-17 · Python 3.13.13 / Torch 2.7.1
One reduced BERT pretraining forward with MLM/NSP heads, bidirectional influence, ignored-target loss behavior and tied-weight gradient checks. Output packs 96 MLM logits and two NSP logits.
Read the model manifest ↗