Is the compute metric function error free. Assume imports are available: [code]
- User
Is the compute metric function error free. Assume imports are available:
code# Load required metrics metric_f1 = evaluate.load("f1") metric_bert = evaluate.load("bertscore") metric_meteor = evaluate.load("meteor") # Define the compute_metrics function def compute_metrics(eval_pred): preds, labels = eval_pred # If logits are returned as a tuple, extract the first element if isinstance(preds, tuple): preds = preds[0] # Replace -100 in preds and the labels as we can't decode them preds = np.where(preds != -100, preds, tokenizer.pad_token_id) labels = np.where(labels != -100, labels, tokenizer.pad_token_id) # Decode the predicted tokens into text decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True) decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) # Post-process predictions and labels decoded_preds = [pred.strip() for pred in decoded_preds] decoded_labels = [[label.strip()] for label in decoded_labels] # METEOR and BERTScore expect a list of references # Flatten labels for F1 and METEOR calculation flattened_labels = [label[0] for label in decoded_labels] # Compute F1 Score f1_result = metric_f1.compute(predictions=decoded_preds, references=flattened_labels) f1_score = f1_result["f1"] # Compute METEOR meteor_result = metric_meteor.compute(predictions=decoded_preds, references=flattened_labels) meteor_score = meteor_result["meteor"] # Compute BERTScore (using default 'bert-base-uncased' model) bert_result = metric_bert.compute(predictions=decoded_preds, references=flattened_labels, lang="en") bert_score_f1 = np.mean(bert_result["f1"]) # Combine the metrics result = { "f1": f1_score * 100, # Express F1 as percentage "meteor": meteor_score * 100, # Express METEOR as percentage "bert_score_f1": bert_score_f1 * 100 # Express BERTScore F1 as percentage } return result - Assistant