Download eval.py from 56m/NumPuzzle: direct link, hf CLI and curl.
- Browser
- Download file 3.2 kB
-
https://huggingface.co/datasets/56m/NumPuzzle/resolve/main/eval.py
- Command line
-
hf download hf://datasets/56m/NumPuzzle/eval.py
-
curl -L -o eval.py https://huggingface.co/datasets/56m/NumPuzzle/resolve/main/eval.py
3.2 kB
| import re | |
| from lighteval.tasks.lighteval_task import LightevalTaskConfig | |
| from lighteval.tasks.requests import Doc | |
| from lighteval.metrics.metrics import SampleLevelMetric | |
| # ========================================== | |
| # 1. カスタム評価ロジック(任意の長さに対応) | |
| # ========================================== | |
| def numpuzzle_match(predictions: list[str], formatted_doc: dict, **kwargs) -> dict: | |
| """ | |
| モデルが生成したテキスト(predictions)と、正解(formatted_doc)を比較する関数。 | |
| """ | |
| # 正解を取得し、その長さ(桁数)を取得 | |
| target = str(formatted_doc["target"]).strip() | |
| target_len = len(target) # ★ここがポイント:9固定ではなく正解の長さに依存させる | |
| # モデルの生成結果(通常はリストの最初の要素) | |
| prediction_raw = str(predictions[0]) | |
| # 数字以外をすべて削除 | |
| prediction_digits = re.sub(r'\D', '', prediction_raw) | |
| # ★正解と同じ桁数を末尾から抽出する | |
| if len(prediction_digits) >= target_len: | |
| final_pred = prediction_digits[-target_len:] | |
| else: | |
| final_pred = prediction_digits | |
| is_match = (final_pred == target) | |
| # 正解なら 1.0、不正解なら 0.0 を返す(これをLighteval全体で平均化してAccuracyを出します) | |
| return {"accuracy": 1.0 if is_match else 0.0} | |
| # 評価関数をLightevalのメトリクスとして登録 | |
| numpuzzle_metric = SampleLevelMetric( | |
| metric_name="numpuzzle_accuracy", | |
| sample_level_fn=numpuzzle_match, | |
| category="accuracy", | |
| use_case="generative" | |
| ) | |
| # ========================================== | |
| # 2. プロンプト生成関数 | |
| # ========================================== | |
| def numpuzzle_prompt(line, task_name: str = None): | |
| """ | |
| データセットの1行(line)を受け取り、モデルに入力するプロンプト形式を作ります。 | |
| ※ line['input'] は、あなたのデータセットの「問題文」の列名に合わせて変更してください。 | |
| """ | |
| return Doc( | |
| task_name=task_name, | |
| query=f"{line['input']}\nAnswer:", # 例: "1+1=?\nAnswer:" | |
| choices=[str(line["target"])], | |
| gold_index=0, | |
| instruction="", | |
| ) | |
| # ========================================== | |
| # 3. タスク設定(Lightevalへ登録) | |
| # ========================================== | |
| task = LightevalTaskConfig( | |
| name="numpuzzle-easy", | |
| prompt_function=numpuzzle_prompt, | |
| suite=["custom"], | |
| hf_repo="your-username/numpuzzle-dataset", # ★Hugging Face上のあなたのデータセットIDに変更してください | |
| hf_subset="default", | |
| hf_avail_splits=["test"], # 評価に使用するスプリット(testやtrain) | |
| evaluation_splits=["test"], | |
| few_shots_split=None, | |
| few_shots_select=None, | |
| generation_size=65536, # モデルに生成させる最大トークン数(長すぎると遅くなります) | |
| metric=[numpuzzle_metric], # 上で定義したカスタム評価関数をセット | |
| ) | |
| # Lightevalがこのファイルを読み込んだ時にタスクを認識できるようにする | |
| TASKS_TABLE = [task] |