Pytorch loss implementation
How to implement custom loss functions in PyTorch. Use this skill whenever the user asks to implement a loss function, write a custom criterion, or mentions PyTorch tensor operations for backpropagation.From its SKILL.md
npx -y skills add cxcscmu/SkillLearnBench --skill pytorch-loss-implementationAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
SKILL.md
1.8 KB, 339 tokens by cl100k_base, as published. Nobody here has run it
PyTorch Loss Implementation
This skill provides guidelines for implementing custom loss functions in PyTorch.
Key Principles
- Use PyTorch Primitives: Always use
torch.*operations (e.g.,torch.log,torch.exp,torch.sum,F.log_softmax) to ensure operations are differentiable and can be tracked by autograd. - Numerical Stability: Be careful with operations like log and exp. Use numerically stable functions like
F.log_softmaxinstead of taking the log of a softmax, andF.binary_cross_entropy_with_logitsinstead of applying sigmoid then BCE. - Handling Padding and Masks: When working with NLP or sequence data, always apply attention masks or ignore_index appropriately to prevent padded tokens from contributing to the loss.
- Batch Reductions: By default, loss functions should support different reduction methods ('mean', 'sum', 'none'). Be explicit about how reductions are applied across batch and sequence dimensions.
Example Pattern
import torch
import torch.nn.functional as F
def custom_loss(logits, targets, mask=None, reduction='mean'):
# Apply operations
loss = F.cross_entropy(logits, targets, reduction='none')
# Apply mask if provided
if mask is not None:
loss = loss * mask
if reduction == 'mean':
return loss.sum() / mask.sum()
elif reduction == 'sum':
return loss.sum()
return loss
if reduction == 'mean':
return loss.mean()
elif reduction == 'sum':
return loss.sum()
return loss
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.