基于通用LLM合成数据生成

使用大型语言模型 (LLM) 生成合成数据为一个常见问题提供了强大的解决方案:提供高质量、多样化且符合隐私要求的数据。这可以用于许多场景,例如训练数据科学机器学习模型(SVM、决策树、KNN)、在数据上微调不同的 GPT 模型、作为冷启动问题的解决方案、帮助使用真实数据构建引人注目的演示/应用程序、场景测试等。

有许多关键驱动因素可能会促使您想要利用合成数据。

  1. 人类数据可能包含我们不希望使用的隐私限制和/或可识别数据。
  2. 合成数据比真实数据更加结构化,因此更容易操作。
  3. 在数据稀疏或某些类别的数据稀疏的领域,我们可能希望增强数据。
  4. 当处理不平衡的数据集或缺乏多样性的数据集时,我们可能希望创建数据来提高数据集的丰富度。

与传统的数据增强或手动数据创建方法不同,使用 LLM 可以生成丰富、细致入微且与上下文相关的数据集,从而显著增强其对企业和开发人员的实用性。

我们将本教程分为两部分。在本指南中,我们将制定以下议程:

  1. 带有结构化提示的 CSV
  2. 使用 Python 程序生成 CSV
  3. 使用 Python 程序实现多表 CSV
  4. 简单创建文本数据
  5. 在第 2部分中,我们将处理不平衡或非多样化的文本数据,并研究获取更好文本数据的提示策略。

最后两种方法尤其适用于创建合成数据来微调另一个 GPT 模型。例如,使用生成的更高质量的数据来gpt-4o更便宜、更快速地微调模型gpt-3.5-turbo,以提高性能并降低成本。

开始设置
%pip install openai
%pip install pandas
%pip install scikit-learn
%pip install matplotlib

from openai import OpenAI
import os
import re
import numpy as np
import pandas as pd
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import json
import matplotlib

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "<your OpenAI API key if not set as env var>"))

1. 带有结构提示的 CSV

这里我们以最简单的方式创建数据。您可以通过解决 3 个关键点来快速生成数据:告诉它数据的格式 (CSV)、架构以及有关列如何关联的有用信息(LLM 将能够从列名称中推断出这一点,但帮助会提高性能)。

datagen_model = "gpt-4o-mini"
question = """
Create a CSV file with 10 rows of housing data.
Each row should include the following fields:
 - id (incrementing integer starting at 1)
 - house size (m^2)
 - house price
 - location
 - number of bedrooms

Make sure that the numbers make sense (i.e. more rooms is usually bigger size, more expensive locations increase price. more size is usually higher price etc. make sure all the numbers make sense). Also only respond with the CSV.
"""

response = client.chat.completions.create(
  model=datagen_model,
  messages=[
    {
   
   "role": "system", "content": "You are a helpful assistant designed to generate synthetic data."},
    {
   
   "role": "user", "content": question}
  ]
)
res = response.choices[0].message.content
print(res)

id、house_size_m2、house_price、location、number_of_bedrooms
1,50,150000、郊区、2
2,75,250000、市中心、3 3,100,350000
、郊区、4
4,120,450000、郊区、4
5,80,300000、市中心、3
6,90,400000、市中心、3
7,150,600000、优质区域、5
8,200,750000、优质区域、5
9,55,180000、郊区、2
10,300,950000、优质区域、6

2. 使用 Python 程序生成 CSV

直接生成数据的问题是,由于上下文的原因,我们能够生成的数据量有限。相反,我们可以做的是让 LLM 生成一个 Python 程序来生成合成数据。这使我们能够扩展到更多数据,同时还通过检查 Python 程序让我们了解数据是如何生成的。

这将使我们能够根据需要编辑 Python 程序,同时也为我们提供了一个良好的起点。

