Building a Matrix from Scratch
A single matrix multiplication can turn a list of 512 random numbers that represent a Wikipedia article into a new list of 512 numbers that the transformer suddenly "understands" as related to similar articles.
Problem
Imagine you are building a self-evolving knowledge wiki powered by a small language model. Every time a user adds or edits an article, you convert its text into a numerical list called an embedding. This embedding must then pass through the first layer of your transformer to become a hidden state — a richer representation that captures meaning. Without a mathematical engine to perform this conversion reliably, your wiki cannot connect related ideas or surface relevant pages. That engine is a weight matrix, and we will build one from scratch in TypeScript.
Concept
Think of a vector the way you think of a shopping list: a simple ordered collection of numbers. Each number is a feature. An embedding is a special kind of vector that encodes the meaning of a token or a short piece of text. In our wiki, the word "transformer" might become the vector [0.12, -0.45, 0.78, ...] with 512 numbers.
A matrix is simply a grid of numbers arranged in rows and columns. When we use a matrix inside a neural network we call it a weight matrix because each number (weight) decides how strongly one feature from the input should influence a feature in the output.
A linear transformation is what happens when you multiply this weight matrix by your input vector. Geometrically it stretches, rotates, or shears the entire space of possible embeddings so that similar wiki articles end up closer together in the new space. Algebraically it is a compact way to mix every input number with every weight to produce a new vector — our first hidden state.