惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
Google DeepMind News
Google DeepMind News
Hugging Face - Blog
Hugging Face - Blog
D
Docker
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
Project Zero
Project Zero
Engineering at Meta
Engineering at Meta
J
Java Code Geeks
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Simon Willison's Weblog
Simon Willison's Weblog
S
Security Affairs
NISL@THU
NISL@THU
T
Tor Project blog
A
About on SuperTechFans
宝玉的分享
宝玉的分享
腾讯CDC
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Privacy & Cybersecurity Law Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
P
Privacy International News Feed
雷峰网
雷峰网
C
Cyber Attacks, Cyber Crime and Cyber Security
Vercel News
Vercel News
Cisco Talos Blog
Cisco Talos Blog
D
DataBreaches.Net
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
L
LINUX DO - 热门话题
Microsoft Security Blog
Microsoft Security Blog
Latest news
Latest news
C
Check Point Blog
有赞技术团队
有赞技术团队
T
The Exploit Database - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
云风的 BLOG
云风的 BLOG
SecWiki News
SecWiki News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
爱范儿
爱范儿
月光博客
月光博客
V
Vulnerabilities – Threatpost
T
Threat Research - Cisco Blogs
P
Palo Alto Networks Blog
T
The Blog of Author Tim Ferriss
C
Cisco Blogs
Webroot Blog
Webroot Blog
S
Security @ Cisco Blogs

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Why Your AI Model's Confidence Score Is Probably Lying (And What To Do About It)
Saee Barve · 2026-06-19 · via DEV Community

The distribution shift problem that breaks modern AI in production explained for developers who actually deploy these things.

You trained the model. Metrics looked great. You deployed it. Six months later, something is quietly wrong but your accuracy dashboard looks fine.

What happened?

If you are running a modern AI system at scale, especially one using a Mixture-of-Experts architecture, there is a good chance your model's confidence scores have drifted out of alignment with reality. Not because the model got worse at prediction. Because the calibration broke silently, without error, without warning.

This post explains what that means, why it happens to MoE models specifically, and what you can do about it as a developer.

Quick Vocabulary Check

Before diving in, two terms you need:

Calibration: If your model says "I'm 80% confident," it should be correct 80% of the time it says that. A calibrated model's confidence scores are honest probability estimates. An uncalibrated model's confidence scores are basically noise.

Distribution shift: The data your model sees in production is not the same as the data it was trained on. The distribution of inputs drifts over time. This is not an edge case it is the normal state of any deployed model.

The Architecture: Mixture-of-Experts (MoE)

Most large-scale AI models today use MoE. The idea is simple:

Instead of one giant network, you have many specialized sub-networks called experts
A router looks at each input and decides which expert(s) handle it
This lets you scale model capacity without scaling compute linearly

Two flavors of routing:

Hard Routing:  input → router → ONE expert → output
Soft Routing:  input → router → weighted blend of MULTIPLE experts → output

Soft routing is more expressive. It is also where calibration gets complicated.

The Problem: Perfectly Calibrated Experts, Broken Aggregate

Here is the scenario that should concern every ML engineer.

Suppose every expert in your MoE is individually well calibrated. When Expert A says 0.8, it is right 80% of the time. Same for Expert B, Expert C, all of them.

You might assume the combined model is also well-calibrated.

It is not under distribution shift.

Here is why.

With soft routing, your final prediction is:

f(x) = r1(x) * f1(x) + r2(x) * f2(x) + ... + rK(x) * fK(x)

Where r1, r2, ...rK are routing weights and f1, f2, ...fK are expert predictions.

The same final score (say, 0.75) can come from completely different configurations:


Config A: r1=0.9, f1=0.75, r2=0.1, f2=0.75  → f(x) = 0.75
Config B: r1=0.5, f1=0.9,  r2=0.5, f2=0.6   → f(x) = 0.75
Config C: r1=0.3, f1=0.5,  r2=0.7, f2=0.89  → f(x) = 0.75

On your training distribution, these configurations fire in certain proportions. Those proportions make the calibration work out — the deviations cancel, and 0.75 ends up being right 75% of the time.

