This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Notebooks · lib/params

A model's parameters as one vector and a table

A model is several matrices of different shapes: an embedding, a few projections, an output layer. Training keeps more arrays of exactly those shapes: the gradient, and whatever the optimizer remembers between steps (a momentum, a running average of squared gradients). Written out one variable per matrix, every one of those arrays becomes six variables, and every loop over them is written six times.

This module keeps all of a model's numbers in one flat vector and keeps a small table beside it that says where each matrix lives: the offset of its first entry, its number of rows and its number of columns. A matrix is stored row by row from its offset. The table is the model's layout, and anything with the same layout (a gradient, a momentum) is one more vector of the same length. An optimizer is then one loop over one vector, or one loop over the table when it needs each matrix whole.

A tensor is named by its row in the table. Declaring a shape returns that row, so a program names its tensors as it declares them:

model = param_table(2)
EMBED = param_declare(model, 6, 8)
OUT = param_declare(model, 8, 6)
theta = param_zeros(model)

Used with import params.

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.

[1]lines 31–189