Thư viện evaluate

Nhập môn LLMs trong Python

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

Thư viện evaluate

import evaluate

accuracy = evaluate.load("accuracy")
print(accuracy.description)
Accuracy là tỷ lệ dự đoán đúng
trên tổng số trường hợp được xử lý.
Công thức:
Accuracy = (TP + TN) / (TP + TN + FP + FN)
Trong đó:
TP: Dương tính thật
TN: Âm tính thật
FP: Dương tính giả
FN: Âm tính giả

 

  • Chỉ số: đánh giá hiệu năng so với nhãn đúng

 

  • So sánh: đối chiếu hai mô hình

 

  • Đo lường: hiểu đặc tính dữ liệu
Nhập môn LLMs trong Python

Thuộc tính features

print(accuracy.features)
{'predictions': Value(dtype='int32', id=None),
 'references': Value(dtype='int32', id=None)}

Xem đầu vào bắt buộc của một chỉ số

  • 'predictions': đầu ra mô hình
  • 'references': nhãn đúng
  • .features: kiểu nhãn hỗ trợ, ví dụ 'int32' hoặc 'float32'
f1 = evaluate.load("f1")
print(f1.features)
{'predictions': Value(dtype='int32', id=None),
 'references': Value(dtype='int32', id=None)}
pearson_corr = evaluate.load("pearsonr")
print(pearson_corr.features)
{'predictions': Value(dtype='float32', id=None),
'references': Value(dtype='float32', id=None)}
Nhập môn LLMs trong Python

Tác vụ LLM và chỉ số

 

Các chỉ số đánh giá cho tác vụ ngôn ngữ

Nhập môn LLMs trong Python

Tác vụ LLM và chỉ số

 

Các chỉ số đánh giá cho tác vụ ngôn ngữ

Nhập môn LLMs trong Python

Chỉ số phân loại

accuracy = evaluate.load("accuracy")
precision = evaluate.load("precision")
recall = evaluate.load("recall")
f1 = evaluate.load("f1")
from transformers import pipeline

classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)

predictions = classifier(evaluation_text)

predicted_labels = [1 if pred["label"] == "POSITIVE" else 0 for pred in predictions]
Nhập môn LLMs trong Python

Kết quả chỉ số

real_labels = [0,1,0,1,1]
predicted_labels = [0,0,0,1,1]

print(accuracy.compute(references=real_labels, predictions=predicted_labels))
print(precision.compute(references=real_labels, predictions=predicted_labels))
print(recall.compute(references=real_labels, predictions=predicted_labels))
print(f1.compute(references=real_labels, predictions=predicted_labels))
{'accuracy': 0.8}
{'precision': 1.0}
{'recall': 0.6666666666666666}
{'f1': 0.8}
Nhập môn LLMs trong Python

Đánh giá model đã fine-tune

# Tải model và tokenizer đã lưu bằng
# .from_pretrained("my_finetuned_files")


new_data = ["This is movie was disappointing!", "This is the best movie ever!"] new_input = tokenizer(new_data, return_tensors="pt", padding=True, truncation=True, max_length=64) with torch.no_grad(): outputs = model(**new_input) predicted = torch.argmax(outputs.logits, dim=1).tolist()
real = [0,1]
print(accuracy.compute(references=real,
                       predictions=predicted))
print(precision.compute(references=real,
                        predictions=predicted))
print(recall.compute(references=real,
                     predictions=predicted))
print(f1.compute(references=real, 
                 predictions=predicted))
{'accuracy': 1.0}
{'precision': 1.0}
{'recall': 1.0}
{'f1': 1.0}
Nhập môn LLMs trong Python

Chọn chỉ số phù hợp

 

  • Cần nhận thức: mỗi chỉ số cho góc nhìn riêng và có giới hạn

 

  • Hãy toàn diện: kết hợp nhiều chỉ số (và KPI theo miền nếu có)

Minh họa bộ não với bóng đèn thể hiện sự nhận thức, suy nghĩ và quyết định.

Nhập môn LLMs trong Python

Ayo berlatih!

Nhập môn LLMs trong Python

Preparing Video For Download...