Math for AI course · Lesson 7 of 180
Powers for AI: Why Doubling Context Can Quadruple Attention Work
Learn exponent laws and roots, then use a log-scale explorer and NumPy to see why doubled model dimensions or context can create four times as many quantities.
A sequence of tokens contains ordered token pairs. Double the sequence to tokens and the pair count becomes —four times as many. Nothing about the number changed. The operation applied to it did.
That operation is a power. Powers compress repeated multiplication into a small superscript, and the superscript tells us how a quantity responds when its input changes scale. By the end of this lesson, you will be able to manipulate positive, zero, negative, and fractional exponents; treat roots as inverse powers with real-number domain limits; and calculate when an AI quantity grows linearly or quadratically.
This lesson builds on Lesson 2’s view of functions as reusable rules. Here, the rule may be , and the question is how its output changes when changes. The official Mathematics for Machine Learning companion separates mathematical foundations from the machine-learning systems that use them. Its introductory chapter motivates those foundations as tools for understanding models and their assumptions; the exponent algebra below is an original prerequisite bridge developed for this course.
One superscript controls the growth rule
In , is the base and is the exponent. The exponent says how the base participates in the operation.
The intuition is “multiply the base by itself times,” but that only defines positive whole-number exponents. The formal starting point is:
Zero and negative exponents extend this definition while preserving the same algebraic laws. For any nonzero ,
The condition matters. A negative power creates a reciprocal, and division by zero is undefined. For example,
whereas has no real value.
The zero-exponent rule follows from the quotient law rather than from “no multiplication at all.” If , then
Both expressions can agree only if . Because the cancellation used , this argument does not decide ; that boundary needs its own context.
Exponent laws account for every factor
For positive integers and , multiplying two powers with the same base joins their repeated factors:
The same counting idea gives the core laws below. Negative exponents require nonzero bases, and fractional exponents require the domain care introduced in the next section.
| Operation | Law | Necessary caution |
|---|---|---|
| Multiply like bases | Both powers must be defined | |
| Divide like bases | ||
| Raise a power | Check the real-number domain for fractional powers | |
| Raise a product | Always safe for integer ; use care with real exponents |
One tempting pattern is not an exponent law:
For , , and , the left side is , while the proposed right side is . Addition inside parentheses must be handled before the power, or expanded with the distributive law.
Roots reverse powers, with a domain attached
The principal square root of a nonnegative real number , written , is the nonnegative real number whose square is :
Thus . Although both and equal , the radical symbol names the nonnegative root. Solving the equation is a different task and has two real solutions, and .
For , the square root is the one-half power:
More generally, for positive and positive integer , . A rational exponent combines a root and a power:
A square root does not mean “divide by two.” The number is , while because . The exponent is what is halved: for real . More generally, , not always ; for , both sides equal .
A doubling test exposes linear and quadratic growth
Suppose a quantity follows the power rule
where is an input size, is the scaling exponent, and is a constant that does not change during the comparison. If the input is multiplied by a scale factor , then
The output therefore changes by the factor . With a doubling, :
| Exponent | Rule | Effect of | Name used here |
|---|---|---|---|
| times | linear growth | ||
| times | quadratic growth | ||
| times | cubic growth | ||
| as large | inverse growth |
The explorer places powers of two at equal distances on each axis. That is a logarithmic scale: equal steps represent equal multiplication factors rather than equal additions. Move the sequence-length control with a pointer or the arrow keys and compare the straight solid line, , with the dashed square curve, .
Choose a power-of-two length from 128 to 8,192 tokens. Arrow keys change one doubling at a time.
- Selected length
- 1,024
- Linear growth
- 8×
- Quadratic growth
- 64×
- n² score positions
- 1,048,576
At n = 1,024, linear growth is 8× the baseline and quadratic growth is 64×, giving 1,048,576 score positions.
Show the chart data as a table
| Sequence length n | Linear n | Quadratic n² | Score positions n² | Current state |
|---|---|---|---|---|
| 128 | 1× | 1× | 16,384 | |
| 256 | 2× | 4× | 65,536 | |
| 512 | 4× | 16× | 262,144 | |
| 1,024 | 8× | 64× | 1,048,576 | ← Selected |
| 2,048 | 16× | 256× | 4,194,304 | |
| 4,096 | 32× | 1,024× | 16,777,216 | |
| 8,192 | 64× | 4,096× | 67,108,864 |
The server-rendered starting state selects . Relative to , the length is times as large, but the square is times as large. The table beneath the chart keeps every plotted value available without relying on the visual shape or on JavaScript.
Dense attention turns token pairs into a square
The original Transformer paper defines scaled dot-product attention with : each query is compared with every key before the scores are scaled, normalized, and used to combine values. In its classical dense self-attention analysis, the paper reports a per-layer complexity term of , where is sequence length and is representation width (Vaswani et al., Attention Is All You Need).
The matrix notation can be read as a grid even before the course formally introduces matrices. With token positions:
- there are query rows;
- each row has one score position for each of keys; and
- the full score grid therefore contains positions per head.
If each query-key comparison uses features, forming those dot products has a leading arithmetic term proportional to . Holding fixed while changing only gives the doubling calculation from the opening:
This count is a mathematical property of the full dense score grid. It does not guarantee that wall-clock time or peak memory will rise by exactly times. Parallel hardware, tiling, fused or recomputed kernels, masking, and attention mechanisms that avoid the full grid can change what an implementation stores and how quickly it runs. The safe claim is narrower: classical dense self-attention has query-key score positions, and its standard arithmetic complexity contains a quadratic sequence-length term.
Parameter growth depends on which dimensions change
A dense layer that maps input features to output features needs one weight for every input-output pair:
An optional bias adds more parameters, but the pairwise weight table is the main scaling relationship in this example.
Suppose the layer changes from inputs and outputs to inputs and outputs. Both dimensions doubled:
The ratio makes the exponent visible:
If only the output width doubled, the count would double instead. Calling all parameter growth “quadratic” hides which dimensions actually changed. A square law appears here only when two multiplied dimensions scale together.
NumPy lets the formula and its inverse check each other
NumPy’s np.logspace
constructs values at equal intervals on a log scale; with base , integer
endpoints through generate the context lengths through .
Its np.power
raises corresponding array elements to powers, and
np.sqrt
returns the nonnegative square root element by element.
import numpy as np
lengths = np.logspace(7, 13, num=7, base=2, dtype=np.int64)
score_positions = np.power(lengths, 2)
quadratic_growth = score_positions // score_positions[0]
recovered_lengths = np.sqrt(score_positions)
print("lengths:", lengths)
print("score positions:", score_positions)
print("quadratic growth:", quadratic_growth)
print("recovered lengths:", recovered_lengths)
lengths: [ 128 256 512 1024 2048 4096 8192]
score positions: [ 16384 65536 262144 1048576 4194304 16777216 67108864]
quadratic growth: [ 1 4 16 64 256 1024 4096]
recovered lengths: [ 128. 256. 512. 1024. 2048. 4096. 8192.]
The final line checks the inverse relationship on nonnegative inputs: because every sequence length in the array is positive. For an array that could contain negative , the correct identity would be .
The boundary cases are part of the operation
Exponent notation is compact enough to hide a domain error. These cases should remain visible when reading equations or code:
- depends on context. The rule above assumed , while for positive . At , those extensions meet without deciding one value. Some combinatorial formulas and software systems define it as for convenience; elementary real exponent algebra often leaves it undefined. State the convention instead of silently choosing one.
- Even roots of negative real numbers are not real. No real satisfies , so has no real value. Complex numbers extend the domain, but they are outside this lesson.
- A negative base with a fractional exponent needs special care. The real
cube root exists, so the exact rational expression
can be interpreted as . A floating-point exponent such as
1 / 3is an approximation, however, and NumPy’s real-valuednp.powerreturnsnanfor a negative base with a non-integral exponent. Do not assume software will reconstruct the intended fraction. - A power does not distribute over addition. The counterexample is enough to reject that shortcut.
- A root does not divide the value by its index. , not . Roots divide exponents when the relevant domain assumptions hold.
The log-scale explorer already hints at the next question. If , which operation recovers ? Lesson 8 introduces logarithms as inverse powers and uses them to turn multiplicative scale into additive steps. Until it is live, the Math for AI course page keeps the verified publication order.
Check your understanding
Question 1
Evaluate 3⁴ by expanding the power, and identify the base and exponent.
Show the step-by-step solution
In , the base is and the exponent is . A positive integer exponent counts repeated factors of the base:
Multiply in stages:
Therefore . Multiplying would confuse a factor count with ordinary multiplication.
Question 2
For x ≠ 0, simplify x³x⁻⁵, rewrite the result without a negative exponent, and check it at x = 2.
Show the step-by-step solution
The bases match, so multiplication adds the exponents:
A negative exponent means reciprocal, and the assumption makes that reciprocal valid:
At ,
The simplified form gives as well, so the calculation checks.
Question 3
Evaluate 64⁻²ᐟ³ and explain the role of the negative sign, numerator 2, and denominator 3 in the exponent.
Show the step-by-step solution
The negative sign asks for a reciprocal:
The denominator asks for a cube root, and the numerator then asks for a square:
Combining the steps gives
The base is positive and nonzero, so both the root and reciprocal are valid in the real numbers.
Question 4
A dense layer grows from 300 × 200 weights to 600 × 400 weights. Calculate both counts and explain why doubling both dimensions produces a fourfold change.
Show the step-by-step solution
The original layer has one weight for each input-output pair:
The larger layer has
Their ratio is
Equivalently, each dimension gained a factor of , so the product gained . This reasoning counts weights only; optional biases would add one value per output rather than another full pairwise table.
Question 5
Classical dense self-attention increases a sequence from 512 to 1,024 tokens while head width stays fixed. Calculate the query-key score positions before and after, then state what the ratio does and does not prove.
Show the step-by-step solution
For a full dense query-key grid, the position count is . Before the increase,
After doubling the length,
The ratio is
This proves that the full grid has four times as many score positions and that the standard dense attention arithmetic has a quadratic length term when head width is fixed. It does not prove an exact change in measured latency or peak memory, because implementation and hardware behavior are additional variables.
Question 6
A student claims (a + b)² = a² + b². Test the claim with a = 2 and b = 3, then give the correct expansion.
Show the step-by-step solution
Substituting and into the left side gives
The claimed right side gives
Because , one counterexample disproves the claimed identity. The correct expansion follows by distributing both factors:
At and , this gives , matching the left side.
Question 7
In the real-number setting, classify 0⁻², √(−9), (−8)¹ᐟ³, √((−5)²), and 0⁰. Give a reason or convention warning for each.
Show the step-by-step solution
Consider each expression with its required domain:
- would divide by zero, so it is undefined.
- has no real value because no real number has square .
- can be read as the real cube root because the root index is odd. Floating-point power functions may not preserve this exact rational interpretation.
- . The principal square root is nonnegative, so this equals , not .
- is context-dependent. The zero-exponent derivation assumed a nonzero base, so it does not settle this case. A formula or software system must state whether it adopts a value such as or leaves the expression undefined.
The common lesson is that notation alone does not erase the assumptions used to define the operation.