惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Security Archives - TechRepublic
Security Archives - TechRepublic
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
S
Security @ Cisco Blogs
S
Security Affairs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
Lohrmann on Cybersecurity
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
Forbes - Security
Forbes - Security
H
Heimdal Security Blog
A
Arctic Wolf
NISL@THU
NISL@THU
P
Proofpoint News Feed
W
WeLiveSecurity
S
Schneier on Security
AI
AI
Schneier on Security
Schneier on Security
N
News and Events Feed by Topic
L
LINUX DO - 最新话题
Cisco Talos Blog
Cisco Talos Blog
AWS News Blog
AWS News Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Know Your Adversary
Know Your Adversary
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
Cyberwarzone
Cyberwarzone
I
Intezer
S
Securelist
Help Net Security
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
小众软件
小众软件
Last Week in AI
Last Week in AI
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
罗磊的独立博客
月光博客
月光博客
雷峰网
雷峰网
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 司徒正美
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events

Louis C Deng's Blog

CS231n Lecture Note: Generative Models CS231n Lecture Note: Self-Supervised Learning CS231n Lecture Note: Large Scale Distributed Training 自動微分 | DIY 實現自己的 PyTorch From RNNs to Transformers CS231n Lecture Note VII: Recurrent Neural Networks Uncovering Batch & Layer Normalization CS231n Lecture Note VI: CNN Architectures and Training CS231n Lecture Note V: Convolution Neural Networks Basics Demystifying Softmax Loss: A Step-by-Step Derivation for Linear Classifiers Backpropagation: A Vector Calculus Perspective CS231n Lecture Note III: Optimization CS231n Lecture Note II: Linear Classifiers CS231n Lecture Note I: Image Classification CSAPP Cache Lab II: Optimizing Matrix Transposition CSAPP Cache Lab I: Let's simulate a cache memory! CS188 Search Lecture Notes III CS188 Search Lecture Notes II How to Use TouchID for Sudo Commands on macOS CS188 Search Lecture Notes I RECAP2025: 留白 CSAPP Bomb Lab 解析 x64 暫存器速查表 CSAPP Data Lab 解析 矩陣的 Modified Gram Schmidt 方法 聊一聊位掩碼(Bit Mask) 整數溢位與未定義行為 快速排序 幾種劃分方法討論 等待 記夢(DeepSeek 輔助創作) 午夜飛行 橋樑 黎明 或 2012 RECAP2024: 水檻臥聽雨 太陽、潮落 RECAP2023: 泡沫 題解 P1622 釋放囚犯 題解 P5888 傳球遊戲 殘陽似火 再會 飢餓藝術家 卡夫卡 Python 中的 zip() 和 enumerate() 泡沫 “救救孩子……”——談魯迅和《狂人日記》 想念 淺灘 蟬 · 夏 微風 觀星 浮塵 復活 【摘錄 | 轉載】普魯斯特 《追憶似水年華》第一卷 《在斯萬家那邊》(一) Time - Pink Floyd - The Dark Side of the Moon 【轉載】靜夜思變調 高樓 幻夢 冰 RECAP2022: 流星雨 清夜 割點 Tarjan 演算法 P3147 USACO16OPEN 262144 P 題解 P3354 Riv 河流 題解 馬拉車演算法 夜雨 層霧 從愚人節玩笑到真的玩笑(bushi): 淺談 lsnotes I made my own Hexo theme 題解 紀念品分組 題解 導彈攔截 如何高效使用搜尋引擎 用 GitHub Actions 格式化 C/C++ 程式碼 四季的天空 洛谷 7 月月賽 Div.2 總結 題解 最近公共祖先 (LCA) 用簡單的物理方法證明牛頓萊布尼茨公式 簡評榮耀手環6 海上生明月,天涯共此時。 我為什麼重新拿出了 iPod Swift 中的 SharedPreferance —— UserDefaults 凝視那一輪明月 用 GitHub Actions 部署 Hexo 部落格 遲來的日誌 - WWDC 2020 獎學金 vcpkg - 方便的 C/C++ 庫管理器 vimrc 配置指南 NextCloud - DIY NAS 解決方案 sudo shutdown -r now sudo shutdown -r now
CS231n Lecture Note IV: Neural Networks and Backpropagation
Louis C Deng · 2026-02-14 · via Louis C Deng's Blog

