Deep Learning Basics

Matrix multiplication is the first batch trick

3 min read

Start with the calculation that keeps getting repeated:

output = input_1 * weight_1 + input_2 * weight_2 + bias

That is a single weighted sum. Matrix multiplication is the bookkeeping that lets the same operation cover a whole set of weighted sums.

Read the result one cell at a time. Each cell comes from one row of the left matrix and one column of the right matrix: multiply matching entries, then add the products. There is no new kind of arithmetic hiding in the notation.

In this example, Matrix A is 2x2 and Matrix B is 2x3, so Matrix C is 2x3.

A rows: 2
B columns: 3
C shape: 2 x 3

The shared middle dimension has to match. Here it is 2, so a 2x2 matrix can multiply a 2x3 matrix. The result keeps the outside dimensions and becomes 2x3.

That shape rule is often more useful than the arithmetic when debugging neural network code. A mismatched inner dimension tells you that the data is arranged incorrectly before you need to inspect any individual number.

Pick one output cell:

C[1, 2] = A row 1 dot B column 2

That is the entire definition at the cell level. A value in Matrix A influences the row of output cells connected to it. A value in Matrix B influences the columns connected to it. The grid makes that dependency easier to inspect than a large equation.

the neural network connection

A layer with several neurons has several weight vectors. Put those vectors together and they form a matrix. A batch gives the layer several inputs to process with those same weights.

outputs = weights * inputs

Then the network adds bias and usually applies an activation function. Matrix multiplication does not replace either step. It packages the repeated weighted sums so the computer can evaluate them together.

That is why batches make the operation feel natural. Three examples do not require three different models. They require one model applied to three inputs, with the same weights reused each time.

shape checks are debugging tools

If A is 2x2 and B is 2x3, the multiplication works. Swap the shapes and it does not. A wrong transpose can make features look like examples, or examples look like features. Sometimes that causes an immediate error. Sometimes it produces plausible nonsense.

Before running a larger example, ask:

  • how many rows should the result have?
  • how many columns should it have?
  • which dimension has to match?
  • what does each row stand for?
  • what does each column stand for?

Those questions are simple, but they catch a large class of shape bugs.

Change one number in Matrix A and predict which row of Matrix C should move. Then change one number in Matrix B and predict which cells should change. The dependency pattern is the useful result of the exercise.

Rows meet columns. Dot products become cells. Cells become a layer output. A 2x2 times 2x3 example is the same operation a larger network uses, with more rows, more columns, and better hardware doing the repetition.

Jeremy London

About Jeremy London

Engineering leader and builder in Denver. I write about AI platforms, agents, security, reliability, homelab infrastructure, and the parts of engineering work that have to survive production.