在Bert分类中如何获取预测准确率

我在我的聊_bot项目中使用Bert分类器。我对传入的文本消息执行了必要的分词操作。然后我将其插入模型并进行预测。我如何获得这个估计的准确率?

for text in test_texts:    encoded_dict = tokenizer.encode_plus(        text,        add_special_tokens=True,        max_length=max_len,        pad_to_max_length=True,        return_attention_mask=True,        return_tensors='pt',    )    input_ids.append(encoded_dict['input_ids'])    attention_masks.append(encoded_dict['attention_mask'])input_ids = torch.cat(input_ids, dim=0)attention_masks = torch.cat(attention_masks, dim=0)print("input_ids ",input_ids)print("attention_masks ",attention_masks)batch_size = 32prediction_data = TensorDataset(input_ids, attention_masks)prediction_sampler = SequentialSampler(prediction_data)prediction_dataloader = DataLoader(prediction_data, sampler=prediction_sampler, batch_size=batch_size)print("prediction_data ",prediction_data)print("prediction_sampler ",prediction_sampler)print("prediction_dataloader ",prediction_dataloader)model.eval()predictions, true_labels = [], []for batch in prediction_dataloader:    batch = tuple(t.to(device) for t in batch)    b_input_ids, b_input_mask = batch    print("b input ids",b_input_ids)    with torch.no_grad():        outputs = model(b_input_ids, token_type_ids=None,                        attention_mask=b_input_mask.to(device))    logits = outputs[0]    logits = logits.detach().cpu().numpy()    label_ids = b_input_mask.to('cpu').numpy()    predictions.append(logits)    true_labels.append(label_ids)    print("logits ",logits)    print("label_ids ",label_ids)    print("true_labels ",true_labels)print('Prediction completed')prediction_set = []for i in range(len(true_labels)):    pred_labels_i = np.argmax(predictions[i], axis=1).flatten()    prediction_set.append(pred_labels_i)prediction= [item for sublist in prediction_set for item in sublist]print("prediction:", prediction[0])

我正在寻找一个百分比值。根据这个百分比值的结果,我会做出回应或通过。


回答:

可以直接使用一些库来计算准确率。

例如,你可以使用sklearn:

from sklearn.metrics import accuracy_scoreprint("Accuracy:", accuracy_score(true_labels, predictions)) # 值在0和1之间print("Accuracy Percentage {} %:".format(100*accuracy_score(true_labels, predictions))) # 值在0和100之间

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注