Batch Normalization During Training

Batch A · two images · one channelStep 1 of 2

Feature activations

Image 11.0003.000Image 25.0007.000

Labels: y1=0,y2=1y_1=0,\quad y_2=1

BatchNorm

μbatch=4,σbatch2=5\mu_{\mathrm{batch}}=4,\quad\sigma_{\mathrm{batch}}^2=5

−2.183−0.3941.3943.183

γ=2.000,β=0.500\gamma=2.000,\quad\beta=0.500

Classifier

ReLU → average per image → linear

ImageClass 0Class 1
150.0%50.0%
283.2%16.8%

Softmax probabilities

Mean loss

1.239

L=L1+L22L=\frac{L_1+L_2}{2}

Two image losses

←Backpropagation←
ParameterBeforeGradientAfter SGD
γ\gamma2.0000.2611.974
β\beta0.5000.2910.471
[w0,w1][w_0,w_1][0.400, −0.300][0.953, −0.953][0.305, −0.205]
[b0,b1][b_0,b_1][0.000, 0.000][0.166, −0.166][−0.017, 0.017]

Parameter gradients

θnew=θ−η∂L∂θ\theta_{\mathrm{new}}=\theta-\eta\frac{\partial L}{\partial\theta}

η=0.1\eta=0.1

θ\theta: scale, offset and classifier parameters

γnew=2.000−0.1×0.261≈1.974\gamma_{\mathrm{new}}=2.000-0.1\times0.261\approx1.974

← Return to lecture

Batch Normalization: Training and Inference

TRAINING · FORWARD PASSThe same forward pass performs steps 1 and 2
1. Normalize the current batch2. Update the saved statistics3. Use the saved statistics

Batch A · one channel

Image 113
Image 257

Two images · two values each

Current-batch statistics

μbatch=4\mu_{\mathrm{batch}}=4

σbatch2=5\sigma^2_{\mathrm{batch}}=5

Calculate from this batch.

Normalized activations

Image 1−1.342−0.447
Image 20.4471.342

Before scale and offset

a^i=ai−μbatchσbatch2+εa^1=1−45+10−5≈−1.342\hat a_i=\frac{a_i-\mu_{\mathrm{batch}}}{\sqrt{\sigma^2_{\mathrm{batch}}+\varepsilon}}\qquad\hat a_1=\frac{1-4}{\sqrt{5+10^{-5}}}\approx-1.342

Training normalizes with the current batch’s mean and variance.

← Return to lecture

Weight Update without L2

Follow one weight ww. Start at w=2w=2.

Backpropagation gives the gradient

g=∂Ldata∂w=0.4g=\frac{\partial L_{\mathrm{data}}}{\partial w}=0.4

SGD applies the update

Learning rate: η=0.1\eta=0.1

wnew=w−ηgw_{\mathrm{new}}=w-\eta g