question = """
Create a Python program to generate 100 rows of housing data.
I want you to at the end of it output a pandas dataframe with 100 rows of data.
Each row should include the following fields:
 - id (incrementing integer starting at 1)
 - house size (m^2)
 - house price
 - location
 - number of bedrooms

Make sure that the numbers make sense (i.e. more rooms is usually bigger size, more expensive locations increase price. more size is usually higher price etc. make sure all the numbers make sense).
"""

response = client.chat.completions.create(
  model=datagen_model,
  messages=[
    {
   
   "role": "system", "content": "You are a helpful assistant designed to generate synthetic data."},
    {
   
   "role": "user", "content": question}
  ]
)
res = response.choices[0].message.content
print(res)
当然可以!下面是一个 Python 程序,它根据您的要求生成合成住房数据。我们将创建一个具有定义字段和特征的 pandas DataFrame。
 
 import pandas as pd
 import random
 
def generate_housing_data(num_rows):
     data = []
     
    locations = [
         ('市中心', 10000, 150), # (位置名称、每平方米基本价格、基本大小)
         ('郊区', 8000, 100),
         ('乡村', 5000, 80),
         ('沿海地区', 12000, 110),
         ('城市社区', 9000, 130)
     ]
     
    for i in range(1, num_rows + 1):
         # 随机选择一个位置
        location, base_price_per_m2, base_size = random.choice(locations)
         
        # 生成卧室数量(1 到 5)
         number_of_bedrooms = random.randint(1, 5)
         
        # 根据卧室数量计算房屋大小
        house_size = base_size + (10 * number_of_bedrooms) + random.randint(-5, 15) # 添加一些噪音
        
        # 根据房屋大小和位置计算房价
        house_price = base_price_per_m2 * house_size + random.randint(-5000, 10000) # 添加一些噪音

        # 将生成的数据附加到列表中
        data.append({
             'id': i,
             'house_size_m2': house_size,
             'house_price': house_price,
             'location': location,
             'number_of_bedrooms': number_of_bedrooms
         })
 
    # 创建一个 pandas DataFrame
     df = pd.DataFrame(data)
     return df
 
#生成 100 行房屋数据
housing_data_df = generate_housing_data(100)
 
#显示结果
print(housing_data_df)

###解释:

  • generate_housing_data 函数为指定行数(num_rows)。
  • 我们定义不同的位置,并对应每平方米的基本价格和平均房屋面积。
  • 对于每栋房子,我们随机选择一个位置、卧室数量,并计算房屋面积和价格,以确保值之间有合理的相关性。
  • 最后,我们从生成的数据中创建一个 pandas DataFrame 并返回它。

您可以在 Python 环境中运行此程序,它将输出一个包含 100 行合成住房数据的 DataFrame。

我们需要确保正确解析此输出,因为 Python 代码周围可能经常有文本。我们还可以明确要求它陈述它对生成的数据做出的所有假设,但在这种情况下,它会自动告诉我们这一点。

3. 使用 Python 程序处理多表 CSV

然而,对于更复杂的关系,我们需要确保指定更多特征。

要创建多个相互关联的不同数据集(例如住房、位置、房屋类型),我们需要像以前一样指定格式、架构和有用信息。但是,现在获得良好性能所需的有用信息更多。这是针对具体情况的,但需要描述的大量内容包括数据集如何相互关联、解决数据集相对于彼此的大小、确保外键和主键正确生成以及理想情况下使用先前生成的数据集来填充新数据集,以便实际数据值在必要时匹配。

