Hello! Welcome to the start of our third module, "Vector Calculus for Automatic Differentiation." In our previous lessons on matrix decompositions, we developed a powerful geometric intuition for analyzing data and model properties, culminating in our derivation of PCA from the spectral theorem. We now pivot from analysis to optimization. The engine that drives deep learning is gradient-based optimization, and the core of that engine is the ability to efficiently compute derivatives of complex, high-dimensional functions.
This module will build the mathematical machinery for automatic differentiation from the ground up. Today, we begin with the most fundamental building block: the gradient. You will learn how to compute derivatives of scalar-valued functions with respect to vectors and matrices—the kind of functions that represent the loss in a neural network. A key focus will be on navigating the different notational conventions, specifically numerator and denominator layouts, which you will inevitably encounter in papers and textbooks. This will lay a solid foundation for understanding backpropagation in the lessons to come.
1. The Gradient: Derivatives with Respect to Vectors
In deep learning, we typically optimize a scalar loss function, , which is a function of potentially millions of parameters (weights and biases) arranged in vectors and matrices. To use gradient descent, we need to compute how a small change in each parameter affects the final loss. This is captured by the gradient.
For a scalar-valued function of a vector, , the gradient is the vector containing all the partial derivatives of with respect to each element of the input vector .
A crucial, and often confusing, point is how these partial derivatives are arranged into a vector. There are two common conventions, known as layout conventions.
The Matrix Calculus You Need For Deep Learning - explained.ai
This article provides a practical introduction to the topic. It clearly defines the gradient and immediately introduces the distinction between numerator and denominator layouts.
Please read from the beginning of the section Matrix calculus up to the end of the paragraph on the denominator layout, ending with this equation. Pay close attention to how the Jacobian is constructed by stacking gradient vectors and how the two layouts are simply transposes of each other.
To summarize the conventions from the reading:
Let and .
-
Numerator Layout: The gradient is a row vector of size .
-
Denominator Layout: The gradient is a column vector of size .
Neither convention is universally "correct," but different fields and authors have strong preferences. Physics and mathematics often prefer the denominator layout, while some machine learning resources, like the article you just read, use the numerator layout. Deep learning frameworks like PyTorch and TensorFlow handle this ambiguity internally, but when you're deriving gradients by hand or reading papers, it's critical to identify which convention is in use.
The following video provides a concise recap and uses the denominator layout, offering a direct contrast to the article.
This video by Herman Kamper gives a clear, alternative presentation of the same concepts.
First, watch the definition of the derivative of a scalar function with respect to a vector here. Note that he defines the gradient as a column vector (denominator layout). Then, watch the segment on layout conventions. This will reinforce the point that the two conventions exist and are often just transposes of each other.
For the rest of this lesson, we will derive gradients using a method that makes the layout explicit at the end, and we will state the result in both conventions.
2. A Coordinate-Free Approach to Gradients
While you can always compute a gradient by grinding through the partial derivatives for each element, this becomes tedious and error-prone for complex functions. A more elegant and powerful method, particularly suited to your background, is to work with vectors and matrices holistically. This approach, championed by Alan Edelman in his MIT lectures, treats the derivative as the best linear approximation to a function's change.
The core idea is this: for a small perturbation to a vector , the change in the function , denoted , is approximated by the inner product of the gradient with the perturbation:
In the denominator layout (gradient is a column vector), this is the dot product . In the numerator layout (gradient is a row vector), this is . We can write this more symmetrically as if the gradient is a column, or if the gradient is a row. The strategy is to find an expression for and then rearrange it into one of these forms to identify the gradient.
Let's see this in action.
Matrix Calculus 2020 04 16 for MIT Linear Algebra 18.06 Spring 2020 (Alan Edelman)
In this excerpt from his MIT 18.06 course, Alan Edelman demonstrates this powerful "perturbation" method. He avoids element-wise calculations, instead relying on the product rule and properties of vector/matrix operations.
Watch the section from Gradients, where he introduces scalar functions of vectors and the example f(\mathbf{x}) = \mathbf{x}^T\mathbf{x}. Then, watch carefully how he uses the product rule for vectors to derive the gradient of \mathbf{x}^T\mathbf{x} without expanding into components. This is the key technique. The relevant segment is from the product rule.
Let's formalize the derivation from the video. We want to find the gradient of .
- Find the differential :
- Apply the product rule, :
- Recognize that each term is a scalar (a matrix). A scalar is equal to its own transpose. So, we can transpose the first term: As Edelman points out, this is because the dot product is commutative.
- Substitute this back into the expression for :
- Now, we match this to our target forms:
- Numerator Layout: We are looking for . Comparing this with , we identify the gradient as the row vector .
- Denominator Layout: We are looking for . We can write , so we identify the gradient as the column vector .
This method is powerful because it relies on algebraic manipulation of vectors and matrices, which is much cleaner than element-wise calculus for complex expressions.
3. Gradients of Common Machine Learning Functions
Let's apply this technique to a few more examples that are central to machine learning.
A. Least Squares Loss
A fundamental problem in ML is minimizing the squared error, . This is the loss function for linear regression. Let's find its gradient with respect to .
Matrix Calculus 2020 04 16 for MIT Linear Algebra 18.06 Spring 2020 (Alan Edelman)
Continue with the same Alan Edelman video. He now applies the exact same product rule technique to derive the gradient for the least squares objective.
Watch his derivation from this example. Follow how he applies the product rule, handles the constants \mathbf{A} and \mathbf{b}, and manipulates transposes to isolate the d\mathbf{x} term.
The derivation is as follows:
- Rewrite the norm: .
- Apply the product rule: .
- The differential of is simply , since and are constants.
- Substitute this in: .
- Simplify the transposed term: .
- Again, both terms are scalars, so they are equal.
- Identify the gradients:
- Numerator Layout:
- Denominator Layout:
Setting the gradient to zero, , gives the famous normal equations for the least-squares solution.
B. Gradient with Respect to a Matrix
The same principle extends to finding the gradient of a scalar function with respect to a matrix, . Here, the gradient is a matrix of the same dimensions as , where each element is .
The linearized change is now expressed using the Frobenius inner product:
Our goal is to manipulate into this trace form to identify .
Matrix Calculus 2020 04 16 for MIT Linear Algebra 18.06 Spring 2020 (Alan Edelman)
Finally, watch Edelman extend this method to matrices. He uses the trace and its cyclic property as the key tool.
Watch the segment from scalar functions of matrices where he derives the gradient of f(A) = \text{tr}(A^2). This demonstrates how the trace properties (linearity, cyclicity) are essential for matrix calculus.
Let's follow his derivation for .
- Find the differential: .
- Use linearity of trace and product rule: .
- Use linearity again: .
- Use the cyclic property of the trace on the first term: .
- Combine terms: .
- To match the target form , we rewrite as .
- By inspection, we identify the gradient: .
This method, combining differentials with the product rule and trace properties, is a remarkably efficient way to derive gradients for complex matrix expressions, which are ubiquitous in deep learning.
4. Back to the Computation Graph
Let's connect this back to a simple neural network. Consider the forward pass for a single neuron, which is essentially logistic regression.

Our goal in training is to compute and to update the parameters. The tools we've developed in this lesson are the first step. For example, if the loss function is Mean Squared Error, , this is a scalar function of the vector . We can now compute using the techniques from today. The next lesson will show how to use the chain rule to propagate this gradient further backward to get the gradients with respect to and .
Conclusion
In this lesson, we established the fundamental tools of vector calculus needed for deep learning. You've learned how to formalize the concept of a derivative with respect to a vector or matrix.
Key Takeaways:
- The gradient of a scalar function is the collection of its partial derivatives, .
- Layout conventions are critical: the gradient can be a row vector (numerator layout) or a column vector (denominator layout). They are transposes of each other, and you must be consistent.
- A powerful, coordinate-free method for finding gradients is to compute the differential and manipulate it into the form . The matrix is then the gradient .
- This method relies on the product rule for vectors/matrices and the linearity and cyclic properties of the trace.
We now have the ability to differentiate the final stage of a computation graph—the loss function. In the next lesson, we will introduce the Jacobian and the chain rule, which will allow us to combine these derivatives and compute gradients for arbitrarily deep composite functions, forming the basis of backpropagation.