AutoSkill 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.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt3.5_8_GLM4.7/pytorch-accuracy-calculation-conversion-crossentropy-to-mse" ~/.claude/skills/ecnu-icalk-autoskill-pytorch-accuracy-calculation-conversion-crossentropy-to-mse && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt3.5_8_GLM4.7/pytorch-accuracy-calculation-conversion-crossentropy-to-mse/SKILL.mdsource content
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
withnn.CrossEntropyLoss()
.nn.MSELoss() - Accuracy Calculation: Replace the classification accuracy logic (e.g.,
) with regression logic.output.max(1)[1] == y- Use
to convert continuous outputs to discrete values for comparison.output.round() - 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
or.int()
)..round() - Tensor Shapes: Be aware that MSELoss typically requires the target
to have the same shape as the model output, whereas CrossEntropyLoss expects class indices.y
Anti-Patterns
- Do not use thresholding (e.g.,
) unless explicitly requested; prefer rounding as per the user's preference.output >= 0.5 - Do not leave the original
logic in place.output.max(1)[1]
Triggers
- convert accuracy calculation to MSELoss
- change CrossEntropyLoss accuracy to MSE
- use round for accuracy calculation
- PyTorch regression accuracy metric