Then distribution shift happens.

New data changes how often different types of inputs appear. Different routing configurations fire at different rates. The proportions that made calibration balance out no longer hold.

Now when the model says 0.75, maybe it is only right 58% of the time. Or 91% of the time. The confidence score has become unreliable — and you have no easy way to know from the outside.

Why Hard Routing Does Not Have This Problem

With hard routing, each input goes to exactly one expert. Your aggregate prediction is just that expert's prediction. The full routing information collapses to a simple pair: (which expert, what confidence).

If Expert 2 says 0.75, and Expert 2 is calibrated, then 0.75 is trustworthy regardless of whether the test distribution sends more or fewer inputs to Expert 2 than the training distribution did.

Hard routing is more robust to distribution shift in this specific dimension. The tradeoff is expressiveness: hard routing cannot capture cases where multiple experts' knowledge genuinely needs to be blended.

How Bad Can It Get?

The failure is worst on inputs that trigger the fragile configurations specifically the cases where:

Multiple experts receive substantial routing weight (not dominated by one expert)
Those experts disagree significantly in their predictions
The aggregate prediction therefore depends heavily on the exact routing weights

These are the cases where a mild shift in data distribution — one that does not change what the right answer is, does not change expert behavior, just changes how often certain input types appear can flip the calibration from reliable to useless.

And these are exactly the kinds of inputs where you most need reliable uncertainty estimates. If experts agree, you already have a signal. When experts disagree and you need the aggregate to guide you, that is when the calibration tends to be least trustworthy.

The Fix: Adversarial Reweighting During Training

The solution is to train the model to be calibrated not just on the average training distribution, but on stressed versions of that distribution.

The key insight: examples where the model has high loss are a proxy for the fragile configurations. These are the examples where routing weights create a shaky balance. If you train against adversarially reweighted distributions that emphasize high-loss examples, you make the model more robust where it needs to be.

In practice, this means using an exponential tilt during training:

# Conceptual implementation of Robust MoE training objective
def robust_moe_loss(losses, eta=1.0):
    """
    losses: per-example losses in the minibatch
    eta: tilt strength (higher = more emphasis on hard examples)
    """
    import torch

    # Compute entropy-balanced weights
    weights = torch.exp(eta * losses)
    weights = weights / weights.sum()  # normalize

    # Weighted loss emphasizes high-loss (fragile) examples
    robust_loss = (weights * losses).sum()

    return robust_loss

# Standard training loop modification
for batch_x, batch_y in dataloader:
    predictions = model(batch_x)

    # Per-example losses
    per_example_losses = criterion(predictions, batch_y, reduction='none')

    # Standard ERM loss
    # erm_loss = per_example_losses.mean()

    # Robust MoE loss - upweights hard examples
    loss = robust_moe_loss(per_example_losses, eta=0.5)

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

There is also a more targeted variant called Robust Filtered, which only applies the reweighting to routing-relevant examples — specifically:

Examples where the blended prediction is worse than the best individual expert
Examples where experts substantially disagree around the aggregate prediction

def robust_filtered_loss(losses, predictions, expert_predictions, routing_weights, eta=1.0):
    """
    Apply robust reweighting only to routing-relevant examples.
    """
    import torch

    # Find examples where blend is worse than best expert
    best_expert_loss = expert_predictions.min(dim=1).values  # simplified
    blend_worse = losses > best_expert_loss

    # Find examples where experts disagree substantially
    expert_variance = expert_predictions.var(dim=1)
    high_disagreement = expert_variance > expert_variance.median()

    # Routing-relevant subset
    routing_relevant = blend_worse | high_disagreement

    # ERM on full batch
    erm_loss = losses.mean()

    # Robust reweighting on routing-relevant subset
    if routing_relevant.sum() > 0:
        subset_losses = losses[routing_relevant]
        weights = torch.exp(eta * subset_losses)
        weights = weights / weights.sum()
        robust_term = (weights * subset_losses).sum()
    else:
        robust_term = 0.0

    return erm_loss + robust_term