Here we are introducing Neural Networks and Backpropagation.

Neural Networks

We will start with a 2-layer example:

f=W2max⁡(0,W1x)f = W_2 \max(0, W_1 x)

x∈RD,W1∈RH×D,W2∈RC×Hx \in \mathbb{R}^D, W_1 \in \mathbb{R}^{H \times D}, W_2 \in \mathbb{R}^{C \times H}

The maxmax here creates some non-linearity between W1W1 and W2W2. It is called the activation function.

In practice, we will usually add a learnable bias at each layer as well.

Here’s an example of a 3-layer one.

f=W3max⁡(0,W2max⁡(0,W1x))f = W_3 \max(0, W_2 \max(0, W_1 x))

x∈RD,W1∈RH1×D,W2∈RH2×H1,W3∈RC×H2x \in \mathbb{R}^D, W_1 \in \mathbb{R}^{H_1 \times D}, W_2 \in \mathbb{R}^{H_2 \times H_1}, W_3 \in \mathbb{R}^{C \times H_2}

The networks we are talking about here are more accurately called fully-connected networks, or sometimes called multi-layer perceptrons (MLP).

Activation Functions

The most important characteristic of activation functions is to create some non-linearity.

There are multiple activation functions to choose from.

The function max(0,x)max(0,x) is called ReLU. It is a good default choice for most problems.

ReLU(x)=max⁡(0,x)\text{ReLU}(x) = \max(0, x)

We also have:

  • Leaky ReLU: max⁡(0.1x,x)\max(0.1x, x) — Allows a small gradient for negative inputs. Avoids dead neurons.
  • Sigmoid: σ(x)=11+e−x\sigma(x) = \frac{1}{1+e^{-x}} — S-shaped curve squashing output between 0 and 1.
  • Tanh: tanh⁡(x)\tanh(x) — S-shaped curve squashing output between -1 and 1.
  • ELU: Exponential Linear Unit.
  • GELU & SiLU: Newer functions often used in modern architectures like Transformers.

Choosing an activation function is very empirical.

Architecture

In a Neural Network, we have an input layer, an output layer, and several hidden layers.

Sample Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import numpy as np
from numpy.random import randn






N, D_in, H, D_out = 64, 1000, 100, 10
x, y = randn(N, D_in), randn(N, D_out)
w1, w2 = randn(D_in, H), randn(H, D_out)

for t in range(2000):

h = 1 / (1 + np.exp(-x.dot(w1)))
y_pred = h.dot(w2)
loss = np.square(y_pred - y).sum()
print(t, loss)


grad_y_pred = 2.0 * (y_pred - y)
grad_w2 = h.T.dot(grad_y_pred)
grad_h = grad_y_pred.dot(w2.T)
grad_w1 = x.T.dot(grad_h * h * (1 - h))


w1 -= 1e-4 * grad_w1
w2 -= 1e-4 * grad_w2

The number of neurson in the hidden layers is often related to the capacity of information. More neurons, more capacity.

A regularizer keeps the network simple and prevents overfitting. Do not use size of neural network as a regularizer. Use stronger regularization instead.

Derivatives

f(x,y)=x+y→∂f∂x=1,∂f∂y=1f(x, y) = x + y \to \frac{\partial f}{\partial x} = 1, \frac{\partial f}{\partial y} = 1

f(x,y)=max⁡(x,y)→∂f∂x=1(x≥y),∂f∂y=1(y≥x)f(x, y) = \max(x, y) \to \frac{\partial f}{\partial x} = \mathbb{1}(x \geq y), \frac{\partial f}{\partial y} = \mathbb{1}(y \geq x)

