BLOGLLM Logo
Go Pro
Guest
Not signed in
所有博客课程练习面试准备系列标签书签价格
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboard游戏
Articles
练习 / Neural networks8 of 18 in topic

264. 在残差分支上应用 drop path (stochastic depth)

EasyNeural networks

重要性 Drop path 是大多数现代 ViT 和 ConvNeXt 模型训练方案的一部分。

非常深的 vision transformers 在训练期间会随机跳过整个残差分支。这就是 drop path,也称为 stochastic depth。

编写 drop_path(x, branch, keep, p, training)。x 和 branch 是 (n, d) 数组。keep 是一个 (n,) 的 boolean 数组:True 表示该样本在此步骤保留其分支。如果是 training,返回 x + branch * keep[:, None] / (1 - p)。如果不是 training,返回 x + branch (忽略 keep)。p 是丢弃概率,0 <= p < 1。

函数def drop_path(x, branch, keep, p, training)

示例 1

输入: x = [[1, 1], [2, 2]], branch = [[1, 2], [3, 4]], keep = [True, False], p = 0.5, training = True

输出: [[3, 5], [2, 2]]

示例 2

输入: x = [[0.0, 1.0, 2.0]], branch = [[1.0, 1.0, 1.0]], keep = [True], p = 0.2, training = True

输出: [[1.25, 2.25, 3.25]]

约束条件

  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 3 tests to be accepted.
提示 1

mask 是按样本生成的,而不是按元素生成的:每一整行对应一个 True 或 False。

提示 2

将 keep reshape 为一列。

提示 3

除以 1 - p,而不是乘以 1 - p。

提示 4

非 training 时返回 x + branch。

常见错误
  • 使用了 elementwise mask,那是普通的 dropout。
  • 在 eval 时进行 scaling 而不是在 training 期间。
理解思路

在一个残差块中,输出是 x + f(x)。Drop path 在训练期间为某些样本随机关闭 f,使得网络对这些样本的行为表现得像一个更浅的网络。它同时训练了多种深度,并降低了每个 block 对其他 block 的依赖性。可以把它想象成一个团队,随机成员轮流缺席练习,这样就不会有人变得不可替代。

Worked example

x = [1, 1], branch = [1, 2], p = 0.5. 如果保留,output = [1, 1] + [1, 2] / 0.5 = [3, 5]. 如果丢弃,output = [1, 1]. 平均而言是 [2, 3],正好等于 x + branch,即 eval 的输出。

train: y = x + branch * keep / (1 - p); eval: y = x + branch

像 ViT-B 和 ConvNeXt 这样的深度图像模型使用的 drop path rate 会随深度增加。

Shape 与数据流
x, branch (n, d); keep (n,) -> (n, 1) to broadcast across features
主题
Neural networks
相似题目
  • 全连接层的反向传播Easy
  • 计算 dense 和 conv 权重的 Xavier 和 He 初始化 scaleEasy
  • 计算 MLP 的参数量Easy
  • Find where early stopping haltsEasy
所有 Neural networks 道题目
反馈
264 / 903
Python 3solution.pyPython 空闲
1
2
3
4
5
6
import numpy as np
​
def drop_path(x, branch, keep, p, training):
# your code here
pass
​

Run 会在浏览器中检查这些用例,无需登录。 Submit 会检查所有 3 个测试。

Ctrl/按 + 回车 运行
© BlogLLM, Bengaluru, India关于条款隐私退款DMCA联系我们RSS