Is there a simple way to add multiple metrics to the Trainer feature in Huggingface Transformers library?
Here is the code I am trying to use:
from datasets import load_metric
import numpy as np
def compute_metrics(eval_pred):
metric1 = load_metric(âprecisionâ)
metric2 = load_metric(ârecallâ)
metric3 = load_metric(âf1â)
metric4 = load_metric(âaccuracyâ)
logits, labels = eval_pred
predictions = np.argmax(logits, axis=-1)
precision = metric1.compute(predictions=predictions, references=labels, average="weighted")["precision"]
recall = metric2.compute(predictions=predictions, references=labels, average="weighted")["recall"]
f1 = metric3.compute(predictions=predictions, references=labels, average="weighted")["f1"]
accuracy = metric4.compute(predictions=predictions, references=labels, average="weighted")["accuracy"]
return {"precision": precision, "recall": recall, "f1": f1, "accuracy": accuracy}
I added the average=âweightedâ to each compute() because I got this error previously:
target is multiclass but average=âbinaryâ. please choose another average setting, one of [none, âmicroâ, âmacroâ, âweightedâ].
However after adding average=âweightedâ, I am getting this error in the middle of training:
_compute() got an unexpected keyword argument âaverageâ
Is there a better way to add multiple metrics? The model I am using is google/vit-base-patch16-224-in21k for image classification.