0%

residual explain

residual explain
some notes from this video

神经网络初始化时,权重是随机的 —— 每一层的权重矩阵都是随机生成的。
前向传播时,输入数据经过每一层都会被随机的权重矩阵变换,换句话说:输入信号在每一层都被“打乱”一次。
如果层数很深(比如几十层),输入数据就会被这些随机变换一遍又一遍,到了最后一层时,输出结果几乎不再携带原始输入的“有效信息” —— 变成了一堆“随机激活”(noise)。
这就像把一个信号通过几十个带随机旋转的transpose处理之后,最后剩下的根本不知道是什么。
我们可以说:原始输入被“扰乱”成了随机噪声。这种情况下,模型的输出和输入几乎没有实际联系。
训练时,我们计算 loss,并进行反向传播(backpropagation)以更新权重。
反向传播的梯度来源于输出误差,但如果输出本身已经是“随机激活”,那这个误差根本不能反映输入的真实特征,所以它对靠后的几层的更新意义也不大。
在反向传播过程中,每一层的梯度也会被它的权重矩阵(也就是在前向传播时使用的那个)反向变换。因为这些矩阵是随机的,所以:
梯度回传到早期层时,已经被多层的随机变换“扰乱”梯度本身也变得和数据无关了
后面的层(靠近输出)的输入是随机的,因此即使我们对它们做了梯度更新,也是在优化一些“无意义的东西”——所以这个更新 “不是很有意义”(not very meaningful)。
早期层的输入其实还比较接近原始输入,但 梯度本身已经被“污染”了,所以即使这些层“想学”,也无法从错误中获得有效信息。
这就解释了为什么深层网络在训练初期进展缓慢,甚至基本不学习 —— 因为梯度下降没有清晰的方向,就像“在黑夜里瞎走”。

  • 对于后面的层,它们的输入已经像噪声,所以即使 loss 传回来,更新的目标本身就没什么意义
  • 对于前面的层,它们的输入还比较“干净”,但传回来的梯度已经乱掉了,更新的方向也没有意义
  • 所以,这些更新不是完全错误,而是无效、低效、方向性差,因此叫 “not very meaningful”

gradient 和 weight的关系

在两层神经网络中,反向传播(Backpropagation) 的目的是计算损失函数关于每一层权重(weights)的梯度,用于更新权重。
下面步步说明梯度是如何与权重相关联的。

假设一个简单的两层神经网络结构:

网络结构:

输入 → 第一层(Linear + Activation) → 第二层(Linear) → 输出(Loss)

  • 输入:$\mathbf{x} \in \mathbb{R}^d$
  • 第一层权重:$\mathbf{W}_1 \in \mathbb{R}^{h \times d}$,偏置:$\mathbf{b}_1 \in \mathbb{R}^h$
  • 激活函数:ReLU 或 Sigmoid(记为 $f$)
  • 第二层权重:$\mathbf{W}_2 \in \mathbb{R}^{o \times h}$,偏置:$\mathbf{b}_2 \in \mathbb{R}^o$
  • 输出层无激活(或直接用 MSE)
Read more »

Bonus 架构图 (现代 Function Calling Agent 流程)

1
2
3
4
5
6
7
8
9
10
11
User Input

Embedding Model ───────→ 语义索引 (FAISS / Qdrant / Weaviate)

Top-K Function Candidates

Inject into Prompt as JSON Schema

LLM 生成 Function Call

调用工具 / API + 结构校验 + 反馈

MCP server 流程图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
               +-------------+
| MCP Client |
+------+-----+
|
| submit task / query result
v
+------+------+
| MCP Server |
+------+------+
|
+--------------+---------------+
| |
v v
+-------------+ +---------------+
| Agent 1 | | Agent 2 |
+-------------+ +---------------+
| ^ | ^
| | Task Execution | | Task Execution
+---+--------------------------+---+

build GPT from scratch

cross_entropy 损失函数估计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class BigramLanguageModel(nn.Module):
……
def forward(self, idx, targets=None):
B, T, C = logits.shape
logits = logits.view(B*T, C)
targets = targets.view(B*T)
loss = F.cross_entropy(logits, targets)

m = BigramLanguageModel(vocab_size)
logits, loss = m(xb, yb)
print(logits.shape)
print(loss)

# output
torch.Size([32, 65])
tensor(4.8786, grad_fn=<NllLossBackward0>)
1
loss = F.cross_entropy(logits, targets)

其实计算的是

其中:

$p$ 是 softmax 后的概率分布
$y$ 是 ground-truth label (目标token)
$p_y$ 是对应ground-truth 类别的概率

对于这个Loss如果我们想估计一下是什么水平,那就对比随机猜的情况,,这个可以作为baseline

Read more »

Layer Normalization

验证LayerNorm的,通过使用torch.mean和torch.var复现的时候发现不一致
LayerNorm默认使用的是bias的整体方差, divided by N
torch.var默认使用的是无bias的样本方差, devided by N-1

对于每一个样本的特征向量 $x \in \mathbb{R}^d$ ,LayerNorm 执行以下操作:

  • $\mu, \sigma^2$ :当前样本的均值和方差(仅用于归一化)
  • $\gamma$ :可学习的缩放参数(scale,类似于权重)
  • $\beta$ :可学习的偏移参数(bias,偏置)

LayerNomr

Read more »

linear regession 或者mpl 会涉及到weight decay, 提及整数解的问题:
k个变量,d阶的项一共有多少种?

