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

Determining Vector and Matrix Shapes in Simple Models

Hello. Last time, you separated an AI system’s input, model output, decision, and success criterion. Now we’ll look inside a simple model computation. Its inputs and parameters are arranged as vectors and matrices, and their shapes determine which calculations are possible—and how many outputs the model produces.

By the end of this lesson, you should be able to look at a computation such as , identify the shape of each part, and determine the shape of the result. Allow about 40 minutes for the lesson, including the videos and reading.


Count rows and columns first

A vector is an ordered collection of numbers. For example, three numerical features describing one support request might form the vector . A matrix arranges numbers in rows and columns. If each row describes one request and each column represents one feature, two requests with three features each form a matrix with 2 rows and 3 columns, written .

Watch Greg Hogg introduce vectors and matrices before we use them in a model calculation.

Linear Algebra Basics for Data Science & Machine Learning: Vectors, Matrices, and Operations Between

In “Linear Algebra Basics for Data Science & Machine Learning,” Greg Hogg builds the vocabulary of vectors, matrices, and dimensions. Watch for the difference between counting a vector’s entries and counting a matrix’s rows and columns.

Watch vectors for ordered entries and their dimensions. Then skip to matrices for the row-by-column convention and how to locate an entry within a matrix.

Suppose our feature matrix is

Its shape is . The first row describes one request; the second describes another. The three columns must represent the same three features in the same order for both requests. Shape tells us how the numbers are arranged, but not what they mean.

The image below shows why that distinction matters. The same six numbers can fit into either arrangement, but a matrix and a matrix would imply different numbers of examples and features in our dataset.

The values 1 through 6 are arranged as a one-dimensional array, a 2-row-by-3-column matrix, and a 3-row-by-2-column matrix. Reshaping preserves the values while changing which positions count as rows and columns.

A single three-feature example can be written as a row vector of shape , or as a column vector of shape . Those orientations are not interchangeable in multiplication. In NumPy, an ordinary one-dimensional array of three values has shape : it has three entries but does not itself have a row or column axis. When orientation matters, make the intended row or column shape explicit.


Multiplication has a shape requirement

To understand matrix multiplication, start with a dot product: pair corresponding entries, multiply each pair, and add the products. For instance, the row and a column containing produce . Both must contain three entries so that every entry has a partner.

Greg Hogg demonstrates that same operation for every row of a matrix. Watch the dot-product explanation, then the matrix–vector example.

Linear Algebra Basics for Data Science & Machine Learning: Vectors, Matrices, and Operations Between

Return to Greg Hogg’s “Linear Algebra Basics for Data Science & Machine Learning.” This time, focus on why a matrix needs one column for each entry in the vector it multiplies.

Watch dot products for the pair-multiply-and-sum operation. Then watch matrix–vector multiplication: notice that each matrix row produces one number, so the result has one entry per row.

The same principle works when the right-hand side has several columns. Khan Academy’s article makes the general shape rule explicit.

Matrix multiplication dimensions (article) | Khan Academy

Read Khan Academy’s “Matrix multiplication dimensions” to connect the row-by-column calculation to a dependable rule for predicting output shapes.

Start under “What you should be familiar with before taking this lesson” and read the matrix definition and dimensions paragraph; row and column counts are given in that order. Continue through “When is matrix multiplication defined?” beginning with the compatibility rule. Finish “Dimension property,” including the worked example that ends by identifying a 3\times4 product. Focus on why a row of the first matrix must have as many entries as a column of the second.

Here is the rule in compact form:

The two inner counts must match: each row on the left has entries, and each column on the right has entries. The outer counts give the result: rows and columns. If the inner counts differ, ordinary matrix multiplication is not defined. The order matters; reversing the matrices can produce a different shape or make the multiplication impossible.

This rule is a check on structure, not a substitute for meaning. A calculation can have compatible shapes even if its columns represent the wrong features in the wrong order.


Trace one model computation

In the previous lesson’s support-request example, a model might use request features to produce scores that inform routing. We can trace the shapes of a small scoring calculation without needing to decide yet how those scores become actions.

In the following StatQuest excerpt, one input row is multiplied by a matrix of weights—coefficients the model uses for its calculations—and then bias values are added. The excerpt uses two input features and two resulting values.

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

Watch “Essential Matrix Algebra for Neural Networks, Clearly Explained!!!” from StatQuest with Josh Starmer for a concrete model computation. The important idea here is the placement and shape of the inputs, weights, and biases, rather than how the weights were learned.

Watch the model example. Track the single row of two input features, the weight matrix’s two output columns, and the two bias values added to the result.

Now extend that arrangement to our two requests, each with three features. Suppose we want two scores per request. We need a weight matrix with three rows—one for each input feature—and two columns—one for each output score:

Before calculating any values, check the shapes: is , and is . The inner counts match, so has shape : two requests, each with two scores.

The values confirm what the shapes predicted. The first result entry pairs the first request’s three features with the first weight column:

Doing the same row-by-column calculation for all four entries gives

Suppose the two output scores also have biases . There is one bias per output column, not one per request. To add biases using ordinary matrix addition, repeat that row for each request:

Here and are both , so their entries can be added position by position. Array libraries often perform that row repetition automatically: they can broadcast a bias row of shape across the result. That is a library convenience, not a change to the matrix-multiplication rule.

If we wanted one score per request instead, the weight matrix would have shape . With the same input matrix, the result would be . Notice how changing the number of weight columns changes the number of outputs for each example, while changing the number of input rows changes how many examples are processed.

These numbers are scores, not automatically probabilities, classifications, or routing decisions. Those interpretations require further choices. For now, shapes let you verify that the model produces the intended number of values for the intended number of requests.


Keep the shape contract in view

When tracing a model calculation, identify what each axis represents: examples, features, or outputs. For a batch with examples, features, and outputs per example, the input has shape , the weights have shape , and the product has shape . Biases must supply one value per output and be added in a shape-compatible way.

This habit catches errors before you inspect any arithmetic. In the next lesson, you’ll examine how a model changes a parameter to reduce a loss. Today’s shape checks tell you which computations are possible; the next lesson asks how their values can improve.

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

Sign up