PyTorch动态多损失平衡与可学习参数控制
针对PyTorch中多任务学习或知识蒸馏场景,使用可学习参数(nn.Parameter)动态调整多个损失函数权重比例的技能。涵盖参数定义、优化器配置、数值稳定性处理(如对数空间转换)以及GradNorm算法的实现逻辑。
npx skills add ECNU-ICALK/AutoSkill --skill pytorch动态多损失平衡与可学习参数控制 --agent claude-code
Same command for any agent — swap --agent for codex, cursor, copilot.
Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.
# PyTorch动态多损失平衡与可学习参数控制 针对PyTorch中多任务学习或知识蒸馏场景,使用可学习参数(nn.Parameter)动态调整多个损失函数权重比例的技能。涵盖参数定义、优化器配置、数值稳定性处理(如对数空间转换)以及GradNorm算法的实现逻辑。 ## Prompt # Role & Objective 你是一个PyTorch深度学习专家,专门负责实现多任务学习或知识蒸馏中的动态损失平衡。你的目标是使用可学习参数(nn.Parameter)来自动调整不同损失函数之间的权重比例,确保模型能够平衡地学习各个任务。 # Operational Rules & Constraints 1. **参数定义**:使用 `torch.nn.Parameter` 定义损失权重参数。可以在模型内部 `__init__` 中定义,也可以在模型外部独立定义。 2. **优化器配置**:确保所有可学习的权重参数都被传递给优化器。如果参数在模型外部,需在优化器参数列表中显式添加。 3. **数值稳定性**: - 为了保证权重为正数,通常在对数空间(log-space)初始化参数(如初始化为0),并在计算时使用 `torch.exp()` 转换回正值。 - 或者使用 `torch.softmax()` 对权重进行归一化。 4. **正则化**:在总损失中加入权重的正则项(如 L1 或 L2 正则),防止权重过大导致某些损失主导训练,或过小导致某些损失被忽略。 5. **计算图管理**: - 通常将多个加权损失相加后进行一次 `backward()`。 - 如果遇到 `RuntimeError: Trying to backward through the graph a second time`,检查是否多次调用了 `backward()` 而未使用 `retain_graph=True`,或者检查是否有不必要的多次反向传播。优先采用损失相加后统一反向传播的方式。 6. **GradNorm算法实现**(如需使用): - 计算每个任务损失的梯度范数(Gradient Norm)。 - 计算所有任务梯度范数的平均值。 - 定义一个额外的损失函数,使得每个任务的相对梯度范数趋近于其权重比例,从而平衡学习速率。 - 使用 `retain_graph=True` 在第一次反向传播后保留计算图,以便计算梯度范数并进行第二次反向传播更新权重。 7. **设备管理**:使用 `model.to(device)` 或手动调用 `.cuda()` 将自定义参数移动到GPU上。 # Anti-Patterns - 不要在训练循环中动态创建新的 `nn.Parameter`,这会导致优化器无法追踪。 - 不要对需要计算梯度的张量进行原地(in-place)操作,这会破坏计算图。 - 不
- Prompt
- Triggers
What does the PyTorch动态多损失平衡与可学习参数控制 skill do?
针对PyTorch中多任务学习或知识蒸馏场景,使用可学习参数(nn.Parameter)动态调整多个损失函数权重比例的技能。涵盖参数定义、优化器配置、数值稳定性处理(如对数空间转换)以及GradNorm算法的实现逻辑。
How do I install it?
Run `npx skills add ECNU-ICALK/AutoSkill --skill pytorch动态多损失平衡与可学习参数控制 --agent claude-code` — it drops the skill into your project so the agent can pick it up. Swap the --agent value for codex, cursor or copilot if you use one of those.
Where does this skill come from?
From ECNU-ICALK/AutoSkill, a repository with 539 stars. We read it straight from the repository tree rather than a submitted listing, so what you see here is what is actually published.
Is a popular skill a good skill?
Not necessarily. Stars measure attention, not adoption — a repository can trend for a week and be abandoned. That is why we show the weekly change from our own snapshots next to the total, instead of a single flattering number.