问题描述
我们有 $ k $ 个变量 $ x_1, x_2, \ldots, x_k $,要求它们的和等于 $ d $,即:

$ x_1 + x_2 + \cdots + x_k = d $

自然数解

从 ( d - 1 ) 个位置中选择 ( k - 1 ) 个位置放置隔板,其余的位置放置星星。

这可以用组合数表示为:

$ \binom{d - 1}{k - 1} = \frac{(d - 1)!}{(k - 1)! \cdot (d - k)!} $

非负整数解

在这个问题中,每个变量 $ x_i $ 可以取0。这意味着在分配过程中,某些变量可能不会获得任何单位。因此,为了表示这种情况,我们需要在星(单位)之间允许隔板(分隔符)彼此相邻,甚至位于首尾位置。这就增加了排列组合的灵活性。

Read more »

通过pandas 的compare function, 可以对比两个csv 文件
用途,例如升级或修改code之后,输出是csv文本文件,对于同样input的数据,预期应该一样
input -> program -> output
input -> program(optimize) -> output_new
expect output == output_new

Read more »

安装

64-bit镜像:
https://downloads.raspberrypi.org/raspios_lite_arm64/images/raspios_lite_arm64-2021-05-28/

获取树莓派ip

树莓派在接入路由器后是动态分配的,需要使用路由器的管理界面查看链接设备的MAC和ip地址
另外一种方式是扫描当前局域网的设备信息,比如可以使用IOS的Fing App去发现当前连接设备,一般树莓派的设备名称是Raspberry开头的

挂载移动硬盘

https://shumeipai.nxez.com/2013/09/08/raspberry-pi-to-mount-the-removable-hard-disk.html

1
sudo mount -o uid=pi,gid=pi /dev/sda1 /mnt/1GB_USB_flash
Read more »

YOLO

1. YOLO 损失函数第二部分

对于width, height的loss, 作者在论文中说明使用square root的原因:

Our error metric should reflect that small deviations in large boxes matter less than in small boxes. To partially address this we predict the square root of the bounding box width and height instead of the width and height directly.
经过实际计算演示如下:

1
2
3
4
5
6
def width_height_loss(w, h, error_shift=30):
return math.pow((math.sqrt(w)-math.sqrt(w-erro_shift)),2) + math.pow((math.sqrt(h)-math.sqrt(h+erro_shift)),2)
print(width_height_loss(300,500))
print(width_height_loss(100,150))
>> 1.22700707099
>> 4.03446019009

可以发现如此设计后,确实能起到如其所说loss对小的bbox比较敏感,惩罚度较大,而对大bbbox则反之。
究其原因,需要画一下此函数的曲线:

从曲线可以直观的看到单调性

Read more »

Camera Raw

Basic Panel中
对于每一个可调节的滑块:

  • 双击滑块可回复原始值
  • 按住shift双击滑块可自动调整
    参考

在自动调整色调时,Camera Raw 忽略以前在其它选项卡中进行的任何调整(如在“色调曲线”选项卡中对色调进行的微调)。因此,先应用自动色调调整(如果有)以获取图像最佳设置的初始近似值。如果在拍摄时很细心并有意使用不同的曝光度进行拍摄,您可能不希望应用自动色调调整以放弃所做的工作。另一方面,如果您不喜欢所做的调整,可随时尝试单击“自动”并撤消这些调整。
为什么白平衡下面需要设置色调补偿绿色好紫色(tint)

查阅

A daunting task, because humans see using an eye/brain combination that alters our visualization of the world. This mechanism is eye independent. Try this experiment. Procure some transparent color filters, cellophane (candy wrappers work nicely). Filter one eye only with a deep red filter. Keep the filter in place for about 2 minutes. Remove the filter and then peer about quickly looking with one eye, then the other. You will find that the filter eye has radically changed as to its color balance. That eye will see the world with a blue-green tint. Try this again with yellow or blue or green cellophane. Each filter rapidly alters the filtered eye color balance. This adaptive mechanism occurs every minute of every day, to both eyes. You will be witnessing the human ability to adapt and normalize when the ambient light is off-color.
With some minor exceptions (special color films) color photography had to wait till the advent of electronic imaging. Now we have “white balance”, a camera mechanism that attempts to mimic our ability to adapt to light source alterations.
Photography adopted color “temperature”. Scientist verified that blacksmiths, glazers, potters and other workers (smiths) were able to regulate the temperatures of the materials of their craft visually by color changes as the material is heated. Seems all materials glow with alike outputs as they are heated to incandescence.
Black red 426°C —- Dark red 593°C – Cherry red 815°C – Yellow 1093°C – White 1315°+ etc.
Now physicists prefer the Celsius scale and they respect an adaptation known as the Absolute Scale. This is the Celsius scale with zero set to -273°C. This is the rock-bottom temperature so starting zero at this value avoids the possibility of negative temperatures. The champion of Absolute scale was William Thomson, 1st Baron Kelvin. In his honor, the Absolute scale was renamed Kelvin scale. Color films and now digital imaging and light sources are typically categorized using the Kelvin scale.
Candle 1850K – Standard 60W tungsten lamp 2400K – Studio tungsten bulb 3200K — Daylight 5500K.
Images taken under various lighting conditions often yield results that are not faithful reproduction. The traditional countermeasure was to mount a color correction optical filter before the lens. These are shades of blue to reverse reddish ambient light from tungsten sources and salmon (pink-amber) to neutralize the excessive bluish tints of north-sky daylight. Digital settings to correct off color lighting conditions are warming or cooling offsets.