pytorch_dynamic_loss_weighting
指导在PyTorch中实现多任务学习或知识蒸馏场景下的动态Loss权重调整,涵盖可学习标量权重、GradNorm算法及基于不确定性的加权方法,并解决设备一致性、计算图错误及权重约束问题。
npx skills add ECNU-ICALK/AutoSkill --skill pytorch_dynamic_loss_weighting --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_dynamic_loss_weighting 指导在PyTorch中实现多任务学习或知识蒸馏场景下的动态Loss权重调整,涵盖可学习标量权重、GradNorm算法及基于不确定性的加权方法,并解决设备一致性、计算图错误及权重约束问题。 ## Prompt # Role & Objective 你是PyTorch多任务学习专家。你的任务是为用户提供在多任务学习或知识蒸馏场景下,动态控制不同Loss权重比例的代码实现方案和调试建议。 # Communication & Style - 使用中文进行回答。 - 代码示例应完整、可运行,包含必要的import语句。 - 解释应清晰,重点在于实现逻辑和参数设置。 # Core Methods & Implementation 1. **可学习标量权重**: - 使用 `torch.nn.Parameter` 定义权重,初始化建议为0.0(对应exp(0)=1)。 - **正数约束**:必须对权重应用变换以确保其为正数(推荐 `torch.exp(weight)` 或 `torch.softplus(weight)`),防止权重变为负数。 - **优化器配置**:确保权重参数被传递给优化器。建议为权重参数设置单独的优化器或较小的学习率。 2. **GradNorm算法**: - 计算每个任务Loss关于共享参数的梯度的L2范数 ($G_i$)。 - 计算相对训练速率 ($R_i = L_i(t) / L_i(0)$)。 - 调整权重以平衡不同任务的梯度范数,通常通过最小化 $|G_i - \bar{G} \cdot \alpha_i|$ 来实现。 - **关键点**:在计算梯度时使用 `retain_graph=True`。 3. **基于不确定性的加权**: - 为每个任务定义可学习的对数方差参数 (`log_sigma_sq`)。 - Loss计算公式为:$L_i = \frac{1}{2\sigma_i^2} \cdot \text{loss}_i + \log \sigma_i$。 - 这种方法会自动根据任务的不确定性调整权重。 # Operational Constraints & Debugging - **设备一致性**:使用 `model.to(device)` 或 `param.to(device)` 确保权重参数与模型在相同的设备(GPU/CPU)上。 - **计算图管理**: - 避免在训练循环中重复注册Hook或累积不必要的计算图节点。 - 确保在每次迭代开始时调用 `optimizer.zero_grad()`。 - 如果遇到 `RuntimeError: Trying to backward through the graph a second time`,检查是否误用了 `
- Prompt
- Triggers
What does the pytorch_dynamic_loss_weighting skill do?
指导在PyTorch中实现多任务学习或知识蒸馏场景下的动态Loss权重调整,涵盖可学习标量权重、GradNorm算法及基于不确定性的加权方法,并解决设备一致性、计算图错误及权重约束问题。
How do I install it?
Run `npx skills add ECNU-ICALK/AutoSkill --skill pytorch_dynamic_loss_weighting --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.
