6 nlp machine-learning python-3.x pytorch huggingface-transformers
这个问题与How can I check a fusion_matrix afterfine-tuning with custom datasets?相同。,在数据科学堆栈交换上。
我想在使用自定义数据集进行微调后检查一个混淆矩阵,包括精度、召回率和 f1 分数,如下所示。
微调过程和任务是Hugging Face 上的使用自定义数据集微调教程上的IMDb 评论的序列分类。
用Trainer完成微调后,在这种情况下如何检查confusion_matrix?
fusion_matrix 的图像,包括精度、召回率和 f1-score原始站点:仅作为输出图像的示例
predictions = np.argmax(trainer.test(test_x), axis=1)
# Confusion matrix and classification report.
print(classification_report(test_y, predictions))
precision recall f1-score support
0 0.75 0.79 0.77 1000
1 0.81 0.87 0.84 1000
2 0.63 0.61 0.62 1000
3 0.55 0.47 0.50 1000
4 0.66 0.66 0.66 1000
5 0.62 0.64 0.63 1000
6 0.74 0.83 0.78 1000
7 0.80 0.74 0.77 1000
8 0.85 0.81 0.83 1000
9 0.79 0.80 0.80 1000
avg / total 0.72 0.72 0.72 10000
Run Code Online (Sandbox Code Playgroud)
predictions = np.argmax(trainer.test(test_x), axis=1)
# Confusion matrix and classification report.
print(classification_report(test_y, predictions))
precision recall f1-score support
0 0.75 0.79 0.77 1000
1 0.81 0.87 0.84 1000
2 0.63 0.61 0.62 1000
3 0.55 0.47 0.50 1000
4 0.66 0.66 0.66 1000
5 0.62 0.64 0.63 1000
6 0.74 0.83 0.78 1000
7 0.80 0.74 0.77 1000
8 0.85 0.81 0.83 1000
9 0.79 0.80 0.80 1000
avg / total 0.72 0.72 0.72 10000
Run Code Online (Sandbox Code Playgroud)
使用 IMDb Reviews准备序列分类的数据集,并且我正在使用 Trainer 进行微调。
from pathlib import Path
def read_imdb_split(split_dir):
split_dir = Path(split_dir)
texts = []
labels = []
for label_dir in ["pos", "neg"]:
for text_file in (split_dir/label_dir).iterdir():
texts.append(text_file.read_text())
labels.append(0 if label_dir is "neg" else 1)
return texts, labels
train_texts, train_labels = read_imdb_split('aclImdb/train')
test_texts, test_labels = read_imdb_split('aclImdb/test')
from sklearn.model_selection import train_test_split
train_texts, val_texts, train_labels, val_labels = train_test_split(train_texts, train_labels, test_size=.2)
from transformers import DistilBertTokenizerFast
tokenizer = DistilBertTokenizerFast.from_pretrained('distilbert-base-uncased')
train_encodings = tokenizer(train_texts, truncation=True, padding=True)
val_encodings = tokenizer(val_texts, truncation=True, padding=True)
test_encodings = tokenizer(test_texts, truncation=True, padding=True)
import torch
class IMDbDataset(torch.utils.data.Dataset):
def __init__(self, encodings, labels):
self.encodings = encodings
self.labels = labels
def __getitem__(self, idx):
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
item['labels'] = torch.tensor(self.labels[idx])
return item
def __len__(self):
return len(self.labels)
train_dataset = IMDbDataset(train_encodings, train_labels)
val_dataset = IMDbDataset(val_encodings, val_labels)
test_dataset = IMDbDataset(test_encodings, test_labels)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您可以做的是迭代验证集(或测试集)并手动创建y_true和的列表y_pred。
import torch
import torch.nn.functional as F
from sklearn import metrics
y_preds = []
y_trues = []
for index,val_text in enumerate(val_texts):
tokenized_val_text = tokenizer([val_text],
truncation=True,
padding=True,
return_tensor='pt')
logits = model(tokenized_val_text)
prediction = F.softmax(logits, dim=1)
y_pred = torch.argmax(prediction).numpy()
y_true = val_labels[index]
y_preds.append(y_pred)
y_trues.append(y_true)
Run Code Online (Sandbox Code Playgroud)
最后,
confusion_matrix = metrics.confusion_matrix(y_trues, y_preds, labels=["neg", "pos"]))
print(confusion_matrix)
Run Code Online (Sandbox Code Playgroud)
观察结果:
logits,而不是标准化的概率。softmax一维来转换为实际概率(例如0.2% class 0,0.8% class 1)。.argmax()操作来获取类的索引。