RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Advanced NLP with Local Models
  6. /Ch. 5
Advanced NLP with Local Models

05. Multi-Label Classification

Chapter 5 of 18 · 15 min
KEY INSIGHT

Multi-label classification requires threshold tuning on domain-specific validation data. Default thresholds produce suboptimal results; calibration against true label distributions improves classification performance significantly.

Standard classification assigns exactly one label to each input instance. Multi-label classification permits multiple simultaneous labels, reflecting real-world complexity where categories overlap. A news article might simultaneously belong to "Politics," "Economics," and "Technology" categories.

Binary relevance treats multi-label problems as multiple independent binary classification tasks. Each label receives its own classifier determining presence or absence. While conceptually simple, binary relevance ignores label correlations—a financial news story correlated with both "Markets" and "Banking" likely contains vocabulary patterns unique to that intersection.

Local LLMs handle multi-label classification through prompt reformulation that specifies label enumeration and output format requirements. Threshold calibration influences results significantly—the same model achieves different precision-recall tradeoffs by adjusting the confidence cutoff for positive label assignment.

import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer

model_name = "local-llama3-for-classification"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(
    model_name,
    num_labels=10,
    problem_type="multi_label_classification"
)

labels = ["politics", "economy", "technology", "sports", 
          "entertainment", "science", "health", "world",
          "business", "crime"]

def classify_multilabel(text, threshold=0.5):
    inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
    
    with torch.no_grad():
        outputs = model(**inputs)
        probs = torch.sigmoid(outputs.logits).squeeze()
    
    predictions = []
    for i, prob in enumerate(probs):
        if prob.item() > threshold:
            predictions.append(labels[i])
    
    return predictions if predictions else ["unknown"]

text = "Federal Reserve announces interest rate decision affecting tech sector investments"
result = classify_multilabel(text, threshold=0.5)
print(result)

Label dependencies emerge in structured domains. Medical coding systems like ICD-10 contain hierarchical relationships where parent categories imply child category presence. Product taxonomies follow similar inclusion rules. Prompt engineering can encode these dependencies by instructing the model to prefer specific label relationships.

Threshold optimization requires validation data with balanced representation across label combinations. Grid search over threshold values (typically 0.3 to 0.7 in 0.05 increments) identifies optimal operating points for F1 macro or subset accuracy metrics. The optimal threshold depends heavily on application requirements—high-stakes applications typically favor precision over recall.

EXERCISE

Implement multi-label classification for a domain of your choice. Generate synthetic validation data representing label co-occurrence patterns. Tune thresholds and report precision, recall, and F1 per label.

← Chapter 4
Relation Extraction
Chapter 6 →
Text Classification Pipelines