Welcome back. Last time, you interpreted a gradient as the complete first-order description of local change: it collects partial derivatives, points toward steepest increase, and has one component per input. Now we make that object concrete by calculating it.
For speech ML, this is the manual version of what PyTorch will later do automatically. A scalar training objective depends on many parameters; the gradient tells us the sensitivity of that objective to each one. The calculations here are deliberately small, but the pattern is the same at model scale.
The calculation rule: freeze all other variables
For a function of two variables,
the gradient is
To calculate , treat as a constant and differentiate normally with respect to . To calculate , reverse those roles: treat as constant.
This is the essential operational rule:
Differentiate one variable at a time; temporarily regard every other variable as a constant.
For example, in the term
is a constant when differentiating with respect to , so
But is a constant when differentiating with respect to , so
You do not need the product rule for this term: in each partial derivative, only one factor is allowed to vary.
Partial Derivatives and the Gradient of a Function
Watch “Partial Derivatives and the Gradient of a Function” by Professor Dave Explains for a compact visual demonstration of this rule and the construction of a gradient vector.
Watch the setup for the meaning of holding one variable fixed. Then watch the worked example, pausing after each partial derivative to reproduce it on paper. Finish with the gradient definition, focusing on the fact that partial derivatives become components of one vector.
A small derivative reference is enough for the polynomial functions in this lesson:
| Expression | Derivative with respect to its variable |
|---|---|
| constant | |
Here, means a constant. During a partial derivative, another variable can play the role of .
A full two-variable gradient calculation
Consider
We will calculate the gradient symbolically first, then evaluate it at a particular point. That order matters: the symbolic gradient tells us how sensitivity varies across the whole input space.
First component: differentiate with respect to
Hold fixed.
Term by term:
Therefore,
Notice that becomes , not . While differentiating with respect to , the entire quantity is just a constant coefficient multiplying .
Second component: differentiate with respect to
Now hold fixed.
This time,
and the two terms containing no vanish:
So,
Assemble the gradient
Place the partial derivatives in the same order as the inputs:
At the point ,
This says that near :
- Increasing , while keeping fixed, decreases at a local rate of about .
- Increasing , while keeping fixed, decreases at a local rate of about .
The local direction of steepest increase is the gradient itself. The local direction of steepest decrease is its negative:
4.3 Partial Derivatives - Calculus Volume 3
Read OpenStax’s explanation of the operational method behind partial derivatives, then see how exactly the same method extends from two to three variables.
In the subsection following “Checkpoint 4.12,” read the calculation rule. Pay particular attention to why terms not containing the active variable have derivative zero. Then go to “Functions of More Than Two Variables.” Read the three-variable extension. The notation becomes longer, but the method does not change.
Three variables: one partial derivative per input
The procedure scales directly. For a scalar-valued function
the gradient is
Consider the three-parameter scalar objective
Although this is a toy expression, treat as three model parameters. Each partial derivative measures sensitivity to one parameter while freezing the other two.
With respect to :
The second and third terms contain no , so they are constants and disappear.
With respect to :
With respect to :
Therefore,
At
we obtain
The gradient has three entries because the input has three entries. More generally, if
then
A modern speech model has far more than three parameters, often organized into matrices and higher-dimensional tensors. Nevertheless, the principle remains unchanged: every parameter has a corresponding gradient entry, and the gradient tensor has the same shape as the parameter tensor.
A reliable workflow for manual gradients
When calculating a gradient by hand, use this routine:
-
Confirm the output is scalar. A gradient in this introductory sense is associated with a scalar-valued function, such as a loss or objective.
-
List the input variables in a fixed order. For example, or . This is the order of gradient components.
-
Take one partial derivative at a time. Circle the active variable mentally; freeze every other variable.
-
Differentiate term by term. Any term without the active variable becomes zero.
-
Assemble the results into a vector. Do not accidentally place in the first slot.
-
Evaluate at a point only after forming the symbolic gradient. This avoids losing the function-level view of the calculation.
A quick numerical check can help catch sign and coefficient errors. For a small number ,
For the earlier function at , the exact first partial derivative is
Using a small positive , the finite-difference estimate should be close to . This is only a sanity check; symbolic differentiation is exact for the functions studied here. Later, automatic differentiation will compute exact derivatives of the operations in a model’s computation graph rather than estimate them numerically.
Common mistakes to eliminate early
| Mistake | Why it is wrong | Correction |
|---|---|---|
| Differentiating every variable at once | A partial derivative permits only one active variable. | Freeze all non-active variables. |
| Keeping a term that lacks the active variable | It is a constant with respect to that variable. | Its derivative is . |
| Dropping another variable entirely | A frozen variable is still a coefficient. | Keep it in the result, such as with respect to . |
| Using the wrong gradient order | Components must align with the original input order. | Write the variable ordering explicitly before assembling the vector. |
| Confusing with | The first is a vector-valued function; the second is a specific vector. | Form the symbolic gradient, then substitute the point. |
In ML code, these are not merely notation issues. A gradient assigned to the wrong parameter, reshaped incorrectly, or interpreted with the wrong sign can make optimization fail even when the model architecture is otherwise sound.
Key takeaways
- To calculate a partial derivative, differentiate with respect to one variable and treat all others as constants.
- The gradient of a scalar function is the vector of its partial derivatives, listed in input order.
- For
the gradient has two components; for
it has three; in general it has one component per input dimension.
- A term that does not contain the active variable contributes zero to that partial derivative.
- Build the symbolic gradient first, then evaluate it at a particular point.
- This manual procedure is the foundation for interpreting the parameter gradients that PyTorch will eventually produce.
Next, you will handle functions built from several nested operations. The chain rule will show how gradients pass through those compositions—the essential mechanism behind backpropagation in neural networks.
Can't find a good explanation? Sign up and we'll make it for you
Sign up