3.3.8 · D1Deep Learning Frameworks

Foundations — TensorFlow - Keras basics

2,257 words10 min readBack to topic

Before you can read one line of the parent note, you need to recognise every symbol and word it throws at you. We build them from nothing, in an order where each rung of the ladder rests on the one below. Everything here supports TensorFlow - Keras basics.


0. What is a number-container? (the tensor)

The parent note's very first sentence talks about a "tensor". Let's earn that word.

Figure — TensorFlow - Keras basics

Look at the figure. Reading left to right:

  • A single number, like , extends in zero directions. Rank 0 — we call it a scalar.
  • A row of numbers extends in one direction. Rank 1 — a vector.
  • A grid of rows and columns extends in two directions. Rank 2 — a matrix.
  • Stack matrices behind each other and you get rank 3, 4, ... — a colour image is rank 3 (height × width × colour), and a batch of images is rank 4.

Notation — shape. The shape is just the list of how many entries lie along each direction, written in round brackets. In plain words:

  • a scalar has an empty shape, written — no directions.
  • a vector of 3 has shape — one direction, holding 3 numbers.
  • a 2 by 2 matrix has shape — two directions, 2 rows and 2 columns.
  • 32 grey images have shape — four directions.

The comma in is not a typo — it tells you "one direction, and it holds 3 numbers".


1. The two operations you must recognise: and matrix-multiply

The parent uses a + b, a * b, and tf.matmul. Two of these are the same everyday idea; one is new.

Figure — TensorFlow - Keras basics

Element-wise ( and ): line the two boxes up, act on matching cells.

  • Add: .
  • Multiply: — pair matching cells and multiply (). This is what a * b means; it is not matrix multiply.

Matrix multiplication (written tf.matmul): this is the new one, and it is the heart of every layer.

For a single number in the output:

Here means "add these up as runs ", where is the shared dimension: the number of columns of the left matrix, which must equal the number of rows of the right matrix. Look at the red highlighted row-and-column in the figure; the red output cell is their paired-and-summed value.


2. From symbols to a neuron:

Now every symbol in the parent's key formula is within reach.

Decode it piece by piece. The convention Keras uses is row vectors: one example is a horizontal strip of numbers, and a batch stacks such rows on top of each other.

  • — the input, written as a row vector of shape for one example (or for many). With pixels that is a strip.
  • — the weight matrix of shape : inputs down, neurons across. One dial per (input, neuron) pair.
  • — the bias, a row vector of shape : one extra dial per neuron that shifts its output up or down even when all inputs are zero. Picture the "+c" that lets a line not pass through the origin. It broadcasts across every row of the batch.
  • — the weighted sum ("logit"), shape : one number per neuron, before any bend.
  • (Greek "sigma") — the activation function: a bend applied to each number. More on it next.
  • — the activation: after the bend; the neuron's final output, also shape .

Notice the shapes line up exactly by the rule from Section 1: , then adding of shape broadcasts fine. That is why Keras writes (row on the left) rather than — the row-vector convention makes the batch dimension come first and the shapes chain cleanly.

Figure — TensorFlow - Keras basics

The figure shows three inputs feeding one neuron: each arrow carries a weight , the summing circle does , and the red box is the bend .

cases you'll meet:

  • ReLU: — keeps positives, flattens negatives to 0. (The bend at zero.)
  • Softmax (whole layer): — turns a vector of raw scores into positive numbers that sum to , i.e. probabilities. Here is Euler's number, the fixed constant , and is the exponential function (constant raised to the power ). We use it because is always positive and always grows as grows, so a bigger score gives a bigger probability, never a negative one.

The single neuron formula generalises to a full layer by letting have one column per neuron — that is the matrix multiply from Section 1.


3. Measuring "how wrong": the loss

The parent writes and calls it the loss.

Picture as the height of a landscape, and as your position on the ground. Training = walking downhill to the lowest valley.


4. The dial-nudge: gradient and the update rule

The parent's central training line is:

Three new symbols:

  • — the gradient (nabla). It is the arrow pointing in the direction where the loss-landscape rises fastest. Walking the opposite way (the minus sign) goes downhill.
  • (Greek "eta") — the learning rate: how big a step you take. Too big → you leap over the valley; too small → you crawl.
  • — "becomes": replace the old dials with the new ones.
Figure — TensorFlow - Keras basics

The red arrow in the figure is pointing uphill; the black step goes the other way, downhill, shrinking the loss. This is the whole of 3.1.01-Gradient-Descent, and the machinery that computes automatically is 3.2.03-Backpropagation — the reason we let TensorFlow, not our pencil, do the calculus.


5. Words around the training loop

Small vocabulary the parent's model.fit example assumes:

  • Batch / batch_size=32: process 32 examples together before nudging the dials once. Fewer nudges, steadier direction.
  • Epoch / epochs=5: one full sweep through all the training data. Five epochs = five sweeps.
  • Normalize / /255.0: pixels run ; dividing squeezes them to so gradients stay well-scaled.
  • Validation split: hide 20% of the data during training and test on it, to catch overfitting — memorising instead of learning (see 3.4.05-Overfitting-Regularization).
  • Dropout: randomly switch off some activations while training so the network can't lean on any one neuron — also an anti-overfitting trick.

Prerequisite map

Tensor - box of numbers

Shape and rank

Matrix multiply

Neuron z equals xW plus b

Activation sigma - the bend

Layer and full network

Loss L - how wrong

Gradient nabla L

Update rule with rate eta

TensorFlow Keras basics

The map reads bottom-up: boxes of numbers become neurons, neurons stack into a network, the network's error is measured by a loss, the gradient of that loss drives the update, and all of it is what the parent topic ties together.


Equipment checklist

Cover the right side and answer aloud; reveal to check.

What is a tensor, in one phrase?
A grid (array) of numbers; its rank is how many directions it extends.
What does the shape describe?
A batch of 32 grey images, each 28 by 28 pixels, 1 colour channel.
When can two tensors be added or multiplied element-wise?
When their shapes are identical, or one broadcasts (a size-1/missing direction gets copied to fit).
For matrix multiply, which dimensions must agree and what shape comes out?
The inner dimensions: needs the two 's equal and yields .
In with the Keras row-vector convention, what are the shapes?
is , is , is , and is .
Why does an element-wise multiply differ from matrix multiply?
Element-wise pairs matching cells; matrix multiply sums each row against each column.
Why is an activation needed?
Without a bend, stacked layers collapse into a single straight-line function; the nonlinearity lets the network fit curves.
What is in the softmax, and why use it?
Euler's number (); is always positive and increasing, so bigger scores map to bigger probabilities.
What does the loss measure and what is ?
How wrong the predictions are (big = bad); names all the weights and biases together.
What does the gradient point toward, and which way do we step?
It points uphill (fastest increase of loss); we step the opposite way (minus sign) to go downhill.
What does the learning rate control?
The size of each downhill step.
Epoch vs batch?
A batch is a small group processed before one dial-nudge; an epoch is one full pass over all the data.
Why divide pixels by 255?
To normalise them into so gradients are well-scaled and training converges faster.