COE 592Lecture 02Part 01
Neurons, weights and the fully connected layer
The vocabulary of neural networks and the simplest layer, where every output neuron sees every input neuron.
- Concepts
- 5
- Slides
- 1-7
- Reading
- 30 min
Why this part matters
Every TinyML decision you will make in this course, whether to prune a model, quantize it, or pick a smaller architecture for a Cortex-M board, starts from two counts that this part defines. Parameters are what a model stores. Activations are what it computes for each input. Until you can produce both numbers from a network diagram, no later result about efficiency will mean anything concrete.
The lecture opens by fixing vocabulary that the rest of the course, and most papers you will read, take for granted: neuron, synapse, weight, activation, feature, parameter, width, depth. It then introduces the simplest layer, the fully connected layer, and writes it as a matrix product with named tensor shapes. Exams ask for those shapes and for parameter counts. Your research project needs the same arithmetic to decide whether a model fits the flash and RAM of a device.
By the end you can
- Write a neuron as y = f(sum_i w_i x_i + b) and name what each symbol corresponds to in the biological picture.
- Use the two synonym families (synapses = weights = parameters; neurons = features = activations) and count layers without counting the input.
- State the shapes X (n, c_i), W (c_o, c_i), b (c_o,) and Y (n, c_o) of a fully connected layer and write Y = X W^T + b.
- Count the parameters and per-sample activations of any MLP, and say which count the batch size touches.
- Map parameters to flash storage and activations to runtime RAM on an embedded target.
Take a concrete board. The STM32F746, an ARM Cortex-M7 microcontroller used throughout the TinyML literature, has 320 kB of SRAM and 1 MB of flash (Lin et al., MCUNet, section 1). A ResNet-50 carries about 25.6 million parameters (torchvision), which is roughly 100 MB of weights in single precision. It does not fit, and no clever compiler will change that. Whether any network fits comes down to two numbers, and the whole point of this lecture is to teach you how to produce them from a diagram.
Two microcontrollers from the MCUNet paper
- STM32F746
- 320 kB SRAM, 1 MB flash
- STM32H743
- 512 kB SRAM, 2 MB flash
The two memories play different roles, and MCUNet states the split in one sentence: SRAM constrains the activation size, which is read and written at runtime, while flash constrains the model size, which is read only (Lin et al., MCUNet). So a model has a storage cost, the number of parameters it carries, and a runtime cost, the number of activations it must hold in memory while it computes. This part defines both words precisely and shows how to count them for the simplest layer. The parts that follow do the same for convolutions, pooling, normalization and attention.
The lecture's outline is a two-step plan. First fix the terminology: neuron, synapse, activation, feature, weight, parameter. Then learn the building blocks one at a time: fully connected, convolution, grouped and depthwise convolution, pooling, normalization, and the transformer block. This part covers the terminology and the first block. Everything later is a variation on the counting you learn here.
Start with a single unit and three numbers arriving at it: x0 = 1, x1 = 2 and x2 = 0.5. The unit does not treat them equally. It trusts the first a little, distrusts the second, and trusts the third a lot: w0 = 0.5, w1 = -1, w2 = 2. It also has a personal lean of b = 0.25. Multiply each input by its trust, add everything up, and the total is 0.5 - 2 + 1 + 0.25 = -0.25. One last step decides whether the unit speaks: a function f applied to that total.
Worked example
One neuron with three inputs
Scale each input by its weight
0.5 × 1 = 0.5, -1 × 2 = -2, 2 × 0.5 = 1.Sum and add the bias
0.5 - 2 + 1 + 0.25 = -0.25.Apply the activation function
With f the identity the output is -0.25. With f = ReLU, which returns max(0, z), the output is 0.The activation function decides whether the neuron fires
Same inputs, same weights, same bias: -0.25 or 0 depending only on f.
That is the whole artificial Neuron. Jurafsky and Martin put it in one sentence: a neural unit takes a weighted sum of its inputs with one additional term called the bias, then applies a function to the result. In symbols, with i ranging over the inputs of neuron j:
The slide draws this formula on top of a biological neuron, and the mapping is worth learning because the course reuses the biological words as technical terms. Sze, Chen, Yang and Emer, whose survey carries the same figure (adapted from Stanford CS231n), describe the key property of a Synapse: it scales the signal crossing it, and that scaling factor is the Weight. Each raw input x_i arrives along an input axon, is scaled to w_i x_i at the synapse, and travels down a dendrite into the cell body, which sums the products; the output axon carries the result out. They add that the input and output signals of a neuron are what the field calls activations.
| Structure | What it does biologically | What it is in the formula |
|---|---|---|
| Input axon | Carries the raw signal x_i from the previous neuron | An input feature, one entry of the vector x |
| Synapse | Scales the signal crossing it | The weight w_i multiplying x_i |
| Dendrite | Carries the scaled signal w_i x_i into the cell body | One product term of the sum |
| Cell body | Accumulates the incoming signals | The sum of all w_i x_i plus one bias b |
| Axon | Fires when the total is large enough | The activation function f and the output y_j |
Two details of the formula matter for counting parameters later. There is one weight per incoming edge, so a neuron with three inputs owns three weights. There is exactly one Bias per neuron, not one per edge, which is why the figure shows three w symbols and a single b. The activation function f is applied once, to the finished sum. Later in this lecture you will meet several choices for f (sigmoid, ReLU, ReLU6, swish); for now it is enough to know that it is usually non-linear and that it is what lets stacked neurons compute anything more interesting than a single straight-line function.
Recall
Write the formula for one neuron and name which symbol is the synapse, which arrives on the input axon, what the dendrite carries, and which is the output axon.
Now wire many neurons together. The network on the slide has a row of 5 inputs, then rows of 4, 3 and 2 neurons, with every neuron connected to every neuron in the row above. Count the edges band by band: 5 × 4 = 20, 4 × 3 = 12, 3 × 2 = 6, so 38 edges. Count the biases, one per computed neuron: 4 + 3 + 2 = 9. That is 47 numbers the network stores and 9 numbers it computes per input. Everything else in this concept is the vocabulary for those two counts.
Two synonym families
The slide attaches two labels to the diagram, and each is a family of three words the course uses interchangeably. The edges are synapses, weights or parameters. The nodes are neurons, features or activations. Sze et al. state the convention directly: the outputs of the neurons are often referred to as activations, and the synapses are often referred to as weights. Which word a paper picks depends on what it is emphasising. "Synapse" stresses the biology, "weight" the arithmetic, "parameter" the fact that it is learned and stored. "Feature" stresses what a node represents, "activation" the fact that it is a computed value that must be held in memory.
| Layer | Weights | Biases | Parameters | Neurons |
|---|---|---|---|---|
| Layer 0, hidden (5 to 4) | 5 × 4 = 20 | 4 | 24 | 4 |
| Layer 1, hidden (4 to 3) | 4 × 3 = 12 | 3 | 15 | 3 |
| Layer 2, output (3 to 2) | 3 × 2 = 6 | 2 | 8 | 2 |
| Total | 38 | 9 | 47 | 9 |
CS231n gives a second example you can use to check your method: a network with layers of sizes 3, 4, 4 and 1 has 4 + 4 + 1 = 9 neurons, 3 × 4 + 4 × 4 + 4 × 1 = 32 weights and 9 biases, for 41 learnable parameters. The recipe is always the same: multiply adjacent layer sizes for weights, add each non-input layer size for biases.
Counting layers, and why the input does not count
The slide calls this a 3-layer network with 2 hidden layers, and both numbers follow one convention: the input row is not a layer. CS231n states it plainly: when we say N-layer neural network, we do not count the input layer, so a single-layer network has no hidden layers. Jurafsky and Martin say the same in a figure caption. The reason is that the inputs compute nothing. They own no weights and no biases; they are simply the numbers handed in. The three computed rows are the layers, the two blue rows in the middle are the hidden layers, and the gray bottom row is the output layer, counted but not hidden.
Width and depth
The sentence at the bottom of the slide is quoted from Goodfellow, Bengio and Courville: the dimensionality of the hidden layers determines the width of the model. On the same pages they define the other axis: a network is a chain of functions, and the overall length of the chain gives its depth. So the slide network has width 4 at its widest hidden layer and depth 3. Making a network wider adds neurons to a row, which grows both parameters and activations. Making it deeper adds rows, which also grows both, but lets features be composed out of earlier features.
Quick check
How many learnable parameters does the 5-4-3-2 network on slide 4 contain in total?
Quick check
In the vocabulary of this lecture, what does the width of a model refer to?
Recall
In the phrase "3-layer network with 2 hidden layers", which layer is counted but not hidden, and which row is not counted at all?
Recall
What do width and depth of a model mean?
Take one band of the ladder and give it names: c_i = 5 inputs x0 to x4 and c_o = 3 outputs y0 to y2, every output connected to every input. Write out the first output by hand: y0 = w00 x0 + w01 x1 + w02 x2 + w03 x3 + w04 x4 + b0. Do the same for y1 and y2. That is 15 products and 3 biases, 18 parameters. Now stack the three rows of weights on top of each other and you have a 3 × 5 table. That table is W, and the three equations collapse into one matrix product.
This is the fully connected layer, also called a linear layer. Jurafsky and Martin define fully connected exactly this way: each unit in a layer takes as input the outputs of all the units in the previous layer, with a link between every pair of units in adjacent layers. The word Channel is the lecture's general name for a feature dimension, so c_i and c_o count input and output channels; for a fully connected layer a channel is simply one neuron.
Tensor shapes of a fully connected layer
- Input features X
- (1, c_i) for one sample, (n, c_i) for a batch
- Output features Y
- (1, c_o) for one sample, (n, c_o) for a batch
- Weights W
- (c_o, c_i)
- Bias b
- (c_o,)
The transpose in Y = X Wᵀ is not decoration. W is stored with one row per output neuron, shape (c_o, c_i), so that row i holds the weights of y_i. To multiply a row vector x of length c_i by it, the matrix has to be flipped to (c_i, c_o). PyTorch uses precisely this convention: nn.Linear computes y = x Aᵀ + b with a weight of shape (out_features, in_features) and a bias of shape (out_features). Jurafsky and Martin write the same matrix as W ∈ R^(n1 × n0), with element W_ji the weight from input i to hidden unit j. Output index first, input index second, in every source.
Batching: more rows, same weights
Nothing in the product cares whether X has one row. Put n samples in as n rows and the same multiplication produces n rows of outputs. That is all the batch size is: a leading dimension on X and Y. PyTorch describes the input as (*, H_in), where the star means any number of leading dimensions, and the output as (*, H_out); the weight stays (H_out, H_in) no matter what the star is. That is the fact to hold on to: W and b never change with n.
Worked example
The slide 5 layer with a batch of 32
Shapes
X (32, 5), W (3, 5), b (3,), Y (32, 3).Values held
X holds 32 × 5 = 160 numbers and Y holds 32 × 3 = 96. Both are activations.Parameters
Still 15 weights and 3 biases, 18 in total, identical to the single-sample case.Work
Each output is 5 multiply-accumulates, so 15 per sample and 32 × 15 = 480 for the batch.Batch size scales activations and work, never parameters
Activations went from 8 to 256 values; parameters stayed at 18.
Move a slider to see which counts respond.
- FC1 weights, W (3, 5)
- 3 × 515
- Biases, one per output neuron
- 33
- Parameters, stored in flash
- 15 + 318
- Activations per batch, live in RAM
- 1 × 88
- Multiply-accumulates per batch
- 1 × 1515
- Parameter storage
- 18 × 4 B72 B
- Activation memory
- 8 × 4 B32 B
The highlighted edges carry w₀₀ and, once the layer has more than one edge, the last weight, indexed (output, input) as in y_i = Σ_j w_ij x_j + b_i. Parameters count every edge plus one bias per output neuron; activations count every node value for every sample in the batch, inputs included.
What the two counts cost on a microcontroller
Scale the same arithmetic to a realistic first layer, one that takes a flattened 28 × 28 MNIST digit (784 values) to 128 hidden units. The table separates what the layer stores from what it computes, and shows which memory of the STM32F746 each column lands in.
| Quantity | Batch n = 1 | Batch n = 8 | Memory |
|---|---|---|---|
| Weights | 784 × 128 = 100,352 | same | flash |
| Biases | 128 | same | flash |
| Parameters | 100,480 | same | flash |
| Parameter bytes, fp32 | 401,920 B (about 392 KiB) | same | flash |
| Parameter bytes, int8 | 100,480 B | same | flash |
| Activations | 784 + 128 = 912 values | 8 × 912 = 7,296 values | SRAM |
The 401,920 bytes of fp32 weights would not fit the board's 320 kB of SRAM but sit comfortably in its 1 MB of flash, which is exactly why weights live in flash. Storing them as 8-bit integers cuts the same column by four, the first hint of why quantization matters later. Only the activation row moved when the batch grew, and it moved linearly.
Quick check
A fully connected layer maps 5 input features to 3 output features. In the course's convention, what is the shape of its weight tensor W?
Quick check
The batch size of an FC layer with 5 inputs and 3 outputs grows from 1 to 32. Which count changes?
Recall
Write the shapes of X, W, b and Y for a batched FC layer with n samples, c_i inputs and c_o outputs, and the formula that joins them.
Recall
Which quantity changes when the batch size doubles, and which microcontroller memory does each quantity map to?
Put a second fully connected layer after the first. The slide takes the 5 to 3 layer and feeds its three outputs into a 3 to 2 layer that produces z0 and z1. The new layer has W2 of shape (2, 3), six weights, and b2 with two biases: 8 more parameters, 26 in total. Per sample it computes two more activations, so 5 computed values, 10 if you include the inputs held in memory.
A chain of fully connected layers is a multilayer perceptron, or MLP. Goodfellow, Bengio and Courville write the chain as f(x) = f⁽³⁾(f⁽²⁾(f⁽¹⁾(x))), call f⁽¹⁾ the first layer and f⁽²⁾ the second, and define depth as the length of that chain. Jurafsky and Martin add a caution about the name: modern multilayer networks are called perceptrons for historical reasons only, since their units are not perceptrons in the strict sense. Use the name, but do not read anything into it.
The shapes must click together: the output channels of layer k are the input channels of layer k + 1. Here c_o = 3 of the first layer becomes c_i = 3 of the second, and the inner dimension of the second product matches. The batch dimension n rides through unchanged, just as it did in one layer.
| Layer | W shape | Weights | Biases | Parameters | Activations per sample |
|---|---|---|---|---|---|
| FC1 (5 to 3) | (3, 5) | 15 | 3 | 18 | 3 |
| FC2 (3 to 2) | (2, 3) | 6 | 2 | 8 | 2 |
| Total | 21 | 5 | 26 | 5 computed, 10 with inputs |
Once you see the MLP as a chain, counting any network becomes mechanical, and this is the skill the rest of the course leans on. For each fully connected layer the parameters are c_o × c_i + c_o; sum over the layers. The activations per sample are the sum of the layer output sizes, plus the input if you are budgeting memory for it. Multiply activations by n for a batch. Never multiply parameters by n.
Quick check
A 5 to 3 fully connected layer is followed by a 3 to 2 layer. How many learnable parameters does the pair contain?
Recall
How many parameters does the slide 7 MLP (5 to 3 to 2) have, and how many activations per sample does it compute?
Recap
If you remember nothing else
- A neuron computes a weighted sum of its inputs plus one bias, then applies an activation function f.
- Edges are synapses, weights or parameters; nodes are neurons, features or activations.
- Layers are counted without the input; 5-4-3-2 is a 3-layer network with 2 hidden layers and 47 parameters.
- Width is the size of the hidden layers, depth is the number of layers in the chain.
- An FC layer has W of shape (c_o, c_i) and b of shape (c_o,), and computes Y = X W^T + b.
- Batching adds rows to X and Y, giving (n, c_i) and (n, c_o); it never changes W or b.
- Parameters per FC layer = c_o x c_i + c_o; the slide 5 layer has 18, the slide 7 MLP has 26.
- Parameters determine flash (model size); activations determine SRAM (runtime memory).
Sources
- Deep Learning, chapter 6: Deep Feedforward NetworksBookMIT Press, Goodfellow, Bengio and Courville, 2016Depth as the length of the chain of layers, width as the dimensionality of the hidden layers (the sentence quoted on slide 4).(opens in a new tab)
- Speech and Language Processing, chapter 6: Neural NetworksBookJurafsky and Martin, 3rd edition draftThe neural unit z = w·x + b, W in R^(n1 × n0), input layer not counted, fully connected definition, MLP naming, XOR.(opens in a new tab)
- Neural Networks Part 1: Setting up the ArchitectureDocsStanford CS231nBiological motivation, the N-layer convention that excludes the input, and the 41-parameter sizing example.(opens in a new tab)
- torch.nn.LinearDocsPyTorch documentationy = x A^T + b with weight (out_features, in_features), bias (out_features), input (*, H_in) and output (*, H_out).(opens in a new tab)
- Efficient Processing of Deep Neural Networks: A Tutorial and SurveyPaperProceedings of the IEEE, Sze, Chen, Yang and Emer, 2017Reproduces the neuron and synapse figure (adapted from CS231n), synapse as a scaling factor, and fixes the activation and weight nomenclature.(opens in a new tab)
- MCUNet: Tiny Deep Learning on IoT DevicesPaperNeurIPS 2020, Lin, Chen, Lin, Cohn, Gan and HanSTM32F746 with 320 kB SRAM and 1 MB flash; SRAM constrains activations, flash constrains model size.(opens in a new tab)
- torchvision.models.resnet50DocsPyTorch documentationLists 25.6 million parameters for ResNet-50, used to size the model that does not fit a microcontroller.(opens in a new tab)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, Lecture 2: Basics of Deep LearningDocsMIT HAN Lab, Song Han, fall 2024The source deck that slides 2 to 7 reproduce, including the Layer 0 and w42 labels.(opens in a new tab)