Runoob Tutorial ArchiveEnglish

Functions and Images

Functions and Images

In this chapter, we will intuitively display the graphs of common functions. Understanding the shape of the graph is more important than memorizing the formula.

Loss function surfaces and activation functions are essentially variations of these basic shapes. Each function is equipped with a switchable interactive graph. Interacting with and dragging it is much more effective than looking at static illustrations.


Why study "visualizing" specifically?

Mathematical formulas describe the "rules," while images describe "what the rules look like."

Many core problems in deep learning are essentially "curve or surface shape" problems:

Deep learning problems The inherent shape problem
Why some loss functions are easier to optimize Look at whether its surface is "bowl-shaped" (convex function)
Why sigmoid causes training to slow down Look at how "flat" its image is at the ends
Why ReLU became the default choice Look at how "simple" its image is

Looking at each function with the questions "what shape is it, what are the ends like, what is the middle like" is more memorable than just memorizing formulas.


Five basic function graphs

Linear function: y = kx + b

The shape is a straight line. k is the slope, which determines the degree and direction of the incline (k>0 goes up and to the right, k<0 goes down and to the right); b is the intercept, which determines the point where the line intersects the y-axis.

The domain and range are both all real numbers, and the rate of change (slope) is the same everywhere, which is the meaning of the word "linear."

Role in AIThe most basic operation of a neuron is a linear function in a high-dimensional space. Weights control the direction and degree of inclination, and bias controls the overall translation.

Quadratic function: y = ax² + bx + c

The shape is a parabola. Opening upwards (bowl-shaped), it has a minimum value; opening downwards (inverted bowl), it has a maximum value.

The vertex is the turning point of the curve and can be found using the method of completing the square; the graph is symmetric about the vertical line passing through the vertex.

Role in AI: The simplest convex function looks like this. Mean Squared Error (MSE) loss in many simplified scenarios is close to this "bowl-shaped" surface--gradient descent easily finds the minimum point on a bowl-shaped surface because no matter which direction you go down, you will head towards the same bottom.

Exponential function: y = aˣ (a>0 and a≠1)

The shape is a curve that is constantly accelerating upwards (when a>1) or constantly decaying (when 0<a<1), and never touches the x-axis; the x-axis is its horizontal asymptote.

The domain is all real numbers, and the range is . Input increases linearly, and the output increases "explosively"; this is the most core property of the exponential function.

Role in AI: The exponential decay in softmax and the momentum term of the Adam optimizer both utilize the properties of "amplifying differences" or "gradual forgetting."

Logarithmic function: y = logₐx (a>0 and a≠1)

Logarithmic and exponential functions are a perfect "mirror" relationship (with the line y=x as the axis of symmetry). The curve rises rapidly starting from x=0, and then the growth slows down.

The domain is ,and the range is all real numbers. The larger the input, the slower the growth, which is completely opposite to the exponential function.

Role in AI: In cross-entropy loss, the use of the logarithm's shape—"extremely steep near 0 and tending to be flat near 1"—is utilized; the more confident the model is while being wrong, the heavier the penalty.

Trigonometric functions: y = sin(x), y = cos(x)

The shape is a wave, constantly repeating (periodicity), and always oscillating between -1 and 1 (boundedness). sin(x) starts from the origin and goes up, while cos(x) reaches its maximum value of 1 at x=0.

The period is ,and the range is .

Role in AI: Transformer's positional encoding directly overlays sin/cos waveforms of different frequencies to generate a unique and regular "coordinate" for each position in the sequence (detailed in Chapter 3).

Interactive Demo: Gallery of Five Basic Function Graphs

Clicking the button switches functions, and the information card below synchronously updates the shape features and AI role.


Three Key AI Activation Functions

Activation functions are the "source of nonlinearity" for each layer in a neural network; their shape directly determines the difficulty of training.

sigmoid: $\sigma(x) = 1 / (1 + e^{-x})$

The shape is an S-shaped curve, slowly rising from 0 to 1, rising fastest in the middle (near x=0), and tending to be flat at the ends.

The range is an open interval and is often interpreted as "probability".

The biggest side effect: the slope at both ends approaches 0.When the absolute value of the input is large (e.g., x=10 or x=-10), the sigmoid curve is almost flat, and the gradient is almost zero.

In deep networks, if many layers use sigmoid, the gradient is weakened layer by layer by these "flat regions" during backpropagation, and eventually cannot reach the earlier layers--this isthe vanishing gradient problem.The most intuitive geometric source.

ReLU: f(x) = max(0, x)

The shape is a piecewise linear function: it lies on the x-axis when x<0 (output is always 0), and it is a line with a slope of 1 when x≥0.

The derivative is always 0 for the negative half-axis and always 1 for the positive half-axis, with no "gradual flattening" process like sigmoid, making the computation extremely simple (only one comparison is needed).

