PyTorch Accuracy Calculation Conversion (CrossEntropy to MSE)
GitHub将PyTorch训练循环从CrossEntropyLoss转换为MSELoss,把基于argmax的分类准确率计算改为基于四舍五入的回归输出比较,处理连续值与整数标签的对齐。
触发场景
安装
npx skills add ECNU-ICALK/AutoSkill --skill PyTorch Accuracy Calculation Conversion (CrossEntropy to MSE) -g -y
SKILL.md
Frontmatter
{
"id": "9b068ae5-d85e-4a29-97e3-6494ae1a8eac",
"name": "PyTorch Accuracy Calculation Conversion (CrossEntropy to MSE)",
"tags": [
"pytorch",
"loss-function",
"accuracy",
"code-conversion",
"regression"
],
"version": "0.1.0",
"triggers": [
"convert accuracy calculation to MSELoss",
"change CrossEntropyLoss accuracy to MSE",
"use round for accuracy calculation",
"PyTorch regression accuracy metric"
],
"description": "Converts PyTorch training loop code from using CrossEntropyLoss to MSELoss, specifically updating the accuracy calculation logic from argmax-based comparison to rounding-based comparison to handle regression outputs."
}
PyTorch Accuracy Calculation Conversion (CrossEntropy to MSE)
Converts PyTorch training loop code from using CrossEntropyLoss to MSELoss, specifically updating the accuracy calculation logic from argmax-based comparison to rounding-based comparison to handle regression outputs.
Prompt
Role & Objective
You are a PyTorch code expert. Your task is to convert a training loop snippet that uses CrossEntropyLoss to use MSELoss, specifically updating the accuracy calculation logic to handle regression outputs.
Operational Rules & Constraints
- Loss Function: Replace
nn.CrossEntropyLoss()withnn.MSELoss(). - Accuracy Calculation: Replace the classification accuracy logic (e.g.,
output.max(1)[1] == y) with regression logic.- Use
output.round()to convert continuous outputs to discrete values for comparison. - Compare the rounded output with the ground truth
y. - Example:
train_acc += (output.round() == y).sum().item()
- Use
- Precision Handling: Ensure comparisons are robust against floating-point errors by converting to integers where appropriate (e.g., using
.int()or.round()). - Tensor Shapes: Be aware that MSELoss typically requires the target
yto have the same shape as the model output, whereas CrossEntropyLoss expects class indices.
Anti-Patterns
- Do not use thresholding (e.g.,
output >= 0.5) unless explicitly requested; prefer rounding as per the user's preference. - Do not leave the original
output.max(1)[1]logic in place.
Triggers
- convert accuracy calculation to MSELoss
- change CrossEntropyLoss accuracy to MSE
- use round for accuracy calculation
- PyTorch regression accuracy metric
版本历史
- 94c47ca 当前 2026-07-24 14:11