f(x,y)=xy→∂f∂x=y,∂f∂y=xf(x, y) = xy \to \frac{\partial f}{\partial x} = y, \frac{\partial f}{\partial y} = x

The derivative on each variable tells you the sensitivity of the whole expression on its value.

  • add gate: gradient distributor
  • mul gate: “swap multiplier”
  • copy gate: gradient adder
  • max gate: gradient router

Backpropagation

The problem now is to calculate the gradients in order to learn the parameters.

Obviously we can’t simply derive it on paper, for it can be very tedious and not feasible for complex models.

Here’s the better idea: Computational graphs + Backpropagation.

Backpropagation is generally chain rules.

Downstream=Upstream×Local\text{Downstream} = \text{Upstream} \times \text{Local}

∂L∂x=∂L∂z⋅∂z∂x\frac{\partial L}{\partial x} = \frac{\partial L}{\partial z} \cdot \frac{\partial z}{\partial x}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
================================================================================
COMPUTATIONAL GRAPH NODE: BACKPROPAGATION
================================================================================

FORWARD PASS
Computes output z from inputs x, y

[ Input x ] ---------------------\
\
\
v
_____________
| |
| Node f | -----------------> [ Output z ]
|_____________|
^
/
/
[ Input y ] ---------------------/


================================================================================

BACKWARD PASS
Chain Rule: Downstream = Upstream * Local

Upstream Gradient
(Received)
dL/dz
|
v
_____________ _____________
Downstream Gradient | | | |
(To x) | Local Grad | | |
dL/dx <----------------| dz/dx | <----------------| |
= |_____________| | |
dL/dz * dz/dx | |
| Node f |
| |
_____________ | |
Downstream Gradient | | | |
(To y) | Local Grad | | |
dL/dy <----------------| dz/dy | <----------------| |
= |_____________| |_____________|
dL/dz * dz/dy

Modularize: Forward/Backward API

Gate / Node / Function object: Actual PyTorch code

1
2
3
4
5
6
7
8
9
10
11
12
class Multiply(torch.autograd.Function):
@staticmethod
def forward(ctx, x, y):
ctx.save_for_backward(x, y)
z = x * y
return z
@staticmethod
def backward(ctx, grad_z):
x, y = ctx.saved_tensors
grad_x = y * grad_z
grad_y = x * grad_z
return grad_x, grad_y

Aside: Vector Derivatives

For vector derivatives:

  1. Vector to Scalar

x∈RN,y∈Rx \in \mathbb{R}^N, y \in \mathbb{R}

The derivative is a gradient.

∂y∂x∈RN(∂y∂x)n=∂y∂xn\frac{\partial y}{\partial x} \in \mathbb{R}^N \quad \left(\frac{\partial y}{\partial x}\right)_n = \frac{\partial y}{\partial x_n}

  1. Vector to Vector

x∈RN,y∈RMx \in \mathbb{R}^N, y \in \mathbb{R}^M

The derivative is a Jacobian Matrix.

∂y∂x∈RN×M(∂y∂x)n,m=∂ym∂xn\frac{\partial y}{\partial x} \in \mathbb{R}^{N \times M} \quad \left(\frac{\partial y}{\partial x}\right)_{n,m} = \frac{\partial y_m}{\partial x_n}

Aside: Jacobian Matrix

A Jacobian Matrix is the derivative for a function that maps a Vector to a Vector.

Definition

Let f:Rn→Rm\mathbf{f} : \mathbb{R}^n \to \mathbb{R}^m be a function such that each of its first-order partial derivatives exists on Rn\mathbb{R}^n. This function takes a point x=(x1,…,xn)∈Rn\mathbf{x} = (x_1, \ldots, x_n) \in \mathbb{R}^n as input and produces the vector f(x)=(f1(x),…,fm(x))∈Rm\mathbf{f}(\mathbf{x}) = (f_1(\mathbf{x}), \ldots, f_m(\mathbf{x})) \in \mathbb{R}^m as output.

