Majid Al-RaimiNeurons, weights and the fully connected layer

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
Understood
0/5 concepts

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

  1. Write a neuron as y = f(sum_i w_i x_i + b) and name what each symbol corresponds to in the biological picture.
  2. Use the two synonym families (synapses = weights = parameters; neurons = features = activations) and count layers without counting the input.
  3. 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.
  4. Count the parameters and per-sample activations of any MLP, and say which count the batch size touches.
  5. 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

  1. Scale each input by its weight

    0.5 × 1 = 0.5, -1 × 2 = -2, 2 × 0.5 = 1.
  2. Sum and add the bias

    0.5 - 2 + 1 + 0.25 = -0.25.
  3. 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.
  4. 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:

yj=f(iwixi+b)y_j = f\left(\sum_i w_i x_i + b\right)
One neuron: weighted sum, one bias, one squashing function

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.

Three inputs cross their synapses, arrive as w_i x_i at the summing cell body, pass the activation function f, and leave along the output axon
StructureWhat it does biologicallyWhat it is in the formula
Input axonCarries the raw signal x_i from the previous neuronAn input feature, one entry of the vector x
SynapseScales the signal crossing itThe weight w_i multiplying x_i
DendriteCarries the scaled signal w_i x_i into the cell bodyOne product term of the sum
Cell bodyAccumulates the incoming signalsThe sum of all w_i x_i plus one bias b
AxonFires when the total is large enoughThe activation function f and the output y_j
Biology to arithmetic, one row per structure

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.

y_j = f(Σ_i w_i x_i + b). Each w_i is a synapse (a weight), each x_i arrives on an input axon (an activation) and is scaled at the synapse, the dendrite carries the product w_i x_i into the cell body, and y_j is the output axon after the activation function f. The bias b is one per neuron.

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.

The 5-4-3-2 ladder: 20, 12 and 6 edges draw in as weights, then the 9 computed nodes fill as activations, while the 5 input nodes stay gray

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.

LayerWeightsBiasesParametersNeurons
Layer 0, hidden (5 to 4)5 × 4 = 204244
Layer 1, hidden (4 to 3)4 × 3 = 123153
Layer 2, output (3 to 2)3 × 2 = 6282
Total389479
Parameters of the slide 4 network, counted layer by layer

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?

The output layer is counted but not hidden. The input row is not counted at all, because it computes nothing and owns no parameters.

Recall

What do width and depth of a model mean?

Width is the dimensionality (number of neurons) of the hidden layers. Depth is the number of layers in the chain, not counting the input.

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.

yi=jwijxj+biy_i = \sum_j w_{ij}\, x_j + b_i
Output i sums over all inputs j. The first index of w is the output, the second the input
Y=XWT+b\mathbf{Y} = \mathbf{X}\,\mathbf{W}^{T} + \mathbf{b}
The same layer as one matrix product

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.

One row of X meets one column of W^T to light one cell of Y. Extra samples then add rows to X and Y while W^T stays exactly as it was

Worked example

The slide 5 layer with a batch of 32

  1. Shapes

    X (32, 5), W (3, 5), b (3,), Y (32, 3).
  2. Values held

    X holds 32 × 5 = 160 numbers and Y holds 32 × 3 = 96. Both are activations.
  3. Parameters

    Still 15 weights and 3 biases, 18 in total, identical to the single-sample case.
  4. Work

    Each output is 5 multiply-accumulates, so 15 per sample and 32 × 15 = 480 for the batch.
  5. Batch size scales activations and work, never parameters

    Activations went from 8 to 256 values; parameters stayed at 18.
SimulatorBuild a fully connected layer
Presets
5
3
1
off
Value precision
xxxxxyyyw₀₀w₂₄
X (1 × 5)×W1ᵀ (5 × 3)+b1 (3,)=Y (1 × 3)

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
Reference: 784 to 128100,480parameters784 × 128 + 128, an MNIST-sized first layer
Same layer in fp32402 kBexceeds 320 kB of SRAM, fits 1 MB of flash
Same layer in int8100 kBone byte per weight, four times smaller

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.

QuantityBatch n = 1Batch n = 8Memory
Weights784 × 128 = 100,352sameflash
Biases128sameflash
Parameters100,480sameflash
Parameter bytes, fp32401,920 B (about 392 KiB)sameflash
Parameter bytes, int8100,480 Bsameflash
Activations784 + 128 = 912 values8 × 912 = 7,296 valuesSRAM
Flash versus RAM for a 784 to 128 fully connected layer

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.

X (n, c_i), W (c_o, c_i), b (c_o,), Y (n, c_o), computed as Y = X Wᵀ + b.

Recall

Which quantity changes when the batch size doubles, and which microcontroller memory does each quantity map to?

Activations double, and they live in SRAM because they are read and written at runtime. Parameters are unchanged, and they live in flash because they are read only.

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.

Y1=XW1T+b1,Z=f(Y1)W2T+b2\mathbf{Y}_1 = \mathbf{X}\mathbf{W}_1^{T} + \mathbf{b}_1,\qquad \mathbf{Z} = f(\mathbf{Y}_1)\,\mathbf{W}_2^{T} + \mathbf{b}_2
Two layers chained. W1 is (3, 5), W2 is (2, 3); with a batch, X is (n, 5), Y1 is (n, 3) and Z is (n, 2)

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.

LayerW shapeWeightsBiasesParametersActivations per sample
FC1 (5 to 3)(3, 5)153183
FC2 (3 to 2)(2, 3)6282
Total215265 computed, 10 with inputs
Parameter and activation budget of the slide 7 MLP

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?

FC1 has 15 + 3 = 18, FC2 has 6 + 2 = 8, total 26 parameters. It computes 3 + 2 = 5 activations per sample, 10 if the 5 inputs are counted.

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