cat /en/pinocchio-turns-into-numbers/
MDN-121:40
SERIES Inside the Puppet · Chapter 2

Pinocchio turns into numbers

· Artificial Intelligence · 6 min read

A neural network does not read, look or listen: it does arithmetic. Everything we give it, a text, an image, the price of a house, must first become a row of numbers. Chapter 2 of the book lines up the tools for doing this: vectors, dot product, matrices, tensors. They are few, and they are enough for the whole rest of the book.

The most important one is the dot product. You multiply two vectors component by component and add up: a · b = a₁b₁ + a₂b₂ + … It is the elementary gesture of every neuron. With the weights w of a model and the features x of a house, w · x + b is already an estimate of the price. With any two vectors, the sign of the result tells you whether they point the same way.

Then there is the step our GPT needs: turning Pinocchio into numbers. You make the list of the distinct characters, the vocabulary, and give each one its number. For a network, however, the number 48 is not “bigger” than 42: they are just names. This is why every character also becomes a one-hot vector, as long as the vocabulary, all zeros except for a 1 in its column.

DEMO Pinocchio turns into numbers · about this demo · full screen ↗

On the left, drag the tips of two arrows and read the dot product and the angle between them; below, three sliders change the house and the calculation redoes itself. On the right, type a text and watch it become a list of numbers and a one-hot grid, with the vocabulary of a single sentence or that of the whole of Pinocchio.

Three experiments

1. Il segno del prodotto (The sign of the product). Press “1” three times: they are the three pairs of figure 2.4 in the book. Almost the same direction: 6.4. At right angles: zero. Opposite directions: −5.28. Then drag b around a yourself. The product is largest when the arrows are aligned and becomes negative as soon as the angle exceeds 90°. It is the way in which, in chapter 12, attention will decide how much two characters “resemble each other”.

2. legno in numeri (wood in numbers). Press “2”. With the vocabulary of the sentence “C’era una volta un pezzo di legno.” (Once upon a time there was a piece of wood.), 18 characters, “legno” becomes [9, 6, 7, 10, 11]. Switch to the vocabulary of the whole of Pinocchio, 69 characters, and it becomes [48, 42, 44, 50, 51]. The numbers change because the list changes: a model trained with one vocabulary does not understand the numbers of another. Try typing a character that does not appear in the book, such as “k” or “w”: it is left out.

3. Il trucco della riga (The row trick). Press “3” and click the rows of the grid. A one-hot vector times a matrix W does no real calculation: it pulls out a row of W, the one for that character. In the book, with the rows of a, e, l, o, “l” in one-hot times W gives [0.5 0.6], that is W[2]. It looks like a detail, but it is the way every language model turns its symbols into vectors, and PyTorch does it by picking the row without computing the product.

The house is left to try with the sliders: with 50 m², 2 bathrooms and 10 km from the centre the estimate is 75 + 16 − 5 + 25 = 111. Each extra square metre is worth 1.5, each extra kilometre costs 0.5.

The same experiment in Python

The code downloads the text of Pinocchio from the book’s repository and redoes the lab’s calculations with NumPy and PyTorch.

!wget -q https://raw.githubusercontent.com/mid079/deep-learning-teoria-pratica/main/dati/pinocchio.txt

import numpy as np
import torch
import torch.nn.functional as F

# 1. The sign of the dot product
for a, b in [([2, 1], [2.4, 1.6]), ([2, 0], [0, 2]), ([2, 1.2], [-1.8, -1.4])]:
    print(np.dot(a, b))                         # positive, zero, negative
# 6.4 / 0 / -5.28

# The house: floor area, bathrooms, distance from the centre; the weights and the bias
x = np.array([50, 2, 10])
w = np.array([1.5, 8, -0.5])
print(w @ x + 25)
# 111.0

# 2. “legno” in numbers, with two different vocabularies
for testo in ["C'era una volta un pezzo di legno.", open("pinocchio.txt", encoding="utf-8").read()]:
    caratteri = sorted(set(testo))              # the vocabulary
    stoi = {c: i for i, c in enumerate(caratteri)}
    print(len(caratteri), [stoi[c] for c in "legno"])
# 18 [9, 6, 7, 10, 11]
# 69 [48, 42, 44, 50, 51]

onehot = F.one_hot(torch.tensor([stoi[c] for c in "legno"]), num_classes=len(caratteri))
print(onehot.shape)                             # one row per character, one column per symbol
# torch.Size([5, 69])

# 3. The row trick: one-hot times matrix = a row of the matrix
W = np.array([[0.1, 0.2], [0.3, 0.4], [0.5, 0.6], [0.7, 0.8]])   # the rows of a, e, l, o
l = np.array([0, 0, 1, 0])
print(l @ W, W[2])
# [0.5 0.6] [0.5 0.6]

In the book

This lab accompanies chapter 2, “Numbers in a row”. The chapter introduces scalars, vectors, matrices and tensors, the dot product and the matrix product with the rule of dimensions, broadcasting, the move from NumPy to PyTorch and the GPU, and closes by turning Pinocchio into a tensor of numbers and into one-hot vectors.

COVER COMING SOON
dltp · 2026

Deep Learning. Teoria, matematica e pratica delle reti neurali

Michele di Nuzzo · in Italian

Over the course of the book we build a small language model, a GPT, from scratch and grow it one piece at a time. At the start it is a model that counts which letter follows which. At the end it is a complete Transformer that writes dialogue, answers questions, reads images and draws the digits we ask for in words.

[the book’s page] [shop links coming soon]

The “Inside the Puppet” series