标签生成数据集 🧠

社区文章 发布于 2024年12月20日

2024年8月20日
Zineb MEFTAH
数据集链接

摘要

本文介绍了使用循环细化和逆向策略两种方法创建标签生成数据集。这种方法生成了2,000个样本的数据集,与基线模型相比,在标签相关性和多样性方面表现出更好的性能。

Logo

动机

在我的个性化新闻项目中,我需要为用户点击的文章生成相关标签。这将改善推荐,使体验更加个性化。我尝试使用GPT基础模型,但它们的性能不足以完成这项特定任务。

Problematic

  • 标签太多,其中许多与主题关联度不高。

解决方案

像GPT这样的模型功能强大,但通常需要针对特定任务进行微调。在这种情况下,生成准确的标签需要一个包含文章和标签的数据集。不幸的是,没有合适的现成数据集可用,所以我决定自己创建一个。

方法

该方法受到了以下启发:

方法论

CyclePresentation

  • 以下是我构建数据集的方式:
  1. 种子数据集:我手动创建了一个小型、高质量的种子标签,用于受BBC新闻文章启发的文章。这些标签多样且格式良好。

  2. 合成扩展:使用GPT-4O-2024-08-06,我使用种子数据集生成了100个示例文章。

  3. 循环细化:我在合成数据集上对GPT-4O-Mini-2024-07-18进行了微调。然后,我在循环中细化模型:

    • 使用微调模型为种子标签生成文章(更高质量的种子)。
    • 使用微调模型创建合成数据生成数据集。
    • 在合成数据集上重新微调基础模型。

      这个循环随着时间的推移提高了模型的性能。

  • 最终数据集:一旦模型达到良好的准确性水平,我就使用GPT基础模型生成了一个包含2,000个多样化标签的完整数据集,因为GPT在该方面表现良好,对于每个标签集,我们都使用微调模型生成了相应的文章。这个数据集足够我的项目使用,但该过程可以扩展以创建更大的数据集。

  • 合成数据生成代码

class NewsHeadlines(BaseModel):
    Keywords: str
    Articles: str

import csv

# Initialize the list to store the examples
examples = []

# Manually define the column names based on their order in the file
column_names = ['Keywords', 'Articles']


# Open the CSV file and read its contents
with open('seed.csv', mode='r') as file:
    # Create a CSV reader without headers
    csv_reader = csv.reader(file)

    # Iterate through each row in the CSV file
    for row in csv_reader:
        # Map the row data to the column names
        row_data = dict(zip(column_names, row))

        # Format the data into the required string
        example_string = f"""The news article's keywords: {row_data['Keywords']}, The news article: {row_data['Articles']}"""

        # Append the formatted string as a dictionary to the examples list
        examples.append({"example": example_string})

OPENAI_TEMPLATE = PromptTemplate(input_variables=["example"], template="{example}")

custom_prompt = f"""
Each time create a new set of keywords then generate its news article.
"""

# Create a FewShotPromptTemplate with the custom prompt
prompt_template = FewShotPromptTemplate(
    prefix=custom_prompt,
    examples=examples,
    suffix=SYNTHETIC_FEW_SHOT_SUFFIX,
    input_variables=["subject", "extra"],
    example_prompt=OPENAI_TEMPLATE,
)

synthetic_data_generator = create_openai_data_generator(
    output_schema=NewsHeadlines,
    llm=ChatOpenAI(model="ft:gpt-4o-mini-2024-07-18:open-ai-website:test5:9w9ELVAc", temperature=1),
    prompt=prompt_template,
)

synthetic_results = synthetic_data_generator.generate(
    subject="News Articles",
    extra="The keywords and news articles should be varied, diversed, and not repeated.",
    runs=100,
)
  • 微调过程是在OpenAI平台进行的。

  • 比较:我们尝试在一个新闻文章上进行简单比较,使用我们基于自己的数据集微调的模型从中提取标签,以下是过程的截图:

GPT40mini

GPT40

Fine-tuned model

模型 比较 效率
gpt-4o-mini 关键词过多,重复,有些相关性弱
gpt-4o 有所改进,但仍包含一些不相关的关键词 中型
微调模型 无重复;关键词相关性提高

用例

这个微调模型除了我的项目之外还有其他应用:

  • 搜索引擎可以利用它来更好地进行索引。
  • 用于博客、新闻或学术内容的自动化标签系统。
  • 任何需要高效文本分类的系统。

在我的项目中集成

在我的新闻项目中,每当用户点击文章时,模型都会生成标签。这些标签被存储起来以细化用户档案,确保未来的推荐更相关、更个性化。


语言(自然语言处理)

英语


参考文献与致谢


许可

本数据集依据知识共享署名 4.0 国际(CC BY 4.0)许可。

社区

注册登录 发表评论