Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Output Shapes in Neural Network Vector and Matrix Multiplication

Hello! This is the first lesson in the mathematical foundations of the course. Your introductory machine-learning work has already involved data with rows of examples and columns of features. Neural networks use that same kind of data, but pass it through many matrix operations. To follow those operations—and eventually the equations in Attention Is All You Need—you need to know what shape each result will have before calculating any numbers.

Today’s focus is shape reasoning: deciding whether a vector or matrix multiplication is valid, then finding the output shape. Allow about 35–40 minutes, including the short videos and reading.


The rule that governs matrix multiplication

A matrix’s shape is written rows by columns. A matrix with two rows and four columns has shape , regardless of the values in its entries.

Suppose has shape and has shape . Their product has shape

The inner dimensions must match: must have as many columns as has rows. The outer dimensions give the result: it has ’s number of rows and ’s number of columns. If the inner dimensions differ, this matrix product is not defined.

Watch Khan Academy establish the condition and show how entries of the result are formed:

Multiplying a matrix by a matrix | Matrices | Precalculus | Khan Academy

“Multiplying a matrix by a matrix” from Khan Academy makes the shape rule visible before doing the arithmetic.

Watch the shape check, then the multiplication. Notice that one row of the first matrix and one column of the second produce one entry of the output.

Why does the inner dimension have to match? Each output entry pairs a row of with a column of , multiplying corresponding values and adding them. Those two groups need the same number of values. There is one output entry for every choice of an row and a column, which explains the result.

A \(2\times 4\) matrix multiplied by a \(4\times 3\) matrix produces a \(2\times 3\) matrix. The four shared positions determine whether multiplication is possible; the two rows and three columns determine the output shape.

In the illustrated example, the top-left output entry is : the first row of is paired with the first column of . You do not have to calculate the other five entries to know that the product has shape . In a neural network, checking shapes first can save you from doing substantial arithmetic—or debugging—with incompatible inputs.


Vectors follow the same rule

A vector can be written as a column vector, with shape , or a row vector, with shape . Its orientation matters when you multiply it by a matrix.

If has shape and is a column vector of shape , then

The result is a column vector with entries. For example, a matrix that turns four input features into three outputs has shape . Multiplying it by one input gives a output.

The row-vector version puts the input on the left. If has shape and has shape , then

Here, one row of input features becomes one row of outputs. Neither layout is inherently more correct; the weight matrix must be arranged to match the chosen layout.

Two other vector products reveal why orientation cannot be ignored. A row vector times an column vector produces a result—a single value. In the opposite order, an column vector times a row vector produces an matrix. The entries could involve the same vector values, yet the order changes the output shape. We will explore the meaning of the single-value product in the next lesson.

One notation detail will matter when we reach code: a one-dimensional array may have shape written as , rather than explicitly or . It has elements but no visible row or column axis. For now, use explicit row and column shapes when reasoning on paper; later, we will see how PyTorch handles one-dimensional tensors.


Recognizing the operation inside a neural network

A neural network layer often takes several input features and produces several output features. Matrix multiplication lets it perform those calculations together. This is a move beyond an introductory workflow that treats a model as a function accepting a feature table: we will track the dimensions inside that function.

Watch the neural-network portion of StatQuest’s explanation. You need only the transformation shapes here, not the details of training or activation functions.

Essential Matrix Algebra for Neural Networks, Clearly Explained!!!

“Essential Matrix Algebra for Neural Networks, Clearly Explained!!!” by StatQuest with Josh Starmer shows inputs multiplied by learned weights to produce new features and, later, class scores.

In the neural-network example, watch the two transformations. Track how the input row becomes a row with two values, then how those two values become three class scores. You can set aside the discussion of how the weights were learned.

In that example, the first multiplication takes two input features to two intermediate values: its output shape is . A later multiplication takes those two values to three class scores: its output shape is . The shapes tell you how many values exist at each stage, even if you do not know the weights.

Stanford’s CS231n notes provide the same idea using column vectors. Read the worked network example to see why its multiplication order looks different from the video’s while following the same rule:

CS231n Deep Learning for Computer Vision

The “Example feed-forward computation” section in Stanford’s CS231n notes follows input, hidden, and output shapes through repeated matrix multiplications.

Under “Neural Network architectures,” find “Example feed-forward computation.” Read the worked example, starting with the explanation of layer-wise matrix-vector operations, continuing through the forward-pass code, and ending with the paragraph about processing a batch. Focus on the shapes of the input, each weight matrix, and each intermediate result; the activation-function details are not needed yet.

CS231n starts with a input column. Its first weight matrix is , so the first layer returns a column:

The next weight matrix is , so the feature count stays at four. The last is , producing a output. At each layer, the output feature count becomes the required input feature count of the next layer. That is the shape relationship that makes a chain of layers possible.


From one example to a batch

Models commonly process multiple examples at once. Put examples into a matrix , one example per row, with features per example. Then has shape . To produce values per example, use a weight matrix with shape :

For instance, five examples with four features each can be transformed into three scores each:

The batch still contains five examples. Each example now has three outputs instead of four input features. This is a useful way to read a result shape: the first dimension counts examples; the second counts the values produced for each example.

CS231n briefly describes a different batch layout: examples can instead be placed in columns. In that convention, the weight matrix goes on the left. When reading neural-network notation, do not memorize a single multiplication order without checking it. Identify what each dimension counts, confirm the inner dimensions match, and then read the outer dimensions as the output.

That habit will remain useful when the matrices in the Transformer paper become larger and acquire names such as queries and keys. Their roles come later; their multiplication shapes obey exactly the rule learned here.


Key takeaways

For a matrix product, check the inner dimensions to decide whether it is valid and keep the outer dimensions to find its shape. Treat a row vector as and a column vector as ; their orientation can change the result from one value to an entire matrix. In a neural-network layer, follow what each axis counts—examples, input features, or output features—rather than relying on memorized symbols.

Next, we will examine the dot product: the single-value calculation inside matrix multiplication, and what it can tell us about the alignment of two vectors.

Can't find a good explanation? Sign up and we'll make it for you

Sign up