Skip to content
Back to skills

Masked Bce Span Loss

ASecurity

Binary cross-entropy loss with mask to ignore special and padding tokens in token-level span classification

  • 61 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 12, 2026
documentationpythongit

Works with

  • cli

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill masked-bce-span-loss --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Masked Bce Span Loss?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Masked Bce Span Loss
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-masked-bce-span-loss/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-masked-bce-span-loss)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: nlp-masked-bce-span-loss
description: Binary cross-entropy loss with mask to ignore special and padding tokens in token-level span classification
domain: nlp
---

# Masked BCE Span Loss

## Overview

For token-level span classification, special tokens (CLS, SEP, PAD) should not contribute to the loss. Label them as -1 during preprocessing, then use `torch.masked_select` to exclude them before computing mean BCE loss.

## Quick Start

```python
import torch
import torch.nn as nn

criterion = nn.BCEWithLogitsLoss(reduction="none")

def masked_bce_loss(logits, labels):
    """BCE loss that ignores tokens labeled -1 (special/padding)."""
    loss = criterion(logits.view(-1, 1), labels.view(-1, 1))
    mask = labels.view(-1, 1) != -1
    return torch.masked_select(loss, mask).mean()
```

## Label Creation

```python
import numpy as np

def create_token_labels(tokenizer, text, char_spans, max_len):
    enc = tokenizer(text, max_length=max_len, padding="max_length",
                    return_offsets_mapping=True, add_special_tokens=True)
    labels = np.zeros(max_len)
    # Mark non-text tokens as -1
    labels[np.array(enc.sequence_ids()) != 0] = -1
    # Mark span tokens as 1
    for start, end in char_spans:
        for i, (os, oe) in enumerate(enc["offset_mapping"]):
            if os >= start and oe <= end and labels[i] != -1:
                labels[i] = 1.0
    return labels
```

## Key Decisions

- **-1 label convention**: compatible with any loss function via masking, no custom loss class needed
- **reduction='none'**: required so masking happens before aggregation
- **Mask includes PAD + special**: both CLS/SEP and padding tokens get -1

## References

- Source: [nbme-deberta-base-baseline-train](https://www.kaggle.com/code/yasufuminakama/nbme-deberta-base-baseline-train)
- Competition: NBME - Score Clinical Patient Notes

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.