Qwen3-4B-LoopIntervention / scripts /build_corr_data.py
tysgydke's picture
Add HF card; mirror of ModelScope tysgydk/Qwen3-4B-LoopIntervention
bb098f4 verified
Raw History Blame Contribute Delete
1.83 kB
# 构造纠错训练配对数据:蓝莓r2错误输出 -> 修正+标准答案
# 输入: eval 收集的 jsonl(含 text/pred/gold/ok)
# 输出: 训练 jsonl, 每条形态:
# "题目\n{蓝莓错误推理全文}\n\nWait, let me double-check that.\n{标准答案推理}"
# 只取 ok=False 的错误样本;超512tok的截断(修正段优先保留)
import json, sys, re
SRC = sys.argv[1] if len(sys.argv) > 1 else \
'/root/loop-exp/math500_intervmix_step4000_r2_cap1024_n200.jsonl'
OUT = sys.argv[2] if len(sys.argv) > 2 else \
'/root/autodl-tmp/data/math_corr_v1.jsonl'
MAXLEN = 512
ROWS = [json.loads(l) for l in open(SRC)]
PROBS = [json.loads(l) for l in open('/root/autodl-tmp/data/math500/test.jsonl')]
def build_correction(wrong_text, gold_ans):
"""构造 错误推理 + 修正触发 + 标准答案 的文档。"""
# 去掉模型输出的系统无关前缀(保留 think 完整链)
t = wrong_text.strip()
# gold 是标准答案,直接作为修正结论
return f'{t}\n\nWait, let me double-check that. The correct answer is {gold_ans}.'
n_wrong = 0
docs = []
for r in ROWS:
if r['ok']:
continue
i = r['i']
gold = PROBS[i]['answer'].strip() if i < len(PROBS) else None
if gold is None:
continue
doc = build_correction(r['text'], gold)
docs.append({'text': doc, 'kind': 'math_corr', 'i': i, 'gold': gold})
n_wrong += 1
# 截断保护:只保留短错误(推理全文 < 1536 字符 ≈ 400 token,训练窗口 512 内装得下)
MAXCHARS = 1536
kept = []
for d in docs:
if len(d['text']) <= MAXCHARS:
kept.append(d)
with open(OUT, 'w') as f:
for d in kept:
f.write(json.dumps(d, ensure_ascii=False) + '\n')
print(json.dumps({'src_n': len(ROWS), 'wrong': n_wrong, 'kept': len(kept),
'out': OUT}, ensure_ascii=False))