Partial derivative — Differentiating multivariate functions one variable at a time
Partial derivative -- Differentiating multivariate functions one variable at a time
Previous derivatives were for univariate functions f(x). Neural networks have millions of parameters, and we need to differentiate with respect to each parameter.
Partial derivative = Differentiating a multivariate function with respect to only one variable, treating the others as constants.
Concept analysis
Previously, we only dealt with the derivative of a univariate function f(x). However, in the real world, the outcome of things is usually determined by multiple factors working together.
For example: housing prices depend on multiple factors such as area, location, floor, and age. If we want to know "how much the housing price increases when the area increases by 1 square meter," we need to keep other factors fixed.
This is the core idea of the partial derivative: changing only one variable at a time and observing its effect on the result, while temporarily freezing the other variables.
For ( f(x, y) = x^2 + xy + y^2 ):
- Partial derivative with respect to x (y is constant, the derivative of y^2 is 0, in xy, y is constant and the derivative of x is 1): ( \frac{\partial f}{\partial x} = 2x + y + 0 )
- Partial derivative with respect to y (x is constant, similarly): ( \frac{\partial f}{\partial y} = 0 + x + 2y )
The symbol ( \partial ) (read as "round d" or "partial") denotes the partial derivative, distinguishing it from d in ordinary derivatives.
Second-order partial derivatives
Taking the partial derivative of the partial derivative yields the second-order partial derivatives:
- ( \frac{\partial^2 f}{\partial x^2} ): Taking the partial derivative with respect to x, and then taking the partial derivative with respect to x again (measures the curvature in the x direction)
- ( \frac{\partial^2 f}{\partial x \partial y} ): First taking the partial derivative with respect to x, and then taking the partial derivative with respect to y (mixed partial derivative, measures the interactive effect of the two variables)
The essence of neural network training is:calculating the partial derivative of the loss function with respect toevery parameter, and then updating along the negative gradient direction.
A model with a million parameters has a gradient that is a vector of one million partial derivatives.
A real-life example
**The taste of a pot of soup
The taste of the soup depends on various seasonings such as salt, soy sauce, and vinegar. You want to know "how much the taste changes if I add one more spoonful of salt"?
Keep the amounts of other seasonings fixed and only look at the effect of salt—this is the mindset of partial derivatives: change only one variable at a time.
Mathematical definition
[ \frac{\partial f}{\partial x} = \lim_{h \to 0} \frac{f(x+h, y) - f(x, y)}{h} ]
Keep y constant and only let x change.
Python hands-on practice
Example
import
sympy
as
sp
x
,
y
=
sp.
symbols
(
'x y'
)
f
=
x**
2
- x*y + y**
2
(
f
"f(x,y) = {f}"
)
(
f
"∂f/∂x = {sp.diff(f, x)}"
)
2x + y
(
f
"∂f/∂y = {sp.diff(f, y)}"
)
x + 2y
Second-order partial derivative
(
f
"∂²f/∂x² = {sp.diff(f, x, 2)}"
)
2
(
f
"∂²f/∂x∂y = {sp.diff(f, x, y)}"
)
1
Partial derivative of the linear model loss with respect to the parameters
w
,
b
,
xi
,
yi
=
sp.
symbols
(
'w b x_i y_i'
)
loss
=
(
w*xi + b - yi
)
**
2
(
f
"\nloss=(w·xi+b-yi)^2"
)
(
f
"∂L/∂w = {sp.diff(loss, w)}"
)
(
f
"∂L/∂b = {sp.diff(loss, b)}"
)
Output is:
f(x,y) = x**2 + x*y + y**2
∂f/∂x = 2*x + y
∂f/∂y = x + 2*y
∂²f/∂x² = 2
∂²f/∂x∂y = 1
loss=(w·xi+b-yi)^2
∂L/∂w = 2*x_i*(b + w*x_i - y_i)
∂L/∂b = 2*b + 2*w*x_i - 2*y_i
Application scenarios in AI
Neural network training = millions of partial derivative calculations
A million-parameter model has a loss that is a multivariate function of a million variables. In each training round, the automatic differentiation engine calculates the partial derivative for every parameter, obtaining a vector of a million partial derivative values that form the gradient vector, and updates all parameters along the negative gradient direction.
Local partial derivatives in backpropagation
Each node in the computation graph needs to calculate a local partial derivative: addition node ($\partial/\partial x = 1$) (gradient passed back as is), multiplication node ($\partial(xy)/\partial x = y$) (gradient passed back multiplied by the other value). These local partial derivatives are multiplied along the computation graph to obtain the final parameter gradient.
Transfer learning with frozen parameters
In transfer learning, pre-trained layers are often "frozen"—the parameters of these layers are not differentiated or updated. In PyTorch, setting requires_grad=False skips the partial derivative calculation for the corresponding parameters, significantly saving memory and computation.