Download evaluate.py from yoavipo/nativ-bench: direct link, hf CLI and curl.
- Browser
- Download file 2.4 kB
-
https://huggingface.co/datasets/yoavipo/nativ-bench/resolve/main/evaluate.py
- Command line
-
hf download hf://datasets/yoavipo/nativ-bench/evaluate.py
-
curl -L -o evaluate.py https://huggingface.co/datasets/yoavipo/nativ-bench/resolve/main/evaluate.py
2.4 kB
| """Scores predictions on the benchmark: accuracy and expected calibration error (ECE) per task. | |
| predictions/<task>.jsonl: one line per item, {"source_id": ..., "probabilities": [p_option_1, p_option_2, ...]} | |
| in the item's option order. python evaluate.py predictions/ | |
| """ | |
| import json | |
| import sys | |
| from pathlib import Path | |
| CALIBRATION_BIN_EDGES = [0.0, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] | |
| def load(path): | |
| with path.open(encoding="utf-8") as lines: | |
| return [json.loads(line) for line in lines if line.strip()] | |
| def expected_calibration_error(confidences, correct): | |
| error = 0.0 | |
| for low, high in zip(CALIBRATION_BIN_EDGES, CALIBRATION_BIN_EDGES[1:]): | |
| in_bin = [index for index, confidence in enumerate(confidences) if low <= confidence < high or confidence == high == 1.0] | |
| if in_bin: | |
| accuracy = sum(correct[index] for index in in_bin) / len(in_bin) | |
| confidence = sum(confidences[index] for index in in_bin) / len(in_bin) | |
| error += len(in_bin) / len(confidences) * abs(accuracy - confidence) | |
| return error | |
| def main(): | |
| predictions_directory = Path(sys.argv[1]) | |
| print(f"{'task':12s} {'items':>6s} {'accuracy':>9s} {'ECE':>6s}") | |
| for data_path in sorted((Path(__file__).parent / "data").glob("*.jsonl")): | |
| predictions_path = predictions_directory / data_path.name | |
| if not predictions_path.exists(): | |
| continue | |
| items = load(data_path) | |
| predictions = {prediction["source_id"]: prediction["probabilities"] for prediction in load(predictions_path)} | |
| confidences, correct = [], [] | |
| for item in items: | |
| if item["source_id"] not in predictions: | |
| raise ValueError(f"{predictions_path}: no prediction for {item['source_id']}") | |
| probabilities = predictions[item["source_id"]] | |
| if len(probabilities) != len(item["options"]): | |
| raise ValueError(f"{item['source_id']}: {len(probabilities)} probabilities for {len(item['options'])} options") | |
| choice = max(range(len(probabilities)), key=probabilities.__getitem__) | |
| confidences.append(probabilities[choice]) | |
| correct.append(choice == item["answer_index"]) | |
| print(f"{data_path.stem:12s} {len(items):6d} {sum(correct) / len(items):9.3f} {expected_calibration_error(confidences, correct):6.3f}") | |
| if __name__ == "__main__": | |
| main() | |