Syntax and Terminology
In this section, we’re going to run through most of the basic syntax and terminology that you’ll need to interpret mathematical equations, specifically for machine learning. This will give you a basis to understand the rest of the book.
Don’t worry if not everything makes sense yet. You’ll probably wish to refer back to this chapter as you move on.
Variables in Maths are not like variables in programming. In software I might write
a = a + 1;
I can do this because a is a pointer to a location in memory.
This makes no sense in mathematics. Variables in Maths have values or values that can be discovered. You can’t just set them to whatever you want. If I write:
is now 12 for the duration of the equation.
Capitals, Bold and Italic in Variable Names
- - lower case italic letters are used for variables representing scalar values, eg
- - bold lower case letters are used for vectors.
- - bold capitals are used to represent matrices and tensors, eg
This isn’t necessarily true for all Maths, but has become a convention in machine learning.
Scalars, Vectors, Matrices and Tensors
In Maths we have a lot of words for arrays. This is for historical reasons, various folks writing at different times using different words for related concepts.
A scalar is a single number. You can happily conceptualise this as a zero dimensional array or tensor if it makes you happy, the math will still work out.
Here’s a scalar in action:
Vectors are one dimensional arrays, just like regular arrays. They also share the same square bracket and comma syntax, which is nice:
Matrices are two dimensional arrays. We can optionally surround them with square braces. A matrix is an array of arrays:
Tensors are N dimensional arrays. A one dimensional tensor is also a vector. A two dimensional tensor is also a matrix.
Tensors are hard to represent on paper, but we could have a go at showing a 3d tensor using a vector of matrices:
If we want to specify that a variable X contains a tensor without drawing the tensor, we can do so by saying that Y is in the set of real numbers . More on sets later.
When writing code, we use tensors most of the time regardless of how many dimensionw we need. You may have heard of a package called TensorFlow? PyTorch also deals with tensors, as does Jax.
Single or Double Vertical Bars around vectors denote the length of the vector ||
Single or double bars around a vector denote the length of a vector. These two notations are used interchangeably in different contexts.
To find the length (or magnitude) of a vector given by , you can use the Pythagorean formula for the magnitude of a 2D vector:
- is the first component of the vector (in this case, 1).
- is the second component of the vector (in this case, 2).
Plugging in the values from vector :
So, the magnitude (or length) of vector is .
Single Vertical Bars around scalars are absolute values |
Single bars can also mean the absolute value of a scalar
We can also write this as a function:
We can define this like so:
Occasioanlly you will also see this:
Vertical bars in sets mean “such that”
A single bar separating two side of an equation means “such that”
TODO: Find real world example of this.
A hat means a prediction.
TODO: Create formula
We often use yHat as a variable name, to mean our current prediction. It’s not necessarily the final value, just what we have right now in this current training epoch.
A star means an ideal value, it’s the value we want to get to