question = """
Create a Python program to generate 3 different pandas dataframes.

1. Housing data
I want 100 rows. Each row should include the following fields:
 - id (incrementing integer starting at 1)
 - house size (m^2)
 - house price
 - location
 - number of bedrooms
 - house type
 + any relevant foreign keys

2. Location
Each row should include the following fields:
 - id (incrementing integer starting at 1)
 - country
 - city
 - population
 - area (m^2)
 + any relevant foreign keys

 3. House types
 - id (incrementing integer starting at 1)
 - house type
 - average house type price
 - number of houses
 + any relevant foreign keys

Make sure that the numbers make sense (i.e. more rooms is usually bigger size, more expensive locations increase price. more size is usually higher price etc. make sure all the numbers make sense).
Make sure that the dataframe generally follow common sense checks, e.g. the size of the dataframes make sense in comparison with one another.
Make sure the foreign keys match up and you can use previously generated dataframes when creating each consecutive dataframes.
You can use the previously generated dataframe to generate the next dataframe.
"""

response = client.chat.completions.create(
  model=datagen_model,
  messages=[
    {
   
   "role": "system", "content": "You are a helpful assistant designed to generate synthetic data."},
    {
   
   "role": "user", "content": question}
  ]
)
res = response.choices[0].message.content
print(res)

当然可以!下面是一个 Python 程序,它为住房数据、位置数据和房屋类型生成三个指定的 pandas DataFrame。每个 DataFrame 将包含必要的字段,外键将确保它们之间的正确关系。

 i
### 通用模型蒸馏方法概述 模型蒸馏(Model Distillation)是一种将复杂的大规模模型(教师模型)中的知识转移到较简单的小型模型(学生模型)的技术。这种方法旨在使小型模型能够继承大型模型的强大性能,同时保持较低的计算开销和资源消耗。在具体实现中,教师模型通过预测概率分布或其他中间表示指导学生模型的学习过程。 #### 蒸馏的核心原理 蒸馏技术基于这样一个假设:大型语言模型不仅可以通过硬标签(hard labels)提供监督信号,还可以通过软标签(soft labels),即经过温度缩放后的概率分布[^1],传递更多关于数据分布的信息。这种额外信息有助于弱模型更好地理解输入特征的空间结构,从而提升泛化能力。 #### 利用LLM生成合成样本增强训练数据 为了改进较小模型的表现力,一种有效策略是从现有大规模预训练模型出发,借助它们强大的生成能力创建高质量的人工标注样例集合。这些由高级别AI工具制造出来的虚拟实例被称为“合成样本”。相比于真实世界采集的数据集而言,这类资料具备以下几个优势: - **多样性增加**:由于不受实际场景约束限制,因此可以轻易覆盖到罕见事件或者极端条件下的情况; - **一致性保障**:整个流程完全自动化完成,减少了人为干扰因素带来的偏差风险; - **效率显著提高**:相比传统方式依赖专家手动操作耗时费力的情况来说要快捷得多[^3]。 以下是具体的实施路径之一——采用教师-学生框架结合自动生成机制来进行优化处理的例子说明: ```python from transformers import AutoTokenizer, AutoModelForCausalLM def generate_synthetic_data(teacher_model_name="gpt-4", num_samples=100): tokenizer = AutoTokenizer.from_pretrained(teacher_model_name) model = AutoModelForCausalLM.from_pretrained(teacher_model_name) synthetic_texts = [] prompt_template = "Write a short paragraph about {}." for _ in range(num_samples): topic = f"topic_{_}" # Replace with dynamic topics as needed input_text = prompt_template.format(topic) inputs = tokenizer(input_text, return_tensors="pt").to(model.device) outputs = model.generate(**inputs, max_length=50, do_sample=True) decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True) synthetic_texts.append(decoded_output) return synthetic_texts ``` 上述脚本展示了如何利用高性能的语言生成器快速批量生产定制化的语料素材供后续环节使用。 #### 应用于微调阶段的具体实践 当获得充足的扩充版训练材料之后,则可将其投入到目标系统的再教育进程中去。此部分涉及到了先前提及过的细调概念[^2],即将已经过初步锻炼的基础网络置于新环境之下接受进一步磨练直至达到预期效果为止。 最终成果表现为一个既保留了原始架构紧凑特性又吸收了先进算法精髓的新一代解决方案问世!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术与健康

你的鼓励将是我最大的创作动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值