Browse Source

train_agent: match numeric gt by value, not by substring

validate_gt_in_text accepted a ground truth when it appeared anywhere in the
answer as a substring, so a numeric gt of 9 matched "19", 100 matched
"1000" and 5 matched "2.5": a wrong answer got the full GT reward and the
policy could exploit it. A numeric gt now only matches a number of equal
value. The number scan also used \w as its boundary, which treats Chinese
characters as word characters, so "答案是59。" yielded no number at all;
the boundary now only looks at ASCII letters, digits, '_' and decimals.
FeiyangP 6 ngày trước cách đây
mục cha
commit
e888e12dab
1 tập tin đã thay đổi với 3 bổ sung và 2 xóa
  1. 3 2
      trainer/train_agent.py

+ 3 - 2
trainer/train_agent.py

@@ -183,9 +183,10 @@ def rollout_batch(rollout_engine, tokenizer, messages_batch, tools_batch, num_ge
 
 # ======== Reward 计算 ========
 def validate_gt_in_text(text, gt_list):
+    # 数值型 gt 只按数值比对(子串会让 gt=9 命中 "19"、gt=100 命中 "1000");数字边界只看 ASCII 字母数字,中文紧挨数字("答案是59。")也能取到
     text, text_num = str(text), str(text).replace(',', '')
-    nums = [float(x) for x in re.findall(r'(?<![\w.])[-+]?\d+(?:\.\d+)?(?![\w.])', text_num)]
-    return {g for g in gt_list if ((s := str(g).strip()) and s.lower() in text.lower()) or (re.fullmatch(r'[-+]?\d+(?:\.\d+)?', str(g).strip().replace(',', '')) and any(abs(float(str(g).strip().replace(',', '')) - n) < 1e-6 for n in nums))}
+    nums = [float(x) for x in re.findall(r'(?<![A-Za-z0-9_.])[-+]?\d+(?:\.\d+)?(?![A-Za-z0-9_]|\.\d)', text_num)]
+    return {g for g in gt_list if (s := str(g).strip()) and (any(abs(float(s.replace(',', '')) - n) < 1e-6 for n in nums) if re.fullmatch(r'[-+]?\d+(?:\.\d+)?', s.replace(',', '')) else s.lower() in text.lower())}
 
 def calculate_rewards(prompts, completions, gt_batch, tools_batch, num_gen, reward_model=None, device="cuda", turn_outputs_batch=None, unfinished_batch=None):
     rewards = torch.zeros(len(completions), device=device)