Then the Jacobian matrix of f\mathbf{f}, denoted Jf\mathbf{J}_{\mathbf{f}}, is the m×nm \times n matrix whose (i,j)(i, j) entry is ∂fi∂xj\frac{\partial f_i}{\partial x_j}; explicitly:

Jf=[∂f∂x1⋯∂f∂xn]=[∇Tf1⋮∇Tfm]=[∂f1∂x1⋯∂f1∂xn⋮⋱⋮∂fm∂x1⋯∂fm∂xn]\mathbf{J}_{\mathbf{f}} = \begin{bmatrix} \frac{\partial \mathbf{f}}{\partial x_1} & \cdots & \frac{\partial \mathbf{f}}{\partial x_n} \end{bmatrix} = \begin{bmatrix} \nabla^T f_1 \\ \vdots \\ \nabla^T f_m \end{bmatrix} = \begin{bmatrix} \frac{\partial f_1}{\partial x_1} & \cdots & \frac{\partial f_1}{\partial x_n} \\ \vdots & \ddots & \vdots \\ \frac{\partial f_m}{\partial x_1} & \cdots & \frac{\partial f_m}{\partial x_n} \end{bmatrix}

where ∇Tfi\nabla^T f_i is the transpose (row vector) of the gradient of the ii-th component.

Backpropagation with Vectors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
================================================================================
COMPUTATIONAL GRAPH NODE: BACKPROP WITH VECTORS
================================================================================

FORWARD PASS (Green Arrows)
Computes output vector z from input vectors x, y

[ Vector x ] --------------------\
Shape: Dx \
\
v
_____________ [ Vector z ]
| | Shape: Dz
| Node f | ----------------->
|_____________|
^
/
/
[ Vector y ] --------------------/
Shape: Dy


================================================================================

BACKWARD PASS (Red Arrows)
Matrix-Vector Multiply: Downstream = Local * Upstream

Upstream Gradient
(dL/dz)
Shape: Dz
|
v
_____________ _____________
Downstream Gradient | | | |
(dL/dx) | Jacobian | | |
Shape: Dx <-------------| [Dx x Dz] | <----------| |
= | dz/dx | | |
(dz/dx) * (dL/dz) |_____________| | |
| |
| Node f |
| |
_____________ | |
Downstream Gradient | | | |
(dL/dy) | Jacobian | | |
Shape: Dy <-------------| [Dy x Dz] | <----------| |
= | dz/dy | |_____________|
(dz/dy) * (dL/dz) |_____________|

Jacobians can be sparse. For element-wise operations, the off-diagonal elements are always zero; the Jacobian becomes a diagonal matrix.

Four Fundamental Equations of Backpropagation

These allow us to propagate error (δ\delta) from the output backward through the network. We get them from simple chain rules.

Notation:

  • ⊙\odot: Hadamard (element-wise) product
  • σ′\sigma': Derivative of the activation function
  • δl\delta^l: Error term for layer ll

(BP1) Error at the Output Layer

δL=∇aC⊙σ′(zL)\delta^L = \nabla_a C \odot \sigma'(z^L)

Interpretation: How much the output layer “missed” the target, scaled by the activation’s sensitivity.

(BP2) Propagating Error to Hidden Layers

δl=((wl+1)Tδl+1)⊙σ′(zl)\delta^l = ((w^{l+1})^T \delta^{l+1}) \odot \sigma'(z^l)

Interpretation: The error at layer ll is the weighted sum of errors from the next layer (l+1l+1), pulled backward through the transpose of the weights.

(BP3) Gradient for Biases

∂C∂bjl=δjl\frac{\partial C}{\partial b^l_j} = \delta^l_j

Interpretation: The gradient for a bias is exactly equal to the error at that neuron.

(BP4) Gradient for Weights

∂C∂wjkl=akl−1δjl\frac{\partial C}{\partial w^l_{jk}} = a^{l-1}_k \delta^l_j

Interpretation: The gradient for a weight is the product of the input activation (al−1a^{l-1}) and the output error (δl\delta^l).