重新审视 TemplateGSM:通过基于模板的数据生成提升语言模型的数学推理能力
作者:Yifan Zhang 等
最初发布:2024 年 2 月
重新审视:2024 年 11 月
2024 年 2 月,我们推出了 TemplateGSM,这是一个旨在提升语言模型数学推理能力的开创性数据集。自那时起,该领域持续快速发展。我们重新审视 TemplateGSM,以强调其贡献,并更深入地了解支撑该数据集的**基于模板的数据生成(TDG)**方法。
项目资源
- 项目主页:https://templatemath.github.io
- 论文链接:https://templatemath.github.io/TemplateMath_Part_I.pdf
- GitHub 仓库:https://github.com/iiis-ai/TemplateMath
- Hugging Face 上的数据集:https://huggingface.co/datasets/math-ai/TemplateGSM
引言
随着 GPT-3、PaLM 和 Llama 等大型语言模型 (LLM) 的出现,自然语言处理 (NLP) 领域取得了显著进展。这些模型在语言理解和生成方面展示了前所未有的能力。然而,在需要复杂推理的任务中,特别是数学问题解决方面,这些模型往往表现不佳。一个重要的障碍是缺乏大规模、高质量、领域特定的数据集,而这些数据集对于训练模型以开发复杂的推理能力至关重要。
为了解决这一挑战,我们引入了**基于模板的数据生成(TDG)**,这是一种利用 GPT-4 自动生成参数化元模板的新颖方法。这些元模板作为基础结构,用于合成大量高质量的问题和解决方案,有效地将数据增强提升到一个新的水平。
利用 TDG,我们推出了 **TemplateGSM**,一个包含超过 **700 万**个合成生成的学龄期数学问题的数据集。每个问题都附带基于代码和自然语言的解决方案,为训练和评估 LLM 在数学推理方面的能力提供了丰富的资源。
什么是基于模板的数据生成(TDG)?
概述
基于模板的数据生成(TDG)是一种方法,旨在通过利用参数化模板系统地生成大量数学问题及其相应的解决方案。通过使用 GPT-4 生成这些元模板,我们捕获了各种问题结构和语言风格。通过在这些 GPT-4 生成的模板中改变参数,TDG 确保了生成数据在可伸缩性和质量方面的双重优势。
方法
TDG 过程涉及几个关键组件,这些组件协同工作以生成高质量的数学数据集
1. 使用 GPT-4 生成元模板
我们首先利用 GPT-4 生成封装各种数学问题类型的元模板。这些模板包含变量组件的占位符,如名称、数量、项目、日期和位置。GPT-4 先进的语言生成能力使我们能够生成多样化的模板,涵盖广泛的数学概念。
2. 参数生成
为了实例化模板,我们开发了生成满足特定条件的参数的函数,以确保问题的可解性和有效性。参数经过精心选择,以避免琐碎或过于复杂的问题,从而在目标教育水平上取得平衡。
3. 问题实例化
将生成的参数替换到 GPT-4 生成的元模板中,以创建具体的题目陈述。每个实例化的问题在细节上都是独特的,但保留了模板定义的结构特征。
4. 解决方案生成与验证
对于每个问题,我们生成解决方案代码(通常是 Python 代码),该代码可以自动解决问题。通过执行解决方案代码,我们获得结果并验证解决方案的正确性。除了基于代码的解决方案之外,我们还生成自然语言解释,描述解决方案步骤,而不使用代码。
示例
下面是我们 TDG 方法的一个示例。代码片段演示了我们如何生成涉及连续两个月销售额的问题。这种问题类型的元模板是使用 GPT-4 生成的,它捕获了可以通过参数替换来变化的现实场景。
def generate_problem_and_solution_code():
# Lists of random terms
months = ["January and February", "March and April", "May and June",
"July and August", "September and October", "November and December"]
# Get initial amount and subsequent ratio
initial_amount = random.randint(50, 150)
subsequent_ratio = random.choice([0.5, 0.6, 0.7, 0.8, 0.9])
# Randomly select terms
name = random.choice(first_names) + ' ' + random.choice(last_names)
item = random.choice(items)
month = random.choice(months)
year = random.randint(2010, 2023)
place = random.choice(places)
county = random.choice(us_counties)
county_name = county['CountyName'] + ", " + county["StateName"]
# Construct problem statement
problem_statement = f"{name} sold {initial_amount} {item} in {month.split(' and ')[0]}, {year} at {place} in {county_name}. "
problem_statement += f"In {month.split(' and ')[1]}, they sold {int(subsequent_ratio*100)}% of the amount sold in the previous month. "
problem_statement += f"How many {item} did {name} sell in total during {month}?"
# Generate solution code
solution_code = f"""# Number of {item} sold by {name} in {month.split(' and ')[0]}, {year}
{item}_sold_in_{month.split(' and ')[0]} = {initial_amount}
# Sales ratio for the next month
{item}_ratio = {subsequent_ratio}
# Calculating the amount of {item} sold in {month.split(' and ')[1]}
subsequent_{item}_sold = {item}_sold_in_{month.split(' and ')[0]} * {item}_ratio
# Calculating the total number of {item} sold during {month}
total_{item}_sold = {item}_sold_in_{month.split(' and ')[0]} + subsequent_{item}_sold
result = total_{item}_sold
"""
# Execute the solution code
exec_globals = {}
exec(solution_code, {}, exec_globals)
result = round(exec_globals['result'])
# Generate the solution without code
solution_wocode = f"{name} sold {initial_amount} {item} in {month.split(' and ')[0]}, {year}. "
solution_wocode += f"In {month.split(' and ')[1]}, they sold {int(subsequent_ratio*100)}% of the previous month's sales, which is {round(subsequent_ratio*initial_amount)} {item}. "
solution_wocode += f"In total, they sold {initial_amount} + {round(subsequent_ratio*initial_amount)} = {result} {item} during {month}."
return problem_statement, solution_code, result, solution_wocode
生成的问题和解决方案示例
问题陈述
2021年3月,Alex Johnson在加利福尼亚州奥兰治县的书店里卖出了120本书。4月,他们卖出了上个月销量的80%。Alex Johnson在3月和4月总共卖出了多少本书?
解决方案代码
# Number of books sold by Alex Johnson in March, 2021
books_sold_in_March = 120
# Sales ratio for the next month
books_ratio = 0.8
# Calculating the amount of books sold in April
subsequent_books_sold = books_sold_in_March * books_ratio
# Calculating the total number of books sold during March and April
total_books_sold = books_sold_in_March + subsequent_books_sold
result = total_books_sold
自然语言解决方案
Alex Johnson 在 2021 年 3 月卖出了 120 本书。4 月,他们卖出了上个月销量的 80%,即 96 本书。因此,他们在 3 月和 4 月总共卖出了 120 + 96 = 216 本书。
TDG 的优势
TDG 方法具有以下几个显著优势
- 可伸缩性:TDG 通过改变 GPT-4 生成的模板中的参数,能够有效地生成无限量的数据。
- 质量保证:通过使用代码执行进行解决方案验证,我们确保每个问题都有正确可靠的解决方案。
- 多样性:使用 GPT-4 生成元模板引入了各种问题结构和语言风格。
- 数据增强的提升:通过将 GPT-4 整合到模板生成过程中,我们将数据增强提升到一个新的水平,从而能够合成既多样化又高质量的数据。
数据集结构
TemplateGSM 根据每个模板生成的问题数量进行配置。
配置
- templategsm-1000-1k:包含从前 1,000 个模板生成的 1,000 个问题。
- templategsm-2000-1k:包含从前 2,000 个模板生成的 1,000 个问题。
- templategsm-4000-1k:包含从前 4,000 个模板生成的 1,000 个问题。
- templategsm-7473-1k:包含从所有 7,473 个模板中生成的 1,000 个问题。
数据字段
数据集中的每个问题都包含以下字段
problem
:问题陈述。solution_code
:解决问题的带注释的 Python 代码。result
:问题的最终答案。solution_wocode
:不使用代码的自然语言解释的解决方案。source
:表示问题生成中使用的数据源和种子。template_id
:生成问题的模板 ID。problem_id
:问题在其模板中的唯一索引。
如何使用 TemplateGSM
数据集可在 Hugging Face 数据集上获取,并可使用 datasets
库轻松访问。
安装
首先,如果尚未安装,请安装 Hugging Face Datasets 库
pip install datasets
加载数据集
您可以按如下方式加载 TemplateGSM 数据集的特定配置
from datasets import load_dataset
# Load a specific configuration
dataset = load_dataset("math-ai/TemplateGSM", "templategsm-4000-1k")
将 "templategsm-4000-1k"
替换为任何其他有效的配置名称,例如 "templategsm-1000-1k"
或 "templategsm-7473-1k"
。
探索数据
以下是您探索数据集的方式
# Access the first problem
first_problem = dataset['train'][0]
print("Problem Statement:\n", first_problem['problem'])
print("\nSolution Code:\n", first_problem['solution_code'])
print("\nNatural Language Solution:\n", first_problem['solution_wocode'])
print("\nFinal Answer:\n", first_problem['result'])
示例
数据集中的一个问题及其解决方案示例
问题陈述
杰米从市场买了 15 个苹果。她给了朋友 5 个苹果,然后又买了 3 个苹果。现在杰米有多少个苹果?
解决方案代码
# Initial number of apples Jamie bought
initial_apples = 15
# Apples given to friend
apples_given = 5
# Apples bought later
apples_bought_later = 3
# Calculating the remaining apples after giving some away
apples_after_giving = initial_apples - apples_given
# Total apples Jamie has now
total_apples = apples_after_giving + apples_bought_later
result = total_apples
自然语言解决方案
杰米最初买了15个苹果。她给了朋友5个苹果,所以她还剩下15-5=10个苹果。然后她又买了3个苹果,所以现在她有10+3=13个苹果。
最终答案
13
访问数据集
您可以在 Hugging Face 上访问 TemplateGSM 数据集
https://huggingface.co/datasets/math-ai/TemplateGSM
许可
TemplateGSM 根据知识共享署名 4.0 国际 (CC BY 4.0) 许可协议提供。
引用
如果您在研究或应用程序中使用了基于模板的数据生成 (TDG) 或 TemplateGSM 数据集,请考虑引用以下内容
@article{zhang2024training,
title={Training and Evaluating Language Models with Template-based Data Generation},
author={Zhang, Yifan and et al.},
journal={arXiv preprint},
year={2024}
}
总结
TemplateGSM 代表了在自然语言处理和数学推理之间架起桥梁的重要一步。通过提供一个海量、多样化且高质量的数学问题和解决方案数据集,我们旨在赋能研究人员和从业者开发具有增强问题解决能力的语言模型。
TDG 方法,凭借其利用 GPT-4 生成元模板的特点,引入了一种新颖的数据增强方法,确保了多样性和可扩展性。我们鼓励研究界利用 TemplateGSM 进行 LLM 在数学推理任务中的训练、微调和评估。
携手共进,我们可以提升人工智能系统理解和解决复杂问题的能力,使我们更接近真正智能的机器。
如需更多详情和贡献,请访问我们的 GitHub 仓库
https://github.com/iiis-ai/TemplateMath
祝您编码和建模愉快!