ความมั่นใจและความไม่แน่นอนในการจำแนก
ขอให้โมเดลให้คะแนนความมั่นใจและจัดการการจำแนกที่กำกวม
ความมั่นใจและความไม่แน่นอนในการจำแนก เป็นบทเรียน AI Prompt Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Prompt Engineering และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Prompt Engineering มีบทเรียนทั้งหมด 4 บทเรียน
ปัญหาของโมเดลที่มั่นใจเกินไป
โดยค่าเริ่มต้น LLM จะตอบงานจัดประเภทด้วยความมั่นใจที่ดูเหมือนแน่นอน แม้ข้อมูลเข้าจะกำกวมอย่างแท้จริงก็ตาม โมเดลที่ถูกสั่งให้ส่งคืน บวก ลบ หรือเป็นกลาง จะเลือกอย่างใดอย่างหนึ่งเสมอ โดยจะไม่พูดว่า ฉันไม่แน่ใจ
ในระบบใช้งานจริง การดำเนินการกับผลการจัดประเภทที่ไม่แน่นอนราวกับว่าแน่นอนจะทำให้เกิดข้อผิดพลาดที่มีค่าใช้จ่ายสูง เช่น การส่งบัตรแจ้งปัญหาไปผิดเส้นทาง คำแนะนำที่ไม่ถูกต้อง และรายงานที่คลาดเคลื่อน
การวัดปริมาณความไม่แน่นอนในพรอมป์ต์การจัดประเภทช่วยแก้ปัญหานี้
คะแนนความมั่นใจ 1–10
การขอให้โมเดลให้คะแนนความมั่นใจด้วยมาตราส่วนตัวเลขจะให้สัญญาณที่ละเอียด ซึ่งระบบปลายทางสามารถใช้กำหนดเกณฑ์ได้:
import anthropic, json
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
def classify_with_confidence(text):
prompt = f'''
Classify the sentiment of the text below.
Return JSON:
{{
"sentiment": "positive|negative|neutral",
"confidence": 1-10,
"reason": "brief explanation of confidence level"
}}
Confidence scale: 10=completely certain, 1=total guess, 5=genuinely ambiguous
Text: {text}
'''
r = client.messages.create(
model='claude-opus-4-5', max_tokens=100,
messages=[{'role': 'user', 'content': prompt}]
)
return json.loads(r.content[0].text)
print(classify_with_confidence('I sort of liked it but the wait was too long.'))
print(classify_with_confidence('This product is absolutely outstanding!'))การตอบกลับ UNCERTAIN
การกำชับให้โมเดลตอบกลับด้วย UNCERTAIN อย่างชัดเจนเมื่อความมั่นใจในการจัดประเภทต่ำกว่าค่าขีดแบ่ง จะทำให้ได้เอาต์พุตสามทาง ได้แก่ บวก ลบ หรือ UNCERTAIN:
def classify_or_uncertain(text, uncertainty_threshold=4):
prompt = f'''
Classify the sentiment of the text: positive, negative, or neutral.
If the sentiment is genuinely ambiguous or you are not confident (confidence below {uncertainty_threshold}/10),
return UNCERTAIN instead of guessing.
Return JSON: {{"sentiment": "positive|negative|neutral|UNCERTAIN", "confidence": 1-10}}
Text: {text}
'''
r = client.messages.create(
model='claude-opus-4-5', max_tokens=80,
messages=[{'role': 'user', 'content': prompt}]
)
result = json.loads(r.content[0].text)
if result['sentiment'] == 'UNCERTAIN' or result['confidence'] < uncertainty_threshold:
print(f'Routing to human review: confidence={result["confidence"]}')
return result
print(classify_or_uncertain('It was fine, I guess. Not bad, not great.'))
print(classify_or_uncertain('Absolutely terrible product. Never buying again.'))ความน่าจะเป็นของหมวดหมู่ตามลำดับ
แทนที่จะบังคับให้เลือกหมวดหมู่เดียว ควรขอให้โมเดลจัดอันดับหมวดหมู่ที่เป็นไปได้ทั้งหมดตามความน่าจะเป็น วิธีนี้จะแสดงให้เห็นว่าหมวดหมู่อันดับหนึ่งและอันดับสองใกล้เคียงกันเพียงใด:
def classify_ranked(text, categories):
cats = ', '.join(categories)
prompt = f'''
Classify this text into one of these categories: {cats}
Return ALL categories ranked by likelihood, highest first.
Return JSON: {{"ranked": [{{"category": str, "probability": 0.0-1.0}}]}}
Probabilities must sum to 1.0.
Text: {text}
'''
r = client.messages.create(
model='claude-opus-4-5', max_tokens=150,
messages=[{'role': 'user', 'content': prompt}]
)
result = json.loads(r.content[0].text)
return result['ranked']
cats = ['billing', 'technical', 'general', 'cancellation']
ranked = classify_ranked('I was charged twice and now my account is locked.', cats)
for item in ranked:
print(f'{item["category"]}: {item["probability"]:.0%}')การใช้ส่วนต่างของความน่าจะเป็นเพื่อตรวจจับความกำกวม
ส่วนต่างระหว่างค่าความน่าจะเป็นของหมวดหมู่อันดับหนึ่งและอันดับสองเป็นสัญญาณความกำกวมที่เชื่อถือได้ ช่องว่างเล็กหมายความว่าโมเดลไม่แน่ใจ ส่วนช่องว่างกว้างหมายความว่าโมเดลมีความมั่นใจ:
def classify_with_ambiguity_detection(text, categories, ambiguity_threshold=0.15):
ranked = classify_ranked(text, categories)
top1_prob = ranked[0]['probability']
top2_prob = ranked[1]['probability'] if len(ranked) > 1 else 0
spread = top1_prob - top2_prob
is_ambiguous = spread < ambiguity_threshold
return {
'primary': ranked[0]['category'],
'secondary': ranked[1]['category'] if len(ranked) > 1 else None,
'confidence_spread': round(spread, 3),
'is_ambiguous': is_ambiguous,
'action': 'human_review' if is_ambiguous else 'auto_classify'
}
result = classify_with_ambiguity_detection(
'My upgrade did not apply and I think I was still charged.', ['billing', 'technical', 'general', 'cancellation']
)
print(result)ความไม่แน่นอนแบบมีเงื่อนไข: หากไม่แน่ใจ ให้ถาม
สำหรับแอปพลิเคชันแบบสนทนา แทนที่จะส่งคืน UNCERTAIN โมเดลสามารถขอคำชี้แจงเพิ่มเติมได้:
SYSTEM_CLARIFY = '''
You are a support ticket classifier.
If the customer message is clear, classify it and respond with JSON:
{"action": "classify", "category": str, "confidence": 1-10}
If the message is ambiguous or you are not sure which category applies, respond with:
{"action": "clarify", "question": "A single clarifying question to ask the customer"}
Categories: billing, technical, account, cancellation
Only ask for clarification when genuinely needed. Prefer classification when possible.
'''
def classify_or_ask(message):
r = client.messages.create(
model='claude-opus-4-5', max_tokens=100,
system=SYSTEM_CLARIFY,
messages=[{'role': 'user', 'content': message}]
)
return json.loads(r.content[0].text)
print(classify_or_ask('It is not working anymore.'))
print(classify_or_ask('Cancel my subscription immediately.'))การปรับเทียบความมั่นใจ: อุณหภูมิและความสม่ำเสมอ
การจัดประเภทเดียวกันหลายครั้งด้วยค่าอุณหภูมิที่แตกต่างกันช่วยเปิดเผยความไม่แน่นอนที่แท้จริงของโมเดล ความแปรปรวนสูงหมายถึงข้อมูลเข้ากำกวมอย่างแท้จริง:
from collections import Counter
def calibrated_classify(text, n_samples=5):
results = []
for _ in range(n_samples):
r = client.messages.create(
model='claude-opus-4-5', max_tokens=50,
messages=[{'role': 'user', 'content': f'Classify as positive/negative/neutral. Return JSON: {{"sentiment": str}}\n\n{text}'}]
)
results.append(json.loads(r.content[0].text)['sentiment'])
counts = Counter(results)
dominant = counts.most_common(1)[0]
agreement_rate = dominant[1] / n_samples
return {
'classification': dominant[0],
'agreement_rate': agreement_rate,
'is_uncertain': agreement_rate < 0.7,
'all_results': dict(counts)
}
result = calibrated_classify('The product is okay, nothing special.')
print(result)การกำหนดเส้นทางตามความมั่นใจ
ระบบกำหนดเส้นทางสำหรับใช้งานจริงใช้ระดับความมั่นใจเพื่อส่งต่อไปยังตัวจัดการที่แตกต่างกัน:
def route_by_confidence(text, classify_fn, auto_threshold=8, human_threshold=4):
result = classify_fn(text)
confidence = result.get('confidence', 5)
category = result.get('category') or result.get('sentiment', 'unknown')
if confidence >= auto_threshold:
return {'route': 'auto_process', 'category': category, 'confidence': confidence}
elif confidence >= human_threshold:
return {'route': 'auto_process_with_flag', 'category': category, 'confidence': confidence,
'flag': 'Low confidence — monitor output'}
else:
return {'route': 'human_review', 'category': category, 'confidence': confidence,
'flag': 'Very low confidence — human classification required'}
print(route_by_confidence('Hate this product.', classify_with_confidence))
print(route_by_confidence('It is kind of okay but also not really.', classify_with_confidence))ช่องข้อมูลความไม่แน่นอนแบบมีโครงสร้าง
แบบแผนข้อมูลความไม่แน่นอนที่ครอบคลุมสำหรับเอาต์พุตการจัดประเภท:
UNCERTAINTY_SCHEMA = '''
Return JSON:
{
"primary_category": "string",
"confidence": 1-10,
"uncertainty_type": "none | ambiguous_input | insufficient_context | boundary_case | none",
"alternative_categories": ["string"] or [],
"uncertainty_explanation": "string or null",
"recommended_action": "auto_classify | human_review | request_more_info"
}
Uncertainty types:
- ambiguous_input: The text could clearly mean multiple things
- insufficient_context: Need more information to classify correctly
- boundary_case: The text sits on the border between two categories
- none: Clear classification, no uncertainty
'''
print(UNCERTAINTY_SCHEMA)
print('Use this schema for any classification task requiring uncertainty quantification.')การติดตามความไม่แน่นอนในระบบใช้งานจริง
ติดตามอัตราความไม่แน่นอนในระบบใช้งานจริงเพื่อตรวจจับการเสื่อมลงของพรอมป์ต์หรือการเบี่ยงเบนของหมวดหมู่:
class ClassificationMonitor:
def __init__(self, human_review_threshold=0.15):
self.total = 0
self.uncertain = 0
self.threshold = human_review_threshold
self.category_counts = {}
def record(self, result):
self.total += 1
cat = result.get('category', 'unknown')
self.category_counts[cat] = self.category_counts.get(cat, 0) + 1
if result.get('confidence', 10) < 5 or result.get('sentiment') == 'UNCERTAIN':
self.uncertain += 1
def report(self):
uncertain_rate = self.uncertain / self.total if self.total else 0
alert = uncertain_rate > self.threshold
return {
'total': self.total,
'uncertain_rate': round(uncertain_rate, 3),
'alert': alert,
'category_distribution': self.category_counts
}
monitor = ClassificationMonitor()
print('Production monitoring system defined.')เมื่อใดจึงควรเชื่อมั่นในความมั่นใจระดับสูง
ความมั่นใจระดับสูงของโมเดลไม่ได้หมายความว่าการจัดประเภทจะถูกต้องเสมอไป ต่อไปนี้คือรูปแบบความล้มเหลวที่พบบ่อยแม้มีความมั่นใจสูง:
- อคติอย่างเป็นระบบ: โมเดลติดป้ายกำกับรูปแบบบางอย่างผิดอย่างสม่ำเสมอ และยังมีความมั่นใจสูง
- การเปลี่ยนแปลงโดเมน: โมเดลมีความมั่นใจ แต่รูปแบบข้อมูลเข้าแตกต่างอย่างมากจากข้อมูลที่ใช้ฝึก
- การประจบเอาใจ: โมเดลปรับระดับความมั่นใจตามสิ่งที่ฟังดูดี ไม่ใช่ตามความมั่นใจจริง
ควรประเมินการปรับเทียบความมั่นใจโดยเทียบกับชุดทดสอบที่มีป้ายกำกับเสมอ ไม่ใช่ดูเพียงความแม่นยำ แต่ต้องตรวจด้วยว่าการคาดการณ์ที่มีความมั่นใจสูงแม่นยำกว่าการคาดการณ์ที่มีความมั่นใจต่ำจริงหรือไม่
ตรวจสอบอย่างรวดเร็ว
ส่วนต่างเล็ก ๆ ระหว่างค่าความน่าจะเป็นของหมวดหมู่อันดับหนึ่งและอันดับสองในผลการจัดประเภทบ่งชี้อะไร
ความไม่แน่นอนในการจัดประเภท — ประเด็นสำคัญ
การวัดปริมาณความไม่แน่นอนเปลี่ยนการจัดประเภทจากกล่องดำให้เป็นระบบที่จัดการได้:
- ขอคะแนนความมั่นใจ (1–10) พร้อมการจัดประเภททุกครั้ง อย่าถือว่าเอาต์พุตทั้งหมดมีความน่าเชื่อถือเท่ากัน
- ใช้การตอบกลับ UNCERTAIN กับข้อมูลเข้าที่กำกวมอย่างแท้จริง แทนการบังคับให้เลือกหมวดหมู่
- จัดอันดับหมวดหมู่ทั้งหมดตามความน่าจะเป็น ส่วนต่างระหว่างสองอันดับแรกเป็นสัญญาณความกำกวมที่ดีที่สุด
- ส่งต่อให้มนุษย์ตรวจสอบเมื่อความมั่นใจต่ำกว่าค่าขีดแบ่ง และประมวลผลโดยอัตโนมัติเมื่อสูงกว่าค่าดังกล่าว
- สำหรับแอปสนทนา ให้ถามคำถามเพื่อขอคำชี้แจงแทนการส่งคืน UNCERTAIN
- ติดตามอัตราความไม่แน่นอนในระบบใช้งานจริง อัตราที่เพิ่มขึ้นเป็นสัญญาณว่าพรอมป์ต์เสื่อมลงหรือหมวดหมู่เบี่ยงเบน
- ประเมินการปรับเทียบความมั่นใจโดยเทียบกับข้อมูลที่มีป้ายกำกับเสมอ ไม่ใช่ดูเพียงความแม่นยำ
เรียนรู้ AI Prompt Engineering ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 53
- บทเรียน
- 199
คำถามที่พบบ่อย
บทเรียน “ความมั่นใจและความไม่แน่นอนในการจำแนก” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ความมั่นใจและความไม่แน่นอนในการจำแนก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Prompt Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Prompt Engineering มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ความมั่นใจและความไม่แน่นอนในการจำแนก”
ขอให้โมเดลให้คะแนนความมั่นใจและจัดการการจำแนกที่กำกวม คุณปฏิบัติ AI Prompt Engineering ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Prompt Engineering หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Prompt Engineering บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ความมั่นใจและความไม่แน่นอนในการจำแนก” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน AI Prompt Engineering นี้ได้ไหม
ได้ บทเรียน AI Prompt Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พรอมป์ตสำหรับการดึงเอนทิตีที่มีชื่อ
- การดึงข้อมูลโดยขับเคลื่อนด้วยสคีมา
- LLM ในฐานะตัวจำแนกข้อความ
- ความมั่นใจและความไม่แน่นอนในการจำแนก