Runoob Tutorial ArchiveEnglish

Gradient — points in the direction where the function increases the fastest

Gradient -- points in the direction where the function increases the fastest

The gradient points in the direction where the function value increases the fastest, so gradient descent moves in the negative gradient direction.

The gradient is a core concept in the entire AI training process—each step update of the optimizer depends on the gradient.


Concept analysis

Gradient = a vector composed of partial derivatives

[ \nabla f(x, y) = \left( \frac{\partial f}{\partial x}, \frac{\partial f}{\partial y} \right) ]

** Gradient direction

Points in the direction of the fastest ascent

Want to go up the mountain $\rightarrow$ follow the gradient

** Negative gradient direction

Points in the direction of the fastest descent

Want to go down the mountain $\rightarrow$ follow the negative gradient

** Gradient magnitude

The steepness of that direction

Gradient is zero $\rightarrow$ extremum or saddle point

Changing the negative sign in the formula \( \theta = \theta - \eta \nabla J \) means "walking in the opposite direction (downhill)".

If you forget the negative sign, it becomes gradient ascent, and the loss will keep increasing.


Real-life example

**Descending a mountain in heavy fog

You are standing on a mountain covered in thick fog. To get down as fast as possible, you feel which direction under your feet is the steepest and take a step in that steepest downhill direction.

Repeatedly: feel $\rightarrow$ step $\rightarrow$ feel $\rightarrow$ step. This is the physical intuition of gradient descent.

The gradient tells you the "steepest direction," and the negative sign tells you to "go down."


Mathematical definition

[ \nabla f(\mathbf{x}) = \begin{bmatrix} \frac{\partial f}{\partial x_1} \ \vdots \ \frac{\partial f}{\partial x_n} \end{bmatrix} ]

Directional derivative along direction u: ( \nabla f \cdot \mathbf{u} = |\nabla f| \cos\theta ). It is maximum when u is in the same direction as the gradient.


Python hands-on practice

Example

import

numpy

as

np

def

f

(

x

,

y

)

:

   

return

x**

2

2

 

Bowl-shaped function

def

grad

(

x

,

y

)

:

   

return

np.

array

(

[

2

*x

,

2

*y

]

)

pt

=

np.

array

(

[

1.5

,

1.0

]

)

g

=

grad

(

*pt

)

print

(

f

"In (1.5, 1.0): gradient = {g}"

)

print

(

f

"Gradient norm = {np.linalg.norm(g):.2f}"

)

print

(

f

"The gradient points away from the origin (ascent), and the negative gradient points towards the origin (descent)."

)

3D Loss Surface Interactive Visualization

Below, we plot the 3D surface of ( f(x,y)=x^2+y^2 ) using Plotly.js to intuitively feel the gradient direction:


Application Scenarios in AI

Core Inputs for Gradient Descent Training

Every parameter update ($\theta = \theta - \eta \nabla_\theta J$) depends on the gradient. The gradient tells each parameter "whether to increase or decrease, and by how much." In PyTorch, loss.backward() calculates the gradient, and optimizer.step() updates the parameters using the gradient.

Vanishing and Exploding Gradients

In deep networks, during backpropagation, the gradient may become progressively smaller (vanish, causing shallow layers to learn nothing) or progressively larger (explode, causing parameter updates to be too large and diverge). ResNet's residual connections (identity shortcut) are designed to alleviate vanishing gradients—providing a "highway" for the gradient to pass directly to the shallow layers.

Gradient Clipping

When training RNNs and large language models, gradients are often clipped: if the L2 norm of the gradient exceeds a threshold, it is scaled down to the threshold size. This is a practical technique to prevent gradient explosion; in GPT training, the standard practice is clip_grad_norm_=1.0.

Practical Significance of Gradient Visualization

Tools like TensorBoard and W&B can visualize the distribution of gradients in each layer. If the gradient of a certain layer is almost zero $\rightarrow$ vanishing gradient; if the gradient norm is over several hundred $\rightarrow$ exploding gradient. This is firsthand information for debugging the deep learning model training process.