Both approaches consistently improve the calibration-accuracy tradeoff under distribution shift without a meaningful accuracy cost.

What To Do Right Now as a Developer

You might not be retraining your model today. Here is what you can do immediately:

  1. Add calibration monitoring to your eval pipeline
import numpy as np

def expected_calibration_error(y_true, y_prob, n_bins=10):
    """
    Compute Expected Calibration Error (ECE).
    Lower is better. 0 = perfect calibration.
    """
    bin_boundaries = np.linspace(0, 1, n_bins + 1)
    ece = 0.0

    for i in range(n_bins):
        lower, upper = bin_boundaries[i], bin_boundaries[i+1]
        mask = (y_prob >= lower) & (y_prob < upper)

        if mask.sum() == 0:
            continue

        bin_accuracy = y_true[mask].mean()
        bin_confidence = y_prob[mask].mean()
        bin_size = mask.sum()

        ece += (bin_size / len(y_true)) * abs(bin_accuracy - bin_confidence)

    return ece

# Add to your regular eval run
ece = expected_calibration_error(y_true, model_probabilities)
print(f"ECE: {ece:.4f}")  # flag if this creeps up over time

  1. Plot reliability diagrams regularly
import matplotlib.pyplot as plt
from sklearn.calibration import calibration_curve

def plot_reliability_diagram(y_true, y_prob, title="Reliability Diagram"):
    fraction_of_positives, mean_predicted_value = calibration_curve(
        y_true, y_prob, n_bins=10
    )

    plt.figure(figsize=(8, 6))
    plt.plot([0, 1], [0, 1], 'k--', label='Perfect calibration')
    plt.plot(mean_predicted_value, fraction_of_positives, 
             's-', label='Model')
    plt.xlabel('Mean predicted probability')
    plt.ylabel('Fraction of positives')
    plt.title(title)
    plt.legend()
    plt.show()

A model drifting toward overconfidence will show a curve that bends below the diagonal. Catch this early.

  1. Track input distribution drift
from scipy.stats import ks_2samp

def detect_distribution_shift(train_features, current_features, threshold=0.05):
    """
    Kolmogorov-Smirnov test for distribution shift per feature.
    Flag features where p-value < threshold.
    """
    shifted_features = []

    for i in range(train_features.shape[1]):
        stat, p_value = ks_2samp(train_features[:, i], current_features[:, i])
        if p_value < threshold:
            shifted_features.append({
                'feature_index': i,
                'ks_statistic': stat,
                'p_value': p_value
            })

    return shifted_features

  1. Use temperature scaling as a quick post-hoc fix

If you cannot retrain, temperature scaling is the fastest way to recalibrate a model after deployment:

import torch
import torch.nn as nn

class TemperatureScaler(nn.Module):
    def __init__(self):
        super().__init__()
        self.temperature = nn.Parameter(torch.ones(1))

    def forward(self, logits):
        return logits / self.temperature

    def fit(self, logits, labels, lr=0.01, max_iter=50):
        optimizer = torch.optim.LBFGS([self.temperature], lr=lr, max_iter=max_iter)
        criterion = nn.CrossEntropyLoss()

        def eval_step():
            optimizer.zero_grad()
            loss = criterion(self.forward(logits), labels)
            loss.backward()
            return loss

 **       optimizer.step(eval_step)
 **       return self

Note: temperature scaling helps on average but does not address the subset-specific calibration failures from distribution shift. It is a patch, not a solution.

Summary

Routing TypeCalibration Under ShiftWhyHard routingRobust ✅Calibration depends only on (expert, confidence) pairSoft routingFragile ⚠️Different configurations collapse to same score; shift changes their balance

The fix: Train with adversarial reweighting (Robust MoE or Robust Filtered) to stress the model on its hardest examples. At minimum, monitor ECE and distribution shift in production.

The deeper lesson: calibration is a system-level property. Calibrated parts do not automatically combine into a calibrated whole — especially when distribution shift changes how those parts interact.

Have you dealt with calibration drift in production? What monitoring setup worked for you? Drop it in the comments.