The gradient for the positive half-axis is always 1 and does not decay as the input increases, greatly alleviating the vanishing gradient problem, which is the intuitive reason why ReLU became the default activation function.

It also has its own problem: the gradient for the negative half-axis is always 0, which can cause some neurons to "die" and stop updating—this can also be seen directly from the image shape.

tanh: f(x) = (eˣ - e⁻ˣ) / (eˣ + e⁻ˣ)

It is an S-shaped curve similar to sigmoid, but centered at the origin with a range of [-1, 1].

Compared to sigmoid, tanh's output is centered around 0. If the mean of the input to the next layer is close to 0, training is often more stable, which is why tanh is preferred in certain scenarios.

However, it does not solve the fundamental problem of "flattening at the ends," which is the background for the later popularity of the ReLU family of functions.

Interactive Demonstration: Activation Functions and Their Derivatives

Switch the activation function; the solid line is the function itself, and the dashed line is its derivative (slope). Pay attention to the peaks and the decay at the ends of the derivative curve.

The maximum value of the sigmoid derivative is only 0.25, and it is almost 0 when |x| > 4—after multiple layers of multiplication, the gradient rapidly disappears, which is the geometric source of "vanishing gradient."


Why is cross-entropy used as -log(p)?

In a binary classification problem, if the model predicts the probability of the "correct class" as p, the cross-entropy loss is p.

Intuitively, you might think: why not use p when the loss is simpler? Placing the two curves together immediately shows the difference.

Interactive demonstration: Comparison of the penalty of -log(p) and 1-p.

Drag the slider to change the model's predicted probability p and observe the difference in penalties given by the two losses. When p is very small (the model is confidently wrong), the penalty of -log(p) is much greater than 1-p.

Blue is -log(p), green is 1-p. The former tends to infinity as p → 0, while the latter is at most 1.

Example

Calculate it manually: Gradient vanishing and the -log(p) penalty

import

math

---- Part 1: Sigmoid derivative decays as |x| increases ----

def

sigmoid

(

x

)

:

   

return

1

/

(

1

math

.

exp

(

-x

)

)

def

sigmoid_grad

(

x

)

:

s

=

sigmoid

(

x

)

   

return

s *

(

1

)

   

derivative formula of sigmoid

print

(

sigmoid_grad

(

0

)

)

   

At the peak: 0.25, which is the maximum value of the sigmoid derivative

print

(

sigmoid_grad

(

5

)

)

   

|x|=5: less than 0.007 remains

print

(

sigmoid_grad

(

10

)

)

 

When |x|=10: approximately 0.0000456, the gradient almost vanishes

---- Part Two: Penalty Growth of -log(p) ----

for

p

in

[

1.0

,

0.5

,

0.1

,

0.01

]

:

   

print

(

p

,

math

.

log

(

p

)

)

 

The smaller p is, the steeper the penalty; when p=0.01, the penalty reaches 4.6.

The output of the above code is:

0.25

0.006648056670790155

4.5395807735957664e-05

1.0 0.0

0.5 0.6931471805599453

0.1 2.302585092994046

0.01 4.605170185988091

Two numbers are worth remembering: the peak value of the sigmoid derivative is only 0.25; when p decreases from 0.5 to 0.01, the penalty of [n] increases from 0.69 to 4.6.


Three scenarios to use "shape intuition"

Scenario What shape to look at Basis for judgment
Judging whether the loss function is easy to optimize Whether the surface is close to "bowl-shaped" (convex) The closer it is to bowl-shaped, the easier gradient descent is to find the global minimum; bumpy (non-convex) may get stuck in a local optimum
Judging whether the activation function is slowing down training Whether the image ends are "flattened" The flatter, the more obvious the gradient vanishing problem
Whether the judgment function is suitable for probability output Whether the value range is bounded The value range must be compressed into the (0,1) interval to be interpreted as a probability, which is the direct reason for choosing sigmoid and softmax

ExerciseWithout looking at the formulas, judge by intuition--ReLU or sigmoid, which is more suitable for the last layer of the network to perform "binary classification probability output"? Why?

Click to view the answer

sigmoid is more suitable because its range is, which can naturally be interpreted as "the probability of belonging to a certain class."

The range of ReLU is, which has no upper bound and cannot be used directly as a probability.


Summary of this chapter

function shape keyword corresponding AI concept
Linear function A straight line with a constant slope everywhere Weighted sum of neurons wx+b
Quadratic function Bowl-shaped / inverted bowl-shaped Loss function surface (convex function intuition)
Exponential function Accelerating rise or decay softmax, momentum term
logarithm function growth is getting slower cross-entropy loss
trigonometric function periodic wave positional encoding
sigmoid S-shape, flat at both ends Probability output, source of gradient vanishing
ReLU Piecewise, zeroing out the negative half-axis Default activation function
tanh S-shape, centered at 0 Scenarios requiring "zero-mean output"