evaluateライブラリ

Pythonで学ぶ LLM 入門

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

evaluateライブラリ

import evaluate

accuracy = evaluate.load("accuracy")
print(accuracy.description)
Accuracy is the proportion of correct
predictions among the total number of cases
processed. It can be computed with:
Accuracy = (TP + TN) / (TP + TN + FP + FN)
Where:
TP: True positive
TN: True negative
FP: False positive
FN: False negative

 

  • 指標(Metric): 正解データに基づいてモデル性能を評価

 

  • 比較(Comparison): 2つのモデルを比較

 

  • 測定(Measurement): データセットの特性を把握
Pythonで学ぶ LLM 入門

featuresアトリビュート

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

指標が必要とする入力の確認

  • 'predictions':モデルの出力
  • 'references':正解データ
  • .features:クラスラベルでサポートされる型(例:'int32' または '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)}
Pythonで学ぶ LLM 入門

LLMタスクと評価指標

 

言語タスクの評価指標

Pythonで学ぶ LLM 入門

LLMタスクと評価指標

 

言語タスクの評価指標

Pythonで学ぶ LLM 入門

分類の評価指標

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]
Pythonで学ぶ LLM 入門

指標の出力結果

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}
Pythonで学ぶ LLM 入門

ファインチューニング済みモデルの評価

# Load saved model and tokenizer with 
# .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}
Pythonで学ぶ LLM 入門

適切な指標の選択

 

  • 注意: 各指標には固有の_洞察があり、限界_も存在します

 

  • 網羅的に: 複数の指標を組み合わせて使用(可能であればKPIも活用)

認識・思考・意思決定を示す、電球付きの脳のイラスト。

Pythonで学ぶ LLM 入門

練習しましょう!

Pythonで学ぶ LLM 入門

Preparing Video For Download...