1.2.3Calculus & Optimization Basics

Partial derivatives

1,516 words7 min readdifficulty · medium6 backlinks

WHY do we need this?

In ML almost every loss function depends on many parameters at once: L(w1,w2,,wn,b)L(w_1, w_2, \dots, w_n, b). To train a model we ask "how does the loss respond to each weight separately?" so we know which direction to push each weight. That per-variable slope is the partial derivative, and stacking them all gives the gradient that gradient descent walks along.


HOW to compute it (the mechanical rule)

WHAT you do: treat every other variable as a number and differentiate normally.

WHY this works: the limit above only changes xx; yy never moves, so from the derivative's point of view yy behaves exactly like the constant 55 or π\pi would.


Derivation from first principles

Let f(x,y)=x2y+3yf(x,y) = x^2 y + 3y. Compute fx\dfrac{\partial f}{\partial x} straight from the limit.

fx=limh0[(x+h)2y+3y][x2y+3y]h\frac{\partial f}{\partial x} = \lim_{h\to 0} \frac{\big[(x+h)^2 y + 3y\big] - \big[x^2 y + 3y\big]}{h}

Why this step? We plug x+hx+h into every xx, but leave yy untouched (that's the definition).

=limh0(x2+2xh+h2)y+3yx2y3yh= \lim_{h\to 0} \frac{(x^2 + 2xh + h^2)y + 3y - x^2 y - 3y}{h}

Why this step? Expand (x+h)2(x+h)^2 so we can cancel the terms that don't contain hh.

=limh02xhy+h2yh=limh0(2xy+hy)=2xy= \lim_{h\to 0} \frac{2xhy + h^2 y}{h} = \lim_{h\to 0}(2xy + hy) = 2xy

Why this step? The x2yx^2y and 3y3y cancel, we factor hh, cancel it, then let h0h\to 0. The 3y3y term vanished because it had no xx — its slope in the xx-direction is zero.

✅ Same answer as the shortcut: differentiate x2yx^2 y as 2xy2xy (with yy a constant), and 3y3y as 00.


Figure — Partial derivatives

Dual coding: the surface z=f(x,y)z=f(x,y) is sliced by a plane y=y=const. On that slice you get a 1-D curve; its slope is f/x\partial f/\partial x. Slicing with x=x=const gives f/y\partial f/\partial y.


Worked examples


Common mistakes


Flashcards

What does f/x\partial f/\partial x measure?
The rate of change of ff as xx changes while all other variables are held constant.
In computing f/x\partial f/\partial x, how do you treat yy?
As a constant (like a fixed number).
Limit definition of f/x\partial f/\partial x
limh0f(x+h,y)f(x,y)h\lim_{h\to0}\frac{f(x+h,y)-f(x,y)}{h}.
(x2y)/x=?\partial(x^2y)/\partial x = ?
2xy2xy.
(exy)/y=?\partial(e^{xy})/\partial y = ?
xexyx\,e^{xy} (chain rule, inner factor xx).
What is the gradient f\nabla f?
The vector of all partial derivatives; points in the direction of steepest ascent.
For L=(wx+bt)2L=(wx+b-t)^2, L/w=?\partial L/\partial w = ?
2(wx+bt)x2(wx+b-t)\,x.
Why does a term with no xx vanish in /x\partial/\partial x?
Its slope in the xx-direction is zero — it doesn't change when only xx moves.

Recall Feynman: explain to a 12-year-old

Imagine a hilly landscape where your height depends on how far east (xx) and how far north (yy) you stand. A partial derivative is: "If I take one step EAST only (not north), do I go uphill or downhill, and how steep?" You answer by looking only at the east–west slope and totally ignoring the north–south tilt for that moment. Do it again facing north, and now you know the slope in both directions — enough to figure out the fastest way down the hill. That "fastest way down" is exactly what a computer uses to learn.


Connections

  • 1.2.02-Derivatives-single-variable — the 1-D case a partial reduces to.
  • 1.2.04-Gradient-and-directional-derivatives — partials assembled into f\nabla f.
  • 1.2.05-Chain-rule-multivariable — needed for backpropagation.
  • 1.3.01-Gradient-descent — uses these partials to update weights.
  • Linear-regression — Example 3 is its exact learning rule.

Concept Map

freeze all but one var

defined by

other vars held constant

gives mechanical rule

verifies

stack all together

walked by

per-weight slope needed

trained via

geometric view

slope of 1-D curve

Partial derivative

Ordinary derivative

Limit as h to 0

Treat y as a number

Differentiate normally

First-principles derivation

Gradient

Gradient descent

ML loss L of many params

Slice surface at y=const

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, partial derivative ka funda bilkul simple hai. Jab tumhara function ek se zyada variable pe depend karta hai — jaise f(x,y)f(x,y) ya ML mein loss L(w,b)L(w,b) — tab tum ek time pe sirf ek variable ko thoda hilate ho aur baaki sabko freeze (constant maan lete ho). Jitna output badla, wahi us variable ka partial derivative hai. Bas itna hi. Normal derivative hi hai, sirf yaad rakhna ki baaki variables ko number ki tarah treat karna hai.

Trick yaad rakho: "Freeze Others, Differentiate". Agar tum f/x\partial f/\partial x nikaal rahe ho, to yy ko 77 ya π\pi jaisa constant samjho. Jis term mein xx hai hi nahi (jaise 3y3y), uska xx-direction mein slope zero hoga, to woh gayab ho jaayega. Yeh common galti hoti hai — log yy ko bhi differentiate karne lagte hain, jo galat hai.

ML mein yeh kyun important hai? Kyunki model ko train karne ke liye humein pata karna hota hai ki har weight ko badalne se loss kitna change hota hai. Example 3 dekho: L=(wx+bt)2L=(wx+b-t)^2 ka L/w=2(wx+bt)x\partial L/\partial w = 2(wx+b-t)x — yehi number gradient descent ko batata hai ki ww ko kis direction mein push karna hai. Saare partials ko ek vector mein rakh do to woh ban jaata hai gradient f\nabla f, aur uske ulte direction mein chalke hum loss minimize karte hain. Yahi poori deep learning training ki jaan hai, bhai.

Go deeper — visual, from zero

Test yourself — Calculus & Optimization Basics

Connections