agentsclimarketplace

Pytorch loss implementation

Skill cxcscmu/SkillLearnBench/skills/b4-skill-creator-gemini-3.1-pro-preview/nlp-paper-reproduction/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

Install
npx -y skills add cxcscmu/SkillLearnBench --skill pytorch-loss-implementation

Assembled 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

  1. 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.
  2. Numerical Stability: Be careful with operations like log and exp. Use numerically stable functions like F.log_softmax instead of taking the log of a softmax, and F.binary_cross_entropy_with_logits instead of applying sigmoid then BCE.
  3. 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.
  4. 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.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.