fhyfhy's picture
Add code/
74b7543 verified
Raw History Blame Contribute Delete
1.08 kB
import numpy as np
def calc_acc(preds, labels, categories):
exact_match = np.mean([1 if pred == label else 0 for pred, label in zip(preds, labels)])
c2stats = {}
for i in range(len(categories)):
if "None" in labels[i]:
continue
if categories[i] not in c2stats:
c2stats[categories[i]] = {"correct": 0, "total": 0}
c2stats[categories[i]]["total"] += 1
if preds[i] == labels[i]:
c2stats[categories[i]]["correct"] += 1
# if preds[i].split("\n\n")[-1] == labels[i].split("\n\n")[-1]:
# c2stats[categories[i]]["cot_answer_correct"] += 1
metrics = {
"exact_match": exact_match,
# "cot_answer_match": cot_answer_match,
# "first_token_match": first_token_match
}
for category, stats in c2stats.items():
metrics[f"{category}_exact_match"] = stats["correct"] / stats["total"]
print(f"Exact match accuracy: {metrics['exact_match']:.2f}")
# print(f"Cot answer match accuracy: {metrics['cot_answer_match']:.2f}")
return metrics