Variables and Expressions in ML
A variable names a value that changes, an expression combines variables and numbers into a computable recipe. Losses, features and hyperparameters are all expressions waiting for inputs.
Why Does This Exist?
Every formula in ML is an expression with the inputs missing. A loss waits for weights. A feature waits for measurements. A learning-rate schedule waits for the epoch. Training is substitution at scale: plug values into expressions, read the outputs, adjust, repeat billions of times.
Variables and expressions are also where code meets math. The math writes , the code writes 2 * x, and fluency means seeing one in the other instantly. Expressions evaluate to numbers; equations with = signs get solved, which is the job of algebra. How whole networks of expressions evaluate node by node is in computational graphs.
The one idea you need first. A variable is a labelled box holding a number that can change. An expression is a recipe combining boxes and numbers with operations. Substituting means opening the boxes and computing.
Think of It Like This
Labelled jars in a kitchen
A kitchen has jars labelled flour, sugar, salt. A recipe says "two cups flour plus one sugar minus half salt": an expression over jars. Different jars, same recipe, different cake. The recipe never changes; only the jar contents do.
A model is a recipe book of expressions, and a training step fills the jars. The loss expression tastes the result. Backpropagation figures out which jar to adjust. Nobody rewrites the recipe per cake, just as nobody rewrites the loss per batch.
Where it stops: jars hold one thing at a time, while ML variables hold millions of values at once in tensors. The analogy shows naming and substitution; the scale needs its own page.
How It Actually Works
An expression has parts with names. In : and are coefficients (multipliers of variable terms), is the variable, the superscript is an exponent, and is the constant term (no variable attached). Terms are the chunks between plus and minus signs: , , . Like terms share the same variable part and combine: , but cannot merge.
Evaluating means substituting a value for every variable, then computing with the precedence ladder. An expression has no = sign, so there is nothing to solve; it produces a number, not an answer to a question.
Worked example: evaluating at x equals 4
Expression: . Substitute :
Powers first: , so . Products: . Sums left to right: , then . The value is .
Same expression at collapses to the constant: . At : . One recipe, three fillings, three results. The diagram above labels each part before the substitution runs.
Code
def evaluate(x: float) -> float: # 2x^2 - 3x + 1 with the value plugged in for x. return 2 * x**2 - 3 * x + 1
print(evaluate(4.0)) # -> 21.0print(evaluate(0.0)) # -> 1.0print(evaluate(1.0)) # -> 0.0Note 2 * x, never 2x: juxtaposition is math shorthand, and code demands the explicit star. The three prints are the three hand computations, agreeing exactly.
Watch Out For
Substituting a negative without parentheses
Evaluating at by writing -2**2 gives in Python, because the power binds first. The intended needs the brackets. The symptom is a feature that flips sign on every negative input, and a model trained on corrupted values with no error anywhere.
Parenthesise every substituted negative: (-2)**2. Better, substitute through a variable so the value carries its own grouping.
Reading 2x as two-x in code
Math writes and means multiply. Code written as 2x is a syntax error at best; worse is a variable actually named x2 being read as "two x". Features silently swap when a formula is transcribed carelessly.
Transcribe juxtaposition as * mechanically, and never name a variable so it reads as a coefficient times something else. The transcription step deserves its own slow pass.
The Quick Version
- Variables name changing values; expressions combine them into computable recipes.
- Coefficients multiply, constants stand alone, exponents shape, terms add.
- Evaluate by substituting values, then computing down the precedence ladder.
- at is ; at it is the constant .
- Parenthesise substituted negatives; transcribe as
2 * x, never by sight.