COE 592Lecture 03Part 02
Parameters and model size
Counting weights for linear, convolution, grouped and depthwise layers, totaling AlexNet to 61M parameters, and converting parameter count into storage at different bit widths.
- Concepts
- 4
- Slides
- 10-17
- Reading
- 24 min
Why this part matters
Every result you will meet later in this course, from pruning AlexNet "9x smaller" to Deep Compression's "35x" storage cut, is a ratio of two numbers computed the way this part teaches. Before you can judge pruning, quantization or an architecture search, you must be able to count what a network stores, and the same count decides whether a model fits into a microcontroller's flash.
Lecture 02 taught how linear, convolution, grouped and depthwise layers compute their outputs. This part does not repeat that. It asks one narrower question of each layer: how many numbers does it keep? Four short formulas answer it, AlexNet gives them a real workout, and one multiplication by the bit width turns the answer into megabytes. Along the way you will find a typo on the slide, a hidden value to compute yourself, and a simulator you can use to check every parameter and model-size number here and the MAC and FLOP numbers in part 04.
By the end you can
- Define #Parameters as the element count of all weight tensors and explain why n, h and w never enter it.
- Write and derive the four counting formulas (linear, convolution, grouped, depthwise) and justify the single division by g.
- Total AlexNet layer by layer, identify where 96 percent of the weights sit, and spot the slide's conv1 typo.
- Convert a parameter count to model size at any bit width in bits, KB and MB, stating the decimal convention.
- Predict how architecture choices (groups, depthwise filters, linear layer width) move model size before touching hardware.
Start with the smallest network worth drawing: five inputs x0 to x4 and three outputs y0 to y2, every input wired to every output. Count the arrows. Each input sends one arrow to each output, so there are 3 × 5 = 15 arrows, labelled from w00 up to w42. Each arrow carries exactly one learned number, its weight. That is the whole idea of the number of parameters: it is the synapse count, the number of weights, and for this layer it is 15.
Now look at the same layer the way a framework stores it. The inputs for a batch are a matrix X of shape n × c_i, the weights are a matrix W of shape c_o × c_i, and the outputs come out as Y = X · W^T of shape n × c_o. The fifteen arrows have become the fifteen elements of W. This is the second, equivalent definition the slides give: #Parameters is the number of elements in the network's weight tensors. For one linear layer that is the product of its two dimensions.
One thing in that matrix picture deserves a second look. The batch size n appears in X and in Y, but never in W. Feed the layer one image or a thousand and the weight matrix is the same c_o × c_i block. Parameter count is a property of the model, not of the data flowing through it. Hold on to this observation: it returns in the next concept for spatial size, and it is the reason parameters and MACs behave so differently in part 04.
The slides define the count for the "given" (slide 10) or "entire" (slide 11) neural network; both mean the same thing, the sum over every layer. They also say bias is ignored, and this lecture keeps that convention throughout. In PyTorch a linear layer stores a bias vector of length c_o and a convolution stores one bias per output channel, so the true count is larger by c_o per layer (PyTorch Conv2d documentation). Next to c_o · c_i that is a rounding error, which is why the lecture drops it.
Notation used on slides 11 to 14 and in the rest of this lecture
- n
- Batch size, the number of inputs processed together
- c_i, c_o
- Input and output channels (for a linear layer, input and output features)
- h_i, h_o
- Input and output height
- w_i, w_o
- Input and output width
- k_h, k_w
- Kernel height and width
- g
- Number of groups in a grouped convolution
Layer mechanics live in lecture 02
How a linear layer, a convolution, padding and stride, grouped and depthwise convolution and pooling produce their outputs, including the output-shape arithmetic behind numbers like 96 × 55 × 55, was covered in lecture 02: neurons and linear layers, convolution layers, padding, stride and grouped convolution and pooling.
Recall
A linear layer maps 512 inputs to 256 outputs. How many parameters does it have, ignoring bias, and does the batch size change that number?
Take AlexNet's third convolution: 384 filters, each looking at 256 input channels through a 3 × 3 window. One filter is a small brick of numbers, 256 deep and 3 × 3 across, so it holds 256 · 9 = 2,304 weights. There are 384 such bricks, so the layer holds 384 · 2,304 = 884,736 weights. That is the whole convolution formula for the #Parameters, read off the shape of the weight tensor.
Notice what is missing. The input is 256 × 13 × 13 and the output is 384 × 13 × 13, yet 13 appears nowhere in the count. A convolution slides the same brick to every output position, so the weights are shared across space. Goodfellow, Bengio and Courville make the point with an edge detector: a two-element kernel applied to a 320 × 280 image stores two parameters, where a dense matrix doing the same job would store billions (Goodfellow et al., section 9.2). The kernel size and the channel counts set the parameter count; the output height and width do not. Part 04 will show that MACs do multiply by h_o · w_o, because the brick is reapplied at every position, and that is the single most important difference between the two metrics.
Grouped convolution: derive it, do not memorize it
A grouped convolution with g groups cuts the c_i input channels into g slices of c_i/g and the c_o output channels into g slices of c_o/g, then runs g ordinary convolutions side by side, each reading only its own input slice and writing only its own output slice. So count one group with the formula you already have: (c_o/g) · (c_i/g) · k_h · k_w. There are g groups, so multiply by g. One factor of g cancels.
This matches what a framework actually stores. PyTorch gives Conv2d a weight of shape (out_channels, in_channels / groups, kH, kW) and requires that in_channels and out_channels are both divisible by groups (PyTorch documentation). The historical reason AlexNet has g = 2 in three of its layers is hardware, not elegance: the network was trained across two GTX 580 GPUs with 3 GB each, and "the kernels of the second, fourth, and fifth convolutional layers are connected only to those kernel maps in the previous layer which reside on the same GPU" (Krizhevsky et al., 2012, section 3.5). Grouping halved the weights each GPU had to hold.
Depthwise convolution: the limit g = c_i = c_o
Push the grouping to its extreme, one channel per group, and you get a depthwise convolution. Now g = c_i = c_o and every channel has its own private k_h × k_w filter. In the MobileNets paper this is stated directly: "the depthwise convolution applies a single filter to each input channel" (Howard et al., 2017, section 3.1). Plug g = c_i into the grouped formula and the c_i cancels.
Worked example
One 3 × 3 layer with 256 input and 256 output channels
Standard convolution
256 · 256 · 3 · 3 = 589,824 weights.Grouped with g = 2
589,824 / 2 = 294,912. Each of the two groups is a 128 → 128 convolution with 147,456 weights.Grouped with g = 8
589,824 / 8 = 73,728. Eight groups of 32 → 32.Depthwise, g = 256
256 · 9 = 2,304. Each channel keeps one 3 × 3 filter.Ratio to the standard layer
Layer Count #Parameters Ratio Standard, g = 1 256 · 256 · 9 589,824 1 Grouped, g = 2 256 · 256 · 9 / 2 294,912 1/2 Grouped, g = 8 256 · 256 · 9 / 8 73,728 1/8 Depthwise, g = 256 256 · 9 2,304 1/256
| Layer | #Parameters | Depends on h, w? | Depends on n? | Why |
|---|---|---|---|---|
| Linear | c_o · c_i | Only through c_i after a flatten | No | Every input feature is wired to every output feature; a flattened c_i carries h × w |
| Convolution | c_o · c_i · k_h · k_w | No | No | c_o filters, each c_i deep and k_h × k_w wide |
| Grouped convolution | c_o · c_i · k_h · k_w / g | No | No | g independent convolutions on c_i/g and c_o/g channels |
| Depthwise convolution | c_o · k_h · k_w | No | No | Grouped with g = c_i = c_o, one filter per channel |
The simulator collects all four formulas, adds the bit width from the final concept of this part and the MACs from part 04, and carries a preset for every parameterized AlexNet layer. Try conv3 now and confirm 884,736, then change h_o and watch which readouts move.
Change h_o or w_o and watch MACs move while the parameter count and model size stay still. Only c_i, c_o, k_h, k_w and g touch the weight count, and only the bit width turns that count into storage.
Quick check
A 3 × 3 convolution has c_i = 64 and c_o = 128. How many parameters does it have?
Quick check
Doubling the input resolution of a CNN does what to a convolution layer's parameter count and MAC count?
Recall
Write the grouped-convolution count two ways and say why the division is by g, not g squared.
AlexNet is the convolutional network whose variant won the ILSVRC-2012 challenge with a top-5 test error of 15.3%, and its authors state up front that it has "60 million parameters and 650,000 neurons" (Krizhevsky et al., 2012). It is also the best possible exercise for this part: five convolutions, three of them grouped, three max-pools and three linear layers, with input 3 × 224 × 224. Walk it top to bottom and apply the right #Parameters formula at each layer. The output shapes come from lecture 02; here we only count.
Worked example
Counting AlexNet, part one: the convolutions
conv1: 11 × 11, 96 channels, stride 4, pad 2
Output 96 × 55 × 55. Weights 96 · 3 · 11 · 11 = 34,848. The stride and padding set the 55, but they do not touch the count.maxpool 3 × 3, stride 2
Output 96 × 27 × 27. Pooling takes a maximum; it learns nothing and stores nothing: 0 parameters.conv2: 5 × 5, 256 channels, pad 2, groups 2
Output 256 × 27 × 27. Grouped, so 256 · 96 · 5 · 5 / 2 = 307,200.maxpool 3 × 3, stride 2
Output 256 × 13 × 13, 0 parameters.conv3: 3 × 3, 384 channels, pad 1
Output 384 × 13 × 13. 384 · 256 · 3 · 3 = 884,736.conv4: 3 × 3, 384 channels, pad 1, groups 2
Output 384 × 13 × 13. 384 · 384 · 3 · 3 / 2 = 663,552.conv5: 3 × 3, 256 channels, pad 1, groups 2
Output 256 × 13 × 13. The slide hides this one behind a question mark. Compute it before you reveal the answer below.
Recall
conv5: 3 × 3 kernels, 256 output channels, 384 input channels, groups 2. How many parameters?
Worked example
Counting AlexNet, part two: the linear layers and the total
maxpool 3 × 3, stride 2
Output 256 × 6 × 6, 0 parameters. This map is flattened into a vector of 256 · 6 · 6 = 9,216 values.fc6: linear, 4096 outputs
4096 · 9,216 = 37,748,736. One layer, and already more than half of the network.fc7: linear, 4096 outputs
4096 · 4096 = 16,777,216.fc8: linear, 1000 outputs
1000 · 4096 = 4,096,000, one output per ImageNet class.Total
2,332,704 in the convolutions plus 58,621,952 in the linear layers gives 60,954,656, about 61M, which is what the slide and the paper both round to.
| Layer | Output C × H × W | Count | #Parameters | Share |
|---|---|---|---|---|
| Image | 3 × 224 × 224 | 0 | ||
| conv1, 11 × 11, 96 ch, stride 4, pad 2 | 96 × 55 × 55 | 96 · 3 · 11 · 11 | 34,848 | 0.06% |
| maxpool 3 × 3, stride 2 | 96 × 27 × 27 | 0 | ||
| conv2, 5 × 5, 256 ch, pad 2, groups 2 | 256 × 27 × 27 | 256 · 96 · 5 · 5 / 2 | 307,200 | 0.50% |
| maxpool 3 × 3, stride 2 | 256 × 13 × 13 | 0 | ||
| conv3, 3 × 3, 384 ch, pad 1 | 384 × 13 × 13 | 384 · 256 · 3 · 3 | 884,736 | 1.45% |
| conv4, 3 × 3, 384 ch, pad 1, groups 2 | 384 × 13 × 13 | 384 · 384 · 3 · 3 / 2 | 663,552 | 1.09% |
| conv5, 3 × 3, 256 ch, pad 1, groups 2 | 256 × 13 × 13 | 256 · 384 · 3 · 3 / 2 | 442,368 | 0.73% |
| maxpool 3 × 3, stride 2 | 256 × 6 × 6 | 0 | ||
| fc6, linear 4096 | 4096 | 4096 · (256 · 6 · 6) | 37,748,736 | 61.93% |
| fc7, linear 4096 | 4096 | 4096 · 4096 | 16,777,216 | 27.52% |
| fc8, linear 1000 | 1000 | 1000 · 4096 | 4,096,000 | 6.72% |
| Five convolutions | 2,332,704 | 3.83% | ||
| Three linear layers | 58,621,952 | 96.17% | ||
| Total | 60,954,656 | 100% |
Where the weights actually are
The share column is the real lesson. The five convolutions together hold 2.3M weights, under 4 percent. The three linear layers hold 58.6M, about 96 percent, and fc6 alone holds 62 percent. Krizhevsky and colleagues knew this: a footnote in the paper says that "most of the net's parameters are in the first fully-connected layer" (Krizhevsky et al., 2012). Every input of fc6 is wired to every one of its 4,096 outputs, and it has 9,216 inputs, so the dense wiring the convolutions avoided comes back in one enormous matrix.
There is a subtle exception hiding in that 9,216. The previous concept said parameter counts do not depend on input resolution, and for every convolution that is true. But fc6 takes the flattened 256 × 6 × 6 map as its c_i, and the 6 × 6 came from 224 × 224 through the strides and pools. Feed AlexNet a larger image and fc6 would need a larger weight matrix. The first linear layer after a flatten is the one place where resolution leaks into the parameter count. Global average pooling, introduced in Network in Network, removes that leak: it averages each channel to one number, and "there is no parameter to optimize" in it (Lin, Chen and Yan, 2014), so the classifier input no longer grows with the image.
This table is the starting line for two results you will study later. Han and colleagues pruned AlexNet "from 61 million to 6.7 million" parameters with no loss of accuracy, a 9x reduction (Han et al., 2015), and Deep Compression then cut its storage "from 240MB to 6.9MB" (Han, Mao and Dally, 2016). Both papers attack exactly the layers this table flags: when 96 percent of the weights sit in three linear layers, that is where pruning pays.
Quick check
In AlexNet, which group of layers holds most of the 61M parameters?
Recall
Which AlexNet layers hold about 96 percent of the parameters, and why is fc6 so large?
A parameter count is a number of boxes. To know how much storage the boxes take, you need the size of each box. Store AlexNet's 61M weights as 32-bit floating point and each weight takes 4 bytes, so the whole network takes 61M × 4 B = 244 MB. Store the same weights as 8-bitintegers and the same network takes 61 MB. Nothing about the architecture changed; only the bit width did.
That is the definition of model size: the storage needed for the weights of the network, the #Parameters times the bits each one occupies. The slide lists MB, KB and bits as the common units, and the formula as written produces bits. To reach bytes divide by 8, and to reach megabytes divide by another 10^6. The slide is explicit that its megabyte is decimal, 244 × 10^6 bytes. For a single layer, kilobytes are the natural unit: AlexNet's conv3 holds 884,736 weights, which at 8 bits is 884,736 bytes, or 884.7 KB (divide bytes by 10^3), and at 32 bits 3,538.9 KB.
| Data type | Size in bits | Size (decimal MB) | Slide rounding |
|---|---|---|---|
| 32-bit float (fp32) | 1,950.5 Mbit | 243.8 MB | 244 MB |
| 16-bit (fp16 or bfloat16) | 975.3 Mbit | 121.9 MB | 122 MB |
| 8-bit integer (int8) | 487.6 Mbit | 61.0 MB | 61 MB |
| 4-bit integer (int4) | 243.8 Mbit | 30.5 MB | 30.5 MB |
| 1-bit (binary weights) | 61.0 Mbit | 7.6 MB | 7.6 MB |
The formula assumes one data type for the whole network. Mixed-precision models break that assumption, and then the size is a sum over layers, each layer's count times its own bit width. That is the form you need when, for example, the first and last layers stay at 8 bits while the middle is pushed to 4.
Why the multiplication matters for this course
The parameter count is fixed once the architecture is chosen, but the bit width is a knob. The lecture on quantization turns that knob, and this formula says exactly what it buys: every halving of the bit width halves the model size with no change to the architecture. Deep Compression combined pruning, quantization and Huffman coding to take AlexNet "from 240MB to 6.9MB" (Han, Mao and Dally, 2016); their 240 MB starting point is, up to rounding, the 244 MB computed in this concept. Part 03 shows a microcontroller SRAM budget of 256 kB (the MCU constraint, a budget for activations). The slides do not give a flash size, but a typical MCU of this class, such as the STM32F746 used by MCUNet (Lin et al., NeurIPS 2020), has about 1 MB of flash, where the weights would live; even 8-bitAlexNet at 61 MB is about 60 times too large for that flash, which is why edge deployment starts with counting.
Quick check
A 10M-parameter model stored entirely in 16-bit numbers occupies how much storage?
Recall
61M parameters at 8 bits: what is the model size in MB, and in bits?
Recap
If you remember nothing else
- #Parameters is the number of elements in the weight tensors of the whole network. Bias is ignored in this lecture.
- Linear c_o · c_i. Convolution c_o · c_i · k_h · k_w. Grouped divides that by g. Depthwise is c_o · k_h · k_w.
- Parameter count ignores batch size and spatial size. MACs (part 04) do not, because every filter is reapplied at every output position.
- AlexNet has 60,954,656 parameters, about 61M. conv1 is 34,848 (the slide prints 24,848) and the hidden conv5 is 442,368.
- The three linear layers hold about 96 percent (58.6M). fc6 alone is 37.7M because its input is the flattened 256 × 6 × 6 map.
- Pooling layers have zero parameters.
- Model size = #Parameters × bit width. 61M weights are 244 MB at 32-bit, 122 MB at 16-bit and 61 MB at 8-bit, in decimal MB.
Sources
- ImageNet Classification with Deep Convolutional Neural NetworksPaperNeurIPS 2012, Krizhevsky, Sutskever and Hinton60 million parameters, per-layer kernel shapes, same-GPU connectivity behind groups = 2, first fully connected layer holds most parameters.(opens in a new tab)
- torch.nn.Conv2dDocsPyTorch documentationWeight shape (out_channels, in_channels / groups, kH, kW), divisibility by groups, depthwise as groups = in_channels, bias shape.(opens in a new tab)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, Fall 2024DocsMIT HAN Lab, Song HanLecture 2, Basics of Deep Learning, is the source deck for slides 10 to 17 including the conv1 typo.(opens in a new tab)
- Deep Compression: Compressing Deep Neural Networks with Pruning, Trained Quantization and Huffman CodingPaperICLR 2016, Han, Mao and DallyAlexNet storage reduced 35x, from 240 MB to 6.9 MB.(opens in a new tab)
- Learning both Weights and Connections for Efficient Neural NetworksPaperNeurIPS 2015, Han, Pool, Tran and DallyAlexNet pruned 9x, from 61 million to 6.7 million parameters.(opens in a new tab)
- MobileNets: Efficient Convolutional Neural Networks for Mobile Vision ApplicationsPaperarXiv 2017, Howard et al.Depthwise convolution applies a single filter per input channel; Table 2 gives parameter shares of 1 x 1, depthwise and linear layers.(opens in a new tab)
- Prefixes for binary multiplesDocsNIST1 MB = 10^6 bytes, 1 MiB = 2^20 bytes.(opens in a new tab)
- torchvision.models.alexnetDocsPyTorch documentation61,100,840 parameters; implementation follows the One weird trick variant, not the 2012 paper.(opens in a new tab)
- Deep Learning, chapter 9: Convolutional NetworksBookMIT Press, Goodfellow, Bengio and CourvilleSection 9.2, parameter sharing: a two-parameter edge kernel versus a dense matrix with billions of entries.(opens in a new tab)
- MCUNet: Tiny Deep Learning on IoT DevicesPaperNeurIPS 2020, Lin et al.Runs TinyNAS networks on the STM32F746, a microcontroller with 320 kB SRAM and 1 MB flash.(opens in a new tab)
- Network In NetworkPaperICLR 2014, Lin, Chen and YanGlobal average pooling replaces fully connected classifier layers and has no parameters to optimize.(opens in a new tab)