Label smoothing binary

From wenmin-wu/ds-skills · View interactive version

Label smoothing binary is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Applies label smoothing to binary cross-entropy loss to reduce overconfidence and improve generalization in text classification.

How to use this skill

Skill file

Label Smoothing (Binary)

Overview

Replace hard labels (0/1) with softened labels (ε, 1-ε) during training. This prevents the model from becoming overconfident on training examples, acts as implicit regularization, and improves calibration of predicted probabilities. Especially useful in text classification where boundary examples are ambiguous.

Quick Start

# Keras / TensorFlow
import tensorflow as tf

loss = tf.keras.losses.BinaryCrossentropy(label_smoothing=0.02)
model.compile(optimizer='adam', loss=loss, metrics=['accuracy'])

# PyTorch (manual)
import torch
import torch.nn.functional as F

def smooth_bce(preds, targets, smoothing=0.02):
    """Binary cross-entropy with label smoothing."""
    targets = targets * (1 - smoothing) + 0.5 * smoothing
    return F.binary_cross_entropy_with_logits(preds, targets)

# Usage in training loop
loss = smooth_bce(logits, labels, smoothing=0.02)
loss.backward()

Workflow

  1. Choose smoothing factor ε (typically 0.01-0.1)
  2. Replace loss function with smoothed version
  3. Train as usual — no other changes needed
  4. At inference, predictions are naturally better calibrated

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.