wnew=2−0.1(0.4)=1.96w_{\mathrm{new}}=2-0.1(0.4)=\color{#087fbc}{1.96}

Weight value1.81.92.0Start: 2−0.041.96

SGD moves against the gradient. The learning rate scales the update.

← Return to lecture

L2 Penalty and Gradient

Add the same squared-weight penalty. Here, we follow one weight.

J(w)=Ldata(w)⏟data loss+λ2w2⏟L2 penaltyJ(w)=\underbrace{L_{\mathrm{data}}(w)}_{\text{data loss}}+\underbrace{\frac{\lambda}{2}w^2}_{\text{L2 penalty}}

λ≥0\lambda\ge0 sets the penalty strength.

Data-loss gradient

∂Ldata∂w=g\frac{\partial L_{\mathrm{data}}}{\partial w}=\color{#087fbc}{g}

Penalty gradient

∂∂w(λ2w2)=λw\frac{\partial}{\partial w}\left(\frac{\lambda}{2}w^2\right)=\color{#bd6500}{\lambda w}

∂J∂w=g+λw\frac{\partial J}{\partial w}=\color{#087fbc}{g}+\color{#bd6500}{\lambda w}

wnew=w  −ηg⏟data-loss update−ηλw⏟L2 contributionw_{\mathrm{new}}=w\;\underbrace{\color{#087fbc}{-\eta g}}_{\text{data-loss update}}\quad\underbrace{\color{#bd6500}{-\eta\lambda w}}_{\text{L2 contribution}}

The L2 contribution points toward zero and grows with ∣w∣|w|.

← Return to lecture

L2 Regularization: Numerical Example

w=2w=2current weightg=0.4g=0.4data-loss gradientη=0.1\eta=0.1learning rateλ=0.5\lambda=0.5penalty strength
Without L21.81.92.0Start: 2−0.041.96With L21.81.92.0Start: 2−0.04−0.101.86

Without L2:2−0.04=1.96\text{Without L2:}\quad 2-\color{#087fbc}{0.04}=\color{#087fbc}{1.96}

ηλw=0.1×0.5×2=0.10\eta\lambda w=0.1\times0.5\times2=\color{#bd6500}{0.10}

With L2:2−0.04−0.10=1.86\text{With L2:}\quad 2-\color{#087fbc}{0.04}-\color{#bd6500}{0.10}=\color{#bd6500}{1.86}

Same starting weight. Same data gradient. L2 contributes an extra −0.10.

← Return to lecture

Dropout

Training uses different subsets of activations.

ii: activation index
1234
Input activation
hih_i
2468
Mask mim_i1: keep · 0: drop
1010
Output activation
h~i\tilde{h}_i
40120

Output activation

h~i=mihi1−p\tilde{h}_i=\frac{m_i h_i}{1-p}

1−p1-p: keep probability

Kept: 2/0.5=42/0.5=4

Dropped: 0/0.5=00/0.5=0

Why divide by the keep probability?

An input of 4 becomes 0 or 8, each with 50% chance.

0.5×0+0.5×8=40.5\times0+0.5\times8=4

← Return to lecture

Soft training targets

Soft targets distribute probability across classes.

qk=(1−ε)yk+εKq_k=(1-\varepsilon)y_k+\frac{\varepsilon}{K}

Keep the image; soften its one-hot target.

K=10,ε=0.10K=10,\quad\varepsilon=0.10

Training target

0.91+9×0.01=1.000.91+9\times0.01=1.00

Label smoothing and mixing replace one-hot training targets.

← Return to lecture

GoogLeNet

GoogLeNet · 22 weighted layers on a longest path · auxiliary heads are used only for training

Convolution+ pooling2 Inceptionmodules5 Inceptionmodules2 Inceptionmodules28 × 2814 × 147 × 71,024 feature maps
7×7×1,0247\times7\times1,024
Global averagepooling
1,024 valuesLinear1,000 scores

Average each feature map, then classify: avoid VGG’s large flattened-input FC head.

Efficient depth is one challenge. Next: can we make deeper networks easier to train?

← Return to lecture

Batch Normalization: Mean and Variance

Compute both statistics from the original activations in one channel.

One feature channel

Current batch B\mathcal{B}

Image 1a1=1a_1=1a2=3a_2=3 Image 2a3=5a_3=5a4=7a_4=7

m=2×1×2=4m=2\times1\times2=4

images × height × width

aia_i: an original activation
mm: number of values in this channel

1. Compute the mean

μB=1m∑i=1mai\mu_{\mathcal{B}}=\frac{1}{m}\sum_{i=1}^{m}a_i

μB=1+3+5+74=4\mu_{\mathcal{B}}=\frac{1+3+5+7}{4}=4

2. Compute the variance

σB2=1m∑i=1m(ai−μB)2\sigma_{\mathcal{B}}^2=\frac{1}{m}\sum_{i=1}^{m}(a_i-\mu_{\mathcal{B}})^2

Square each distance from the mean, then average.

σB2=(1−4)2+(3−4)2+(5−4)2+(7−4)24=5\sigma_{\mathcal{B}}^2=\frac{(1-4)^2+(3-4)^2+(5-4)^2+(7-4)^2}{4}=5

μB=4,σB2=5\mu_{\mathcal{B}}=4,\qquad\sigma_{\mathcal{B}}^2=5

← Return to lecture

Batch Normalization: Normalize the Activations

Use the mean and variance just computed for this channel.

Original activations aia_i

Image 113Image 257

μB=4,σB2=5\mu_{\mathcal{B}}=4,\quad\sigma_{\mathcal{B}}^2=5

Shared statistics within this channel.

ε=10−5\varepsilon=10^{-5} keeps the denominator nonzero.

3. Normalize

a^i=ai−μBσB2+ε\hat{a}_i=\frac{a_i-\mu_{\mathcal{B}}}{\sqrt{\sigma_{\mathcal{B}}^2+\varepsilon}}

Subtract the mean.
Divide by the standard deviation
with ε\varepsilon for stability.

For the first activation, a1=1a_1=1:

a^1=1−45+10−5≈−1.342\hat{a}_1=\frac{1-4}{\sqrt{5+10^{-5}}}\approx-1.342

Normalized activations a^i\hat{a}_i

Image 1−1.342−0.447Image 20.4471.342

Mean=0\text{Mean}=0
Variance≈1\text{Variance}\approx1

← Return to lecture

Batch Normalization: Scale and Offset

Learn one scale γ\gamma and one offset β\beta per channel.

Normalized values a^i\hat{a}_i

Input to the affine step

−1.342−0.4470.4471.342

Multiply by the scale

Example: γ=2\gamma = 2

−2.683−0.8940.8942.683

γ⋅a^i=2×a^i\gamma \cdot \hat{a}_i = 2 \times \hat{a}_i

Add the offset

Example: β=0.5\beta = 0.5

−2.183−0.3941.3943.183

aout,i=γ⋅a^i+βa_{\mathrm{out},i} = \gamma \cdot \hat{a}_i + \beta

Output mean=0.5\text{Output mean}=0.5; variance≈4\text{variance}\approx4

Loss → backpropagation → update γ\gamma and β\beta

← Return to lecture

ResNet-18 and ResNet-50

224×224224\times224 input · repeat a residual block within each spatial stage.

ResNet-18: basic block
64→64→64 channels64\to64\to64\ \text{channels}
3×33\times3
64 channels
3×33\times3
64 channels
++
ResNet-50: bottleneck
256→64→64→256 channels256\to64\to64\to256\ \text{channels}
1×11\times1
reduce
3×33\times3
filter
1×11\times1
restore
++
Four stages
56×5656\times56
18: 2 blocks50: 3 blocks
28×2828\times28
18: 2 blocks50: 4 blocks
14×1414\times14
18: 2 blocks50: 6 blocks
7×77\times7
18: 2 blocks50: 3 blocks
One shape-matching choice:y=F(x)+Wsx\mathbf{y}=F(\mathbf{x})+W_s\mathbf{x}WsW_s: a projection matches the shape.
← Return to lecture

Depthwise-Separable Convolution

Factor spatial filtering from channel mixing · one spatial filter per input channel

InputDepthwisePointwiseOutputchannel 1channel 2channel 3
3×33\times3
3×33\times3
3×33\times3
One filter per input channel
1×1 filters1\times1\ \text{filters}
∑\sum
∑\sum
Weighted channel sums
output channel 1output channel 2
← Return to lecture

MobileNet: Computation and Accuracy

One layer
28×28×32 input ⟶ 28×28×64 output28\times28\times32\ \text{input}\ \longrightarrow\ 28\times28\times64\ \text{output}
Standard convolution
32×32×64=18,4323^2\times32\times64=18{,}432
weights
Depthwise + pointwise
32×32+32×64=2,3363^2\times32+32\times64=2{,}336
weights
MACs=weights×28×28\text{MACs}=\text{weights}\times28\times28
14.45 M ⟶ 1.83 M14.45\,\text{M}\ \longrightarrow\ 1.83\,\text{M}
7.9× fewer7.9\times\ \text{fewer}
Published MobileNet comparisonImageNet · top-1 accuracy
224×224 input224\times224\ \text{input}
Full-convolution MobileNet71.7%4,866 M29.3 MAccuracyMult-AddsParameters
Depthwise-separable MobileNet70.6%569 M4.2 M
← Return to lecture

MobileNetV2: Inverted Residuals

Same shape · stride 1ReLU6 clips values to the range 0–6.32 channelsExpand
1×11\times1
then ReLU6192 channels
Depthwise
3×33\times3
then ReLU6192 channels
Project
1×11\times1
no ReLU32 channels
Shortcut joins the narrow representations
++
32 channels
Why no ReLU after projection?
(−2, 1) →ReLU (0, 1)(-2,\,1)\ \xrightarrow{\mathrm{ReLU}}\ (0,\,1)
(−1, 1) →ReLU (0, 1)(-1,\,1)\ \xrightarrow{\mathrm{ReLU}}\ (0,\,1)
Different inputs.Same output.
← Return to lecture

EfficientNet-B0

Searched B0 baseline: MBConv + SE channel gating · compound scaling comes next

Network structure
224×224 input224\times224\ \text{input}
StemMBConvMBConvMBConvMBConvMBConvMBConvMBConvSpatial size
112→56→28→14→7112\to56\to28\to14\to7
Channels
16→⋯→32016\to\cdots\to320
Head
Head: 1×1 convolution → global average pooling → classifier\text{Head: }1\times1\ \text{convolution}\ \to\ \text{global average pooling}\ \to\ \text{classifier}
MBConv: expand → depthwise → channel gate → project\text{MBConv: expand}\ \to\ \text{depthwise}\ \to\ \text{channel gate}\ \to\ \text{project}
Channel gatingGlobal meanper channel0.20.9Learned gate networkRescale eachfeature map
← Return to lecture

Compound Scaling

Scale an existing networkPublished ImageNet top-1 accuracyDepthWidthResolutionCompound: change all three together
compute ∝ d w2r2\text{compute}\ \propto\ d\,w^2r^2
d: depthw: widthr: resolutiond\text{: depth}\quad w\text{: width}\quad r\text{: resolution}
70727476788076.8%Depth1.2 B76.4%Width1.1 B74.8%Resolution1.2 B77.4%Compound1.3 BMobileNetV2 scaled four waysReported FLOPs per image
← Return to lecture

A Two-Class Classification Problem

80 training examples

-1012-101
x1x_1
x2x_2
Class 0Class 1Each point is one example.

Two numerical inputs

Predict class 0 or class 1.

Synthetic two-moons data

A small neural network

(x1,x2)(x_1,x_2)2 inputs
→
20tanh units
→
ppsigmoid

p=P(class 1∣x1,x2)p=P(\text{class }1\mid x_1,x_2)

80 training points
Learn the parameters.

600 validation points
Measure performance on new points.

After training

p≥0.5p\ge0.5: predict class 1.
p<0.5p<0.5: predict class 0.

The boundary is where p=0.5p=0.5.

← Return to lecture

Regularization Strength

Fit the training labels

J=LdataJ=L_{\mathrm{data}}

JJ: objective minimized during training

LdataL_{\mathrm{data}}: mean training binary cross-entropy

Add a weight penalty

R=12∑jwj2R=\frac12\sum_j w_j^2

wjw_j: one network weight

RR: half the sum of squared weights

λ\lambda: penalty strength

Chosen before training.
Larger values give the penalty more weight.

η\eta: learning rate

Controls the size of each update.

Train three models separately

λ=0\lambda=0No penalty
λ=0.003\lambda=0.003Moderate penalty
λ=0.3\lambda=0.3Strong penalty

Same data and starting parameters · only λ\lambda changes

Full-batch gradient descent · η=0.1\eta=0.1 · 12,000 updates

← Return to lecture

One Weight Update, with and without L2

w=2w=2current weightg=0.4g=0.4data-loss gradientη=0.1\eta=0.1learning rateλ=0.5\lambda=0.5penalty strength
J=Ldata+λ2w2J=L_{\mathrm{data}}+\frac{\lambda}{2}w^2 → ∂J∂w=g+λw\frac{\partial J}{\partial w}=g+\lambda w
Without L21.81.92.0Start: 2−0.041.96With L21.81.92.0Start: 2−0.04−0.101.86

Without L2:2−0.04=1.96\text{Without L2:}\quad 2-\color{#087fbc}{0.04}=\color{#087fbc}{1.96}

ηλw=0.1×0.5×2=0.10\eta\lambda w=0.1\times0.5\times2=\color{#bd6500}{0.10}

With L2:2−0.04−0.10=1.86\text{With L2:}\quad 2-\color{#087fbc}{0.04}-\color{#bd6500}{0.10}=\color{#bd6500}{1.86}

Same starting weight. Same data gradient. L2 contributes an extra −0.10.

Optional walkthroughDerive the SGD and L2 updates ↗
← Return to lecture

DeiT: Training Recipe and Distillation

DeiT-B · published ImageNet results · 300-epoch comparison

ImageNet-only training

AdamW · Mixup · CutMix

Label smoothing · stochastic depth

Same basic ViT encoder

DeiT-B ImageNet top-1 accuracy at 300 training epochs: undistilled 81.8 percent at 224 and 83.1 at 384; distilled 83.4 and 84.5 percent respectively.
Shared encoderpatches + two tokensClass headDistillation headCE lossCE lossTrue labelTeacher labelCNN prediction

A strong recipe already helps. The optional teacher adds supervision during training.

← Return to lecture

From Architecture to Evaluation

The current course measurements compare the MLP and small CNN; ViT remains unmeasured.

BackboneMLP · CNN · ViTImage representationTask-specific headWhat evidence would justify this choice?DataInitializationTraining recipeMeasured cost

Next: compare the architectures, then examine the existing CIFAR-10 validation evidence.

← Return to lecture

Comparing MLPs, CNNs and ViTs

The main distinction is how image positions exchange information.

FamilyRepresentationSpatial interactionBuilt-in structure
MLPFlattened pixelsDense connections across positionsPosition-specific weights
CNNSpatial feature mapsLocal filters shared across the imageLocality and translation structure
ViTPatch tokens and positionsAttention weights depend on token contentPatch structure and shared token operations

CNN depth expands context. ViT attention can connect all patches in each block.

Compare trained recipes on the task and device that matter.

← Return to lecture

Averaging Across Classes

Macro averages weight classes equally.

AverageWhat receives equal weight?PrecisionRecallF1
macroEach class75.6%75.6%75.5%
weightedEach image through class support75.6%75.6%75.5%
microAll classification decisions pooled75.6%75.6%75.6%

Equal class support makes macro and weighted averages identical here.

Single-label classification, all classes:

micro precision=micro recall=micro F1=accuracy\text{micro precision}=\text{micro recall}=\text{micro }F_1=\text{accuracy}

CIFAR-10 · 5,000 validation images · CNN

Macro F1 averages the per-class F1 scores.

← Return to lecture

Top-k Accuracy

CIFAR-10 · 5,000 validation images · CNN

Correct if the true class is in the selected shortlist.

← Return to lecture

Thresholds, Precision–Recall and ROC

predict class-positive if p(class)≥τp(\text{class})\ge\tau

CIFAR-10 · 5,000 validation images · CNN · one versus rest

Lower thresholds can increase recall and false positives.

← Return to lecture

Calibration

CIFAR-10 · 5,000 validation images · CNN

Calibrated confidence matches observed accuracy.

← Return to lecture

Dropout and Test Accuracy

CIFAR-10 · 50,000 training images · 10,000 test images

3-layer CNNfeature extractor64-unit hidden layer50% dropout during trainingClassifier10 classes

Same architecture · 150 epochs · no data augmentation

Test accuracy0%20%40%60%80%100%Without dropout76.5%With dropout80.3%

+3.8 percentage points in test accuracy

Optional: dropout masks and scaling ↗
← Return to lecture