Functions and graphs: linear, quadratic, exponential and logarithmic
A function is a rule that turns a number into a number, and a graph is that rule drawn as a picture. If you have not done math in a long time, start here. Move a slider and the graph changes right away. The formulas in calculus and AI are all built by chaining these few functions together.
Short answer. A function is "a rule that takes one input and gives back one output," and a graph is that rule drawn as a picture (across = input, up = output). A linear function is a straight line, and a quadratic function is a bowl. An exponential function grows faster and faster, and a logarithm does the opposite: it grows slower and slower.
Pick a kind of function and change its numbers with the sliders or the number boxes. Hover over the graph (on a phone, drag a finger left and right) or type a number in the x box to see (x, y) at that spot. The green line is the graph and the orange dot is the value at the current x. The gray dashed lines show how to read the graph, and the light orange area is where there is no value.
Definitions
The text below explains with a vending machine and graphs. Textbooks define the ideas as follows.
- function
- For two sets X and Y, a rule f that pairs each element x of X with exactly one element of Y is a function from X to Y. The element paired with x is written f(x).
- domain, codomain, range
- For a function f : X → Y, X is the domain, Y is the codomain, and the set of all values f(x) is the range. The range is a subset of the codomain.
- linear function, quadratic function
- f is a linear function when f(x) is a first-degree expression in x, and a quadratic function when it is a second-degree expression. In a linear function, a is the slope and b is the y-intercept. A quadratic function can be rewritten in a form whose vertex is (h, k).
- exponential function
- For a constant b with b > 0 and b ≠ 1, the function below is the exponential function with base b. Its domain is all real numbers, and its range is all positive real numbers.
- logarithm, logarithmic function
- For b > 0, b ≠ 1, and x > 0, the number y with by = x is the logarithm of x with base b, written logb x. The function f(x) = logb x is a logarithmic function, and it is the inverse of the exponential function bx. The logarithm with base e is the natural logarithm, written ln x.
- inverse function
- When a function f : X → Y is a one-to-one correspondence (different x give different f(x), and the range is all of Y), the function that sends each y in Y to the x in X with f(x) = y is the inverse function of f, written f−1.
- composite function
- For two functions g : X → Y and f : Y → Z, the function that sends each x in X to f(g(x)) is the composite function of g and f, written f ∘ g.
1. A function: put one in, get one out
A function is like a vending machine. Press a button (the input) and one drink (the output) comes out. Press the same button and you always get the same drink. A math function works the same way: put in one number and one number comes out, and the same number in always gives the same answer.
In code it looks like this. It does not read or change anything outside. It decides the answer only from the value it gets (a pure function).
function f(x) {
return 2 * x + 1;
}
f(3); // 7
In math we write this as f(x) = 2x + 1. f(x) means "the value you get when you put x into f." It does not mean f times x. f(3) = 2 × 3 + 1 = 7. We often call the output y and write y = 2x + 1. f is just a name, so g or h works too.
There must be exactly one output. A rule that gives back "the numbers whose square is 4" is not a function, because putting in 4 gives two answers, 2 and −2. The other way around is fine: different inputs may give the same output. x² gives 4 for both 2 and −2.
What can go in and what can come out. Each function has a range of numbers you are allowed to put in. This is called the domain. The range of values that can come out is called the range. Where you cannot put a number in, we say the function is "undefined" there. In the experiment, those places are shaded light orange.
| Function | x you can put in (domain) | y that can come out (range) |
|---|---|---|
2x + 1 | any number | any number |
x² | any number | 0 or more |
2ˣ | any number | above 0 |
log₂ x | above 0 | any number |
1 ÷ x | any number except 0 | any number except 0 |
√x (= x^0.5) | 0 or more | 0 or more |
| Sigmoid | any number | between 0 and 1 (never exactly 0 or 1) |
| ReLU | any number | 0 or more |
In programming terms, the domain is the values a parameter can accept, and the range is the values the function can return. JavaScript's Math.log(-1) returns NaN because −1 is outside the domain.
2. Reading a graph: across is what goes in, up is what comes out
A graph is every (input, output) pair a function makes, drawn as dots and joined up. The position across is the x you put in, and the height is the y that comes out. The dot (3, 7) means "put in 3 and you get 7."
Work it by hand: f(x) = 2x + 1.
| x | Work | f(x) | Dot on the graph |
|---|---|---|---|
| −1 | 2 × (−1) + 1 | −1 | (−1, −1) |
| 0 | 2 × 0 + 1 | 1 | (0, 1) |
| 1 | 2 × 1 + 1 | 3 | (1, 3) |
| 2 | 2 × 2 + 1 | 5 | (2, 5) |
The four dots sit on one line. Fill in every x in between too (0.5, 1.7, and so on), and the dots become a solid straight line. That is the graph of 2x + 1.
To read a value off a graph, start at x on the horizontal axis, go up or down until you hit the curve, then go sideways and read the number on the vertical axis. The gray dashed lines in the experiment show this path. To go backward ("which x gives y = 5?"), start at 5 on the vertical axis, go sideways to the curve, and read where you are across.
The vertical line test. If a vertical line crosses a graph two or more times anywhere, it is not the graph of a function, because one x would have two y values. A circle meets a vertical line twice, so it is not the graph of a function.
Try this. ① As it starts (linear, a = 2, b = 1), type −1, 0, 1 and 2 in the x box. You get −1, 1, 3 and 5, just like the table. ② Move the mouse left and right over the graph. The dot follows and shows (x, y). Every time x goes up by 1, y goes up by 2.
3. What the numbers do: shift, stretch, flip
Change the numbers in a formula (the coefficients) and the graph moves. The rules are the same for every kind of function. Change something outside the function and the graph moves up and down. Change something right next to x (inside) and it moves left and right.
| What you change | What it does | Example |
|---|---|---|
Add outside f(x) + k | Moves it up and down. Positive k moves it up | x² + 2: the bottom moves up to 2 |
Subtract inside f(x − h) | Moves it left and right. Positive h moves it right | (x − 2)²: the bottom moves to x = 2 |
Multiply outside a·f(x) | Stretches it taller (a > 1) or squashes it (0 < a < 1) | 3x²: a narrow, steep bowl |
Multiply outside by a negative −f(x) | Flips it upside down | −x²: an upside-down bowl |
Multiply inside f(b·x) | Squeezes it sideways (b > 1) or spreads it (0 < b < 1) | sin(2x): the wave is twice as tight |
Negative inside f(−x) | Flips it left to right | 2^(−x) = 0.5ˣ: growth turns into shrinking |
The confusing one is "it says x − 2, so why does it move right?" (x − 2)² is 0 when x is 2. The bottom that used to be at x = 0 is now at x = 2. To get the same value, x has to go 2 further, so the graph moves right. To see it step by step while following one point, read 4. How the formula is built just below.
In a linear function a·x + b, a is the slope. When x goes up by 1, y changes by a. If a is negative the line goes down as you move right, and if a is 0 it is flat. b is the value at x = 0, which is the height where the line crosses the vertical axis.
Try this. ① In linear, set a to −1. The line goes downhill, and the value at x = 1 is 0. Set a to 0 and you get a flat line at height 1. ② Pick quadratic and move h to 2. The bowl moves 2 to the right, and the formula becomes f(x) = 1·(x − 2)² + 0. Set a to −1 and the bowl turns upside down. ③ Pick sine and set b to 2. The wave gets twice as tight. One repeat (2π ÷ b, from the note next to b) shrinks from about 6.28 long to 3.14.
4. How the formula is built: y = a·f(x − h) + k
The table above only lists the rules as results. Here we start from the base graph y = x² and add one piece at a time until we have y = a·(x − h)² + k. Follow one point on the graph (P), and you can see where each piece sends the graph. Switch the base to |x| or sin x and the rules stay the same.
Press "Next" to go one step at a time. A piece is added to the formula at the top (the orange background marks the new piece), the graph moves or stretches by that much, and the reason appears below. The green line is the current formula and the gray dashed line is the one before it (the base graph at the last step). The orange dot P is the point we follow, and the orange arrow is the path P took in this step. The h, a and k sliders open at the step where their piece is added.
Written only as formulas, the steps above look like this.
- Base:
y = x². A bowl with its bottom at (0, 0). The point we follow, P, is at x = 1 with height 1, so P = (1, 1). - Subtract h inside:
y = (x − h)². With h = 2, x = 3 gives(3 − 2)² = 1. The height 1 that x = 1 gave on the base graph now comes from x = 3. What gets squared is x − 2, not x, so to get the same height as before, x has to be 2 bigger. That is why subtracting 2 moves the graph 2 to the right, and P goes to (3, 1). Why this piece: it moves the graph sideways. To move it sideways you have to change the x that goes in, not the height that comes out. - Multiply by a outside:
y = a·(x − h)². With a = 2, every height doubles. Nothing moves across, so P goes up from (3, 1) to (3, 2). The bottom has height 0, and twice 0 is still 0, so it stays at (2, 0). A negative a flips the graph upside down. Why this piece: it decides how narrow the bowl is and whether it opens up or down. - Add k outside:
y = a·(x − h)² + k. With k = −3, every height loses 3, so the whole graph moves down by 3. P goes down from (3, 2) to (3, −1), and the bottom lands on (2, −3). Why this piece: it decides how high the bottom is. It has to come after multiplying by a. In the other order,a·((x − h)² + k), k gets multiplied by a too, and the bottom ends up at height a·k = −6. - Done:
y = a·(x − h)² + k, with the vertex at (h, k). Check by hand:2·(3 − 2)² − 3 = −1, so P is at (3, −1). Every point (p, q) of the base graph goes to (p + h, a·q + k). Put any function f in place of x² and this rule stays the same, so we writey = a·f(x − h) + k.
Multiplying inside, f(b·x), squeezes the graph sideways. The height that x = c gave on the base graph now comes from x = c ÷ b, so b = 2 makes it half as wide. This section does not go into it.
Try this. ① Press "Next" four times. P goes (1, 1) → (3, 1) → (3, 2) → (3, −1), and the formula becomes y = 2·(x − 2)² − 3. ② Press "Start over", go one step, and set h to −1. The graph moves 1 to the left, the formula is y = (x + 1)², and P is (0, 1). ③ Put h back to 2, go one more step, and set a to −1. The bowl turns upside down and P drops to (3, −1). ④ Put a back to 2, switch the base to sin x, and go to the last step. P goes from (1.57, 1) to (3.57, −1), and the base point (0, 0) lands on (2, −3).
5. Linear and quadratic: a straight line and a bowl
A linear function a·x + b is a straight line. With every equal step (x goes up by 1), it adds the same amount (a). Saving the same amount of money every day gives a total that looks like this.
A quadratic function has x² in it, x multiplied by itself. Its graph is a bowl, which textbooks call a parabola. Written as a·(x − h)² + k, you can read the bottom point (the vertex) straight off: it is (h, k) (see how it is built). (x − h)² can never go below 0, so when a is positive the lowest value is k, at x = h. If you multiply out the brackets you get the form a·x² + b·x + c, which is the same kind of function.
Work it by hand: f(x) = (x − 1)² − 4.
- Read the bottom. h = 1 and k = −4, so the bottom is (1, −4).
f(1) = 0² − 4 = −4. - Put in a few values.
f(3) = 2² − 4 = 0,f(−1) = (−2)² − 4 = 0,f(0) = (−1)² − 4 = −3. - Check that both sides match. 2 to the right of the bottom (x = 3) and 2 to the left (x = −1) both give 0. A bowl is the same on both sides of its bottom.
- Find where it crosses the x axis. f(x) = 0 at x = −1 and x = 3. These are the answers to the equation
(x − 1)² = 4.
Written only as formulas, the same work looks like this.
Try this. In quadratic, set a to 1, h to 1 and k to −4, then type 3, −1 and 0 in the x box. You get 0, 0 and −3. Type 1 to land on the bottom, (1, −4).
6. Exponential functions: the same multiplier at every step
A linear function adds the same amount at every step. An exponential function multiplies by the same number at every step. 2ˣ doubles every time x goes up by 1. The number being multiplied again and again (2 here) is the base, and the small raised x is the exponent. 2ˣ means "start at 1 and multiply by 2, x times."
| x | −1 | 0 | 1 | 2 | 3 | 10 |
|---|---|---|---|---|---|---|
| 2ˣ | 0.5 | 1 | 2 | 4 | 8 | 1024 |
One step to the left halves the value. That is why 2⁰ = 1 (no multiplying at all) and 2⁻¹ = 0.5 (divided once). However small x gets, the value only keeps halving. It never reaches 0 or goes negative, so 2ˣ is always above 0.
At first it looks slower than a linear function, but soon it passes everything. Saving the same amount every day is linear. Compound interest, where interest earns more interest, is exponential. When the base is below 1, it shrinks by the same ratio instead. 0.5ˣ halves at every step.
Exponent rules. An exponent counts how many times you multiplied, so multiplying turns into adding exponents.
| Rule | In plain words | Example |
|---|---|---|
bᵐ × bⁿ = bᵐ⁺ⁿ | Multiplying adds the counts | 2³ × 2⁴ = 2⁷ = 128 |
bᵐ ÷ bⁿ = bᵐ⁻ⁿ | Dividing subtracts the counts | 2⁵ ÷ 2² = 2³ = 8 |
(bᵐ)ⁿ = b^(m × n) | Repeating multiplies the counts | (2³)² = 2⁶ = 64 |
b⁰ = 1 | Multiplying zero times leaves 1 | 2⁰ = 1 |
b⁻ⁿ = 1 ÷ bⁿ | A negative count means dividing | 2⁻³ = 1 ÷ 8 = 0.125 |
b^(1/2) = √b | Half a time is the square root | 4^(1/2) = 2 |
Work it by hand: 2³ × 2⁴. 2³ is 2 × 2 × 2, and 2⁴ is 2 × 2 × 2 × 2. Multiply them and you have 3 + 4 = 7 twos multiplied together. So the answer is 2⁷ = 128. Check: 8 × 16 = 128.
Try this. Pick exponential in the first experiment: a = 1 and b = 2. Type 3 in the x box to get 8, and −1 to get 0.5. Lower b to 0.5 and the curve shrinks as you go right; at x = 3 it shows 0.13 (exactly 0.125). Set b to 1 and multiplying any number of times leaves 1, so the line is flat. Put b back to 2 and set a to −1: the curve flips below the x axis, and at x = 2 it is −4.
The number e: interest that never stops
Say a bank pays 100% interest on 1 dollar after one year. After a year you have 2. If instead it adds 50% every half year, twice, you get 1.5 × 1.5 = 2.25. That is a little more, because the first interest also earns interest. Split the year into n parts and you get (1 + 1/n)ⁿ.
| Times per year n | 1 | 2 | 4 | 12 (monthly) | 365 (daily) | 1,000,000 |
|---|---|---|---|---|---|---|
| After one year | 2 | 2.25 | 2.4414 | 2.613 | 2.7146 | 2.71828 |
Splitting more often gives more, but not without end: the result gets closer and closer to 2.71828…. That number is e. It is what 1 becomes after a year when interest is added all the time, without stopping. Numbers that get closer and closer to a value are the subject of the next topic, limits.
eˣ grows at a speed equal to its current size. That is why its derivative is itself (see the table on the derivatives page). Because of this, e shows up all over calculus and AI.
Where bases 2, e and 10 are used
| Base | Where | Why |
|---|---|---|
| 2 | Computers, algorithms, information (bits) | Things are on or off. It counts how many times you halve |
| e (≈ 2.718) | Calculus, probability, AI (sigmoid, softmax, loss) | Its derivative is itself, which keeps formulas simplest |
| 10 | Digits, writing big numbers (1.27 × 10³⁰) | People count in tens |
Logarithms are named by their base too. The logarithm with base e is the natural logarithm, written ln x. Plain log x means different bases in different fields. JavaScript's Math.log and Python's math.log use base e. Base 2 is Math.log2 and base 10 is Math.log10.
7. Logarithms: asking "how many times?" backward
An exponent answers "what do I get if I multiply by 2 three times?" (8). A logarithm asks the reverse: "how many times do I multiply by 2 to reach 8?" (3). We write log₂ 8 = 3 and say "log base 2 of 8."
bʸ = x and log_b x = y say the same fact from two sides, like 2³ = 8 and log₂ 8 = 3.
Work it by hand.
log₂ 8: divide 8 by 2 to get 4, again to get 2, again to get 1. Three divisions reach 1, so it is 3.log₂ 1024: the same way, ten divisions reach 1. It is 10.log₁₀ 1000:1000 = 10 × 10 × 10, so it is 3. The base 10 logarithm is roughly "number of digits − 1."log₂ 1 = 0(no multiplying),log₂ 0.5 = −1(divided once).
Why 0 and negative numbers have no logarithm. Multiply by 2 any number of times, even a negative number of times or half a time, and the result is always above 0. So "how many times do I multiply by 2 to reach 0?" or "to reach −4?" has no answer. The domain of a logarithm is only the numbers above 0. That is why the left half of the logarithm graph in the first experiment is empty.
Logarithm rules. These are the exponent rules read backward. A logarithm turns multiplying into adding.
| Rule | In plain words | Example |
|---|---|---|
log(a × b) = log a + log b | A product becomes a sum | log₂(8 × 4) = 3 + 2 = 5 |
log(a ÷ b) = log a − log b | Dividing becomes subtracting | log₂(8 ÷ 2) = 3 − 1 = 2 |
log(aⁿ) = n × log a | A power becomes multiplying | log₂(8²) = 2 × 3 = 6 |
log_b 1 = 0, log_b b = 1 | 1 is zero multiplications, b is one | log₂ 2 = 1 |
log_b x = ln x ÷ ln b | Changing the base | log₂ 10 = 2.303 ÷ 0.693 ≈ 3.32 |
Work it by hand: log₂(8 × 4). 8 = 2³ and 4 = 2², so 8 × 4 = 2³ × 2² = 2⁵ = 32 (exponent rule: multiplying adds the counts). So log₂ 32 = 5 = log₂ 8 + log₂ 4.
Multiply many probabilities together and you get a number so small that a computer rounds it to 0. Take logarithms first and the product becomes a sum, so that does not happen. This is one reason AI losses such as cross-entropy use logarithms.
Drag point A or B, or type a in the box. The green curve is y = 2ˣ, the blue curve is y = log₂ x, and the black dashed line is y = x. The orange dashed line joins A and B, and its middle point always sits on y = x.
Try this. ① At the start, A = (2, 4) and B = (4, 2). 2² = 4 and log₂ 4 = 2. ② Drag A to x = 0: A = (0, 1) and B = (1, 0). 2⁰ = 1 and log₂ 1 = 0. ③ Type −1 in the a box: B = (0.5, −1), so log₂ 0.5 = −1. ④ Move the scale slider to 10. Doubling 1 ten times gives 2¹⁰ = 1024, and backward, log₂ 1024 = 10. The top scale has equal steps from 0 to 10, while the bottom value at the same spot grows from 1 to 1024.
8. Inverse functions: going backward
If a function f sends x to y, the function that sends y back to x is the inverse of f, written f⁻¹. Do one after the other and you end up where you started: f⁻¹(f(x)) = x.
Example: f(x) = 2x + 1 means "multiply by 2, then add 1." To go backward, undo the last step first: subtract 1, then divide by 2. f⁻¹(y) = (y − 1) ÷ 2. f(3) = 7, and f⁻¹(7) = (7 − 1) ÷ 2 = 3.
The graph is a mirror image across the line y = x. If the graph of f has the dot (a, b), it means "put in a, get b." The inverse means "put in b, get a," so it goes through (b, a). Swapping across and up is the same as folding the paper along y = x. 2ˣ and log₂ x in the experiment above are like this.
For an inverse to exist, different inputs must give different outputs. x² gives 4 for both 2 and −2, so if you get 4 you cannot tell where to send it back. Only after you limit the inputs to 0 or more do you get the inverse √x.
Easy mistake: the −1 in f⁻¹(x) just means "backward." It is not 1 ÷ f(x).
| Function | Inverse |
|---|---|
2x + 1 | (x − 1) ÷ 2 |
x² (x ≥ 0 only) | √x |
2ˣ | log₂ x |
eˣ | ln x |
10ˣ | log₁₀ x |
Sigmoid 1 ÷ (1 + e^(−x)) | Logit ln(p ÷ (1 − p)). See the logistic regression page |
9. How fast do they grow?
Put the same x into different functions and the outputs can be wildly different. They may look alike at first, but as x gets bigger the gap gets out of hand.
Use the "x up to" slider to widen the range across. The five lines differ by line style (solid, dashed) as well as color. Their names are in the top left. Turn on "Log scale on the vertical axis" and each equal step up on the vertical axis means multiplying by the same number (10 times, 10,000 times, and so on).
Try this. ① At the start (x up to 4), at x = 4: log₂ x = 2, x = 4, x·log₂ x = 8, x² = 16 and 2ˣ = 16. x² and 2ˣ meet at x = 2 and at x = 4 (4 and 16). ② Widen to 8: x² = 64 and 2ˣ = 256, four times apart. At 16 it is 256 against 65,536, 256 times apart. ③ Widen to 64: 2ˣ = 1.84 × 10¹⁹, and on the ordinary scale the other four all look flat along the bottom. ④ Turn on the log scale: 2ˣ becomes a straight line, and the four that were stuck at the bottom spread apart. log₂ x is still only 6 at x = 64.
Why 2ˣ is a straight line on a log scale. A log scale places each value at the height of its logarithm instead of the value itself. log(2ˣ) = x × log 2, which is a linear function of x, so it is a straight line. Turn that around: data that looks like a straight line on a log scale is growing exponentially.
| n | log₂ n | n | n·log₂ n | n² | 2ⁿ |
|---|---|---|---|---|---|
| 10 | 3.32 | 10 | 33 | 100 | 1024 |
| 100 | 6.64 | 100 | 664 | 10,000 | about 1.27 × 10³⁰ |
| 1000 | 9.97 | 1000 | 9,966 | 1,000,000 | about 1.07 × 10³⁰¹ |
| 1,000,000 | 19.93 | 1,000,000 | about 1.99 × 10⁷ | 10¹² | a number with 301,030 digits |
Connecting to algorithm speed, O(n)
Big O notation treats the amount of work a program does as a function of the input size n, and only writes down which function it grows like when n is very large. It drops the coefficients and the smaller terms. A program that does 3n² + 5n steps is O(n²), because when n is large, n² swamps everything else. The order of the columns in the table above, left to right, is the order of big O.
| Notation | Example | When n = 1,000,000 |
|---|---|---|
O(log n) | Find an item in a sorted list by cutting it in half each time (binary search) | about 20 steps |
O(n) | Look through a list once from start to end | a million steps |
O(n log n) | A good sort, such as merge sort | about 20 million steps |
O(n²) | Compare every pair | a trillion steps |
O(2ⁿ) | Try every subset | never finishes |
This is where "a logarithm counts how many times you halve" pays off. Halve a million items 20 times and one is left (2²⁰ is about a million). That is why binary search finishes in about 20 steps even with a million items.
10. Chaining functions: composition
Feed the output of one function into another and you get a new function. This is called composition, written f(g(x)). Work from the inside out: g first, then f. Textbooks also write (f ∘ g)(x). It is exactly f(g(x)) in code, and the same idea as the shell pipe g | f.
Work it by hand: g(x) = x + 3, f(x) = x², x = 2.
- Put 2 into g.
g(2) = 2 + 3 = 5. Call this middle value u. - Put 5 into f.
f(5) = 5² = 25. Sof(g(2)) = 25. - Write it as a formula.
f(g(x)) = (x + 3)². - Swap the order.
f(2) = 4, theng(4) = 4 + 3 = 7.g(f(x)) = x² + 3, and the answer is 7.
Order matters. "Add 3, then square" and "square, then add 3" are different jobs, the same way socks then shoes is different from shoes then socks.
Input
Pick g and f and move x. x goes through the green box g first, and the middle value u that comes out goes into the blue box f. The second line of the explanation shows the values with the order swapped. σ is the sigmoid.
Try this. ① At the start (g = x + 3, f = x², x = 2), u = 5 and y = 25. Press "Swap the order" and you get u = 4 and y = 7. ② Pick log₂ x for g and 2ˣ for f. Any x above 0 comes out unchanged: x = 4 gives u = 2 and y = 4. Chaining a function with its inverse brings you back where you started. ③ With g = x + 3 and f = log₂ x, move x to −4. u = −1, which log₂ cannot take, so there is no value. ④ Set both g and f to 2·x, and swapping the order gives the same answer.
This leads into derivatives. The slope of a chained function is the slopes of the steps multiplied together. That is the chain rule. A neural network is a very long chain of functions, one per layer, so neural network training (backpropagation) runs on the chain rule.
11. Functions you meet in AI
The formulas in AI models are, in the end, the functions on this page chained together.
| Function | Formula | What it does | More |
|---|---|---|---|
| Linear (multiply by weights and add) | w·x + b | Gives the input a score | Neural networks |
| Sigmoid | 1 ÷ (1 + e^(−x)) | Turns any score into a probability between 0 and 1. σ(0) = 0.5, σ(2) = 0.881 | Logistic regression |
| ReLU | max(0, x) | Turns negative numbers into 0 and leaves positive numbers alone. Goes between the layers of a neural network | Neural networks |
| Softmax | e^(zᵢ) ÷ (sum of all e^(z)) | Turns several scores into probabilities that add up to 1 | Softmax and sampling |
| Logarithm | −log p | Punishes hard when the probability p given to the right answer is small (a loss) | Cross-entropy |
Why a bent function is needed. However many linear functions you chain, the result is still linear. Do g(x) = 3x + 1 and then f(x) = 2x + 5, and you get 2(3x + 1) + 5 = 6x + 7, a straight line again. Stack a hundred layers and it is still the same as one straight line. So a bent or curved function such as ReLU or the sigmoid goes between the layers. Only then can more layers copy more complicated shapes.
Try this. In the first experiment, pick sigmoid and type 0 and 2 in the x box: you get 0.5 and 0.88. Raise a to 4 and it is already 0.98 at x = 1, so it switches from 0 to 1 almost like a step. Pick ReLU: it gives 0 at x = −3 and 2 at x = 2.
12. Key properties
| Property | In plain words |
|---|---|
| One input, one output | If a vertical line meets the graph twice, it is not a function. Different inputs giving the same output is fine |
| Linear adds, exponential multiplies | At every equal step, 2x + 1 adds 2 and 2ˣ multiplies by 2 |
| A logarithm is the exponential run backward | log₂(2ˣ) = x. The graphs mirror each other across y = x. It turns products into sums |
| Outside moves up and down, inside moves sideways | f(x) + k moves up, f(x − h) moves right, a·f(x) stretches up and down. Multiplying by a negative flips |
| The order for large x | log x < x < x·log x < x² < 2ˣ. For small x the order can differ (at x = 3, 2ˣ = 8 and x² = 9), but as x grows it always ends up in this order |
| Order matters when chaining | Usually f(g(x)) ≠ g(f(x)). (x + 3)² versus x² + 3 |
Next, read limits, which is about a function "getting closer" to a value, and then derivatives, which measure the slope of a graph.
Where it is used
- Programming. Pure functions, which give the same output for the same input, are easy to test and easy to cache. An array's
mapapplies a function to each item, and passing a value through several functions in a row is composition. - Estimating growth and decay. Compound interest, a user count that grows by a steady percentage, and an amount that shrinks by a steady percentage are all estimated with exponential functions.
- Log scale charts. When values differ by several digits (spread of response times, training loss curves), the vertical axis is drawn on a log scale. A straight line means exponential change.
- Choosing algorithms. As data grows, O(n²) slows down fast while O(n log n) holds up. Knowing which function the work grows like lets you choose ahead of time.
- AI. The sigmoid, ReLU, softmax and log loss are all functions from this page. A neural network is a long chain of them.
Frequently asked questions
What is a function?
A rule that takes one input and gives back one output. The same input always gives the same output. If f(x) = 2x + 1, then f(3) = 7. A graph is this rule drawn on a flat surface: every pair is a dot, with the input going across and the output going up.
Why can't you take the logarithm of 0 or a negative number?
A logarithm asks "how many times do I multiply by the base to reach this number?" With a positive base, the result is always above 0, no matter how many times you multiply, even a negative number of times or half a time. So no count ever reaches 0 or a negative number, and the logarithm has no value there.
What is the difference between log and ln?
Only the base. ln is the natural logarithm, with base e (about 2.718). Plain log means different bases in different fields. Math and many programming languages often mean e, engineering often means 10, and algorithms often mean 2. You can switch any base b with ln x ÷ ln b.
How is an exponential function different from a power function?
x sits in a different place. In 2ˣ (an exponential function), x is up in the exponent, so the value doubles every time x goes up by 1. In x² (a power function), x is down in the base, and the number of multiplications is fixed at 2. As x gets large, an exponential function always ends up bigger than any power function.
Is a function in programming the same as a function in math?
It is the same if it is a pure function: it always gives the same output for the same input and does not touch anything outside. A function that uses random numbers, reads the clock or changes a database can give different results for the same input, so it is not a math function.
Words
| Plain words used here | Textbook term |
|---|---|
| A rule that gives one output per input | function, f(x) |
| What goes in / what comes out | input, independent variable / output, function value, dependent variable |
| The x values you can put in | domain |
| The y values that can come out | range |
| No value there | undefined |
| The numbers in a formula | coefficients, parameters |
| A straight-line function | linear function |
| Slope / value at x = 0 | slope / y-intercept |
| A bowl-shaped function | quadratic function, parabola |
| The bottom of the bowl | vertex |
| The same multiplier at every step | exponential function |
| The number multiplied / how many times | base / exponent |
| How many times you multiplied | logarithm, log_b x |
| The logarithm with base e | natural logarithm, ln x |
| The number for interest that never stops, 2.718… | e, Euler's number |
| x multiplied by itself n times | power function |
| The function that goes backward | inverse function, f⁻¹ |
| Chaining functions | composition, composite function, f ∘ g |
| Shift / stretch / flip | translation / scaling / reflection |
| A scale where equal steps mean equal multipliers | logarithmic scale |
| Which function the work grows like for large n | big O notation, asymptotic notation |
| The S-shape that squeezes into 0 to 1 | sigmoid, logistic function |
| The line that bends negatives to 0 | ReLU (rectified linear unit) |
Sources
- OpenStax, Calculus Volume 1. 1.1 Review of Functions, 1.2 Basic Classes of Functions, 1.4 Inverse Functions, 1.5 Exponential and Logarithmic Functions.
- OpenStax, College Algebra 2e. 3.4 Composition of Functions, 6.1 Exponential Functions (compound interest and e), 6.3 Logarithmic Functions, 6.5 Logarithmic Properties.
- Knuth (1976). Big Omicron and big Omega and big Theta. SIGACT News. Big O notation.
- Nair, Hinton (2010). Rectified Linear Units Improve Restricted Boltzmann Machines. Glorot, Bordes, Bengio (2011). Deep Sparse Rectifier Neural Networks. ReLU.
- PyTorch documentation: torch.nn.Sigmoid, torch.nn.ReLU, torch.nn.Softmax.
- MDN Math.log, Python math module documentation. log in these languages uses base e.
← Tensors and shapes: axes, shape and broadcastingLimits and continuity →