合成数据:保留关系,用目标留出集检验
Synthetic data generation
区分分布匹配、条件采样与下游用途。
synthetic-data-generation行数更多,需要的关系却可能更少
一张表的标签可能依赖两个二元输入之间的关系。生成器即使复现了每列的直方图,也可能破坏这种关系。再增加几千行,只会把错误训练分布表示得更精确。
合成数据生成是为某个目的构造例子,例如训练、测试、模拟或受控分析。CTGAN处理混合类型表格和类别失衡;Self-Instruct利用语言模型、人工种子任务和筛选构造指令数据。它们展示不同工作流程,都不保证所有生成记录正确或保护隐私。建议先读数据划分与生成模型。
保留联合分布,也保留条件先验
有限表格状态z=(a,b,y)的无平滑计数估计为:
这个最大似然估计通过整行建模依赖,无法给未见行分配正概率。若独立估计每列,则得到:
列边缘完全匹配,条件标签信息却可能消失。条件生成器还涉及另一个区别:训练时使用的条件分布,不一定是生成时需要的分布。若选定目标先验π:
所选CTGAN方法在改变离散条件采样方式的同时训练条件模型,期望的目标混合使用目标类别频率。固定采样器从log(count+1)计算训练条件分布;对于一个二元条件:
相对原始频率,它增加少数类出现机会,但通常不会使类别均匀。本程序把这个先验与原始先验分开保存。已检查的sample_original_condvec辅助函数复用一个已存概率表,仅凭函数名字不能证明整个库恢复了原始频率;本次没有审查完整调用路径。
下游用途应在未参与训练的目标数据上评估。对固定分类器h,目标准确率为:
对某个h有用,不意味着生成器匹配完整分布。对于这个准确率之类固定的有界指示函数,总变差给出:
把h分类正确的状态收成一个事件,任意事件的概率差都不超过TV,就能得到该界。它约束固定分类器,不保证在Q上训练能找到目标分布的最佳分类器。
用生成行训练,在另一批数据上测试
保存为synthetic_data_generation.py,使用Python3.12与NumPy1.26.4。所有数据都是人工构造。100行训练表按(a,b,y)字典序的计数为[36,4,4,36,1,9,9,1]。归一化计数也定义了设定总体:a=0概率.8,b平衡,y为a异或b,再以.1概率独立翻转标签。
程序拟合四种生成器:联合计数、独立列、平衡条件、对数频率条件。每种生成器抽4,096行,训练一个四单元多数分类器。另用独立种子从目标分布抽10,000行作为未触碰测试集;不拿测试标签选择生成器或分类器。因为人工总体已知,还能计算精确总体准确率。
"""Original synthetic table: dependencies, sampling priors and held-out utility."""
import itertools
import json
import numpy as np
STATES = np.array(list(itertools.product([0, 1], repeat=3)))
def table(x):
x = np.asarray(x)
if x.ndim != 2 or x.shape[1] != 3 or not 1 <= len(x) <= 100000 or x.dtype.kind not in 'iu' or not np.isin(x, [0, 1]).all():
raise ValueError('Expected nonempty binary integer rows (a,b,y)')
return x
def prob(p):
p = np.asarray(p, dtype=float)
if p.shape != (8,) or not np.isfinite(p).all() or np.any(p < 0) or not np.isclose(p.sum(), 1, atol=1e-12, rtol=0):
raise ValueError('Expected normalized eight-state distribution')
return p
def fit(x):
x = table(x); return np.bincount(x @ [4, 2, 1], minlength=8)/len(x)
def independent(p):
p = prob(p); marginal = p @ STATES
return np.prod(np.where(STATES, marginal, 1-marginal), axis=1)
def change_prior(p, prior):
p = prob(p).reshape(2, 4); prior = np.asarray(prior, dtype=float)
if prior.shape != (2,) or not np.isfinite(prior).all() or np.any(prior < 0) or not np.isclose(prior.sum(), 1) or np.any(p.sum(axis=1) == 0):
raise ValueError('Both conditions must be represented; specify a valid prior')
return (p/p.sum(axis=1, keepdims=True)*prior[:, None]).ravel()
def sample(p, count, seed):
p = prob(p)
if type(count) is not int or not 1 <= count <= 100000: raise ValueError('Invalid count')
return STATES[np.random.default_rng(seed).choice(8, size=count, p=p)].copy()
def classifier(p):
return prob(p).reshape(4, 2).argmax(axis=1)
def accuracy(pred, test):
pred = np.asarray(pred); test = table(test)
if pred.shape != (4,) or not np.isin(pred, [0, 1]).all(): raise ValueError('Invalid classifier')
return float(np.mean(pred[test[:, :2] @ [2, 1]] == test[:, 2]))
def expected_accuracy(pred, truth):
pred = np.asarray(pred); truth = prob(truth)
if pred.shape != (4,) or not np.isin(pred, [0, 1]).all(): raise ValueError('Invalid classifier')
return float(truth[pred[STATES[:, :2] @ [2, 1]] == STATES[:, 2]].sum())
def demo():
counts = np.array([36, 4, 4, 36, 1, 9, 9, 1]); training = np.repeat(STATES, counts, axis=0)
truth = counts/counts.sum(); learned = fit(training); test = sample(truth, 10000, 29)
log_prior = np.log1p([80, 20]); log_prior /= log_prior.sum()
models = dict(joint=learned, independent=independent(learned),
balanced=change_prior(learned, [.5, .5]), log_prior=change_prior(learned, log_prior))
results = {}
for i, (name, p) in enumerate(models.items()):
synthetic = sample(p, 4096, 40+i); pred = classifier(fit(synthetic))
results[name] = dict(distribution=p.tolist(), marginal=(p @ STATES).tolist(),
total_variation=float(abs(p-truth).sum()/2), classifier=pred.tolist(),
population_accuracy=expected_accuracy(pred, truth), heldout_accuracy=accuracy(pred, test),
exact_training_row_fraction=float(np.mean([np.any(np.all(training == row, axis=1)) for row in synthetic])))
return dict(training_counts=counts.tolist(), training_rows=len(training), synthetic_rows=4096, test_rows=len(test),
models=results, independent_population_classifier=classifier(models['independent']).tolist(),
independent_population_accuracy=expected_accuracy(classifier(models['independent']), truth),
majority_accuracy=accuracy(np.zeros(4, dtype=int), test), log_condition_prior=log_prior.tolist(),
scope='Count-fitted artificial binary table; no CTGAN or language-model training, real-person data or privacy guarantee.')
if __name__ == '__main__': print(json.dumps(demo(), indent=2))
联合、平衡和对数先验样本都恢复异或分类器,总体准确率.9,留出准确率.8993。独立总体没有标签信号,平局选零的分类器准确率为.5。但这次有限独立样本产生[0,1,0,1],目标总体准确率.74,留出准确率.7422。应保留这个结果:采样噪声恰巧对齐了占多数的a=0子群,并没有恢复丢失的异或关系。重复种子能够揭示这种不稳定性。
平衡条件使TV改变.3,拟合分类器却仍有用。每条生成行还都能匹配训练表中的某行,包括正确的联合采样器,因为训练表包含全部八种可能状态。因此,在这个有限支持上,仅看精确匹配率无法诊断记忆。
对固定三列,计数需要O(N),存储八个概率;若有k个二元列,完整表增长为2^k。采样使用有限分类表,在M行生成数据上拟合下游分类器需要O(M)。这不是可扩展的混合类型CTGAN实现。
筛选不会凭空提供真值
Self-Instruct生成指令与实例、筛掉无效或过于相似的输出,再进行微调。人工种子仍是流程的一部分,论文的质量抽检也仍发现错误。固定筛选代码使用ROUGE-L相似度;词面重叠阈值不能证明答案为真。
实际应用应从非生成基线开始,保留真实目标群组或后续时间段,检查子群错误,并在固定预算下比较仅真实数据与加入合成数据的训练。保留来源,避免评测例子进入生成提示或模型选择。隐私需要独立威胁模型与评估;记录由模型生成,不代表它已经匿名化。
审计新增的行保留了什么
- 计算。 本例独立列生成得到什么分布? 答案:[.2,.2,.2,.2,.05,.05,.05,.05]。它匹配a的.8/.2先验和b/y的平衡边缘,却删除异或关系。
- 诊断。 平衡条件采样为什么改变联合分布? 答案:即使各条件分布精确,它也把.8/.2混合权重换成.5/.5。恢复.8/.2即可恢复原混合。
- 解释。 独立样本的.74总体准确率为什么不能证明生成器忠实? 答案:有限样本的多数决策恰好拟合常见子群。精确独立总体的标签并不包含特征信息,少数子群的关系仍然错误。
- 迁移。 100%训练行匹配率足以证明记忆吗? 答案:不足。这里所有八种可能行都在训练中,正确总体采样也必然匹配;新颖性和隐私需要适合任务的审计。
先说明用途,再选生成器
CTGAN关注表格表示与条件训练,Self-Instruct关注指令自举。有限表格让混合与用途问题可以独立核对,不复现这两类模型。继续读生成模型记忆,查看人工候选上的明确审计。
先判断,再看解释
最终采样必须沿用训练条件先验吗?
学习目标
- 拟合并采样有限联合分布。
- 恢复目标条件先验。
- 在未参与训练的留出集评估用途。
先修与延伸
查看知识关联图 →参考文献
- Modeling Tabular Data using Conditional GAN (v2)
Lei Xu; Maria Skoularidou; Alfredo Cuesta-Infante; Kalyan Veeramachaneni · 2019 - Self-Instruct: Aligning Language Models with Self-Generated Instructions (ACL2023)
Yizhong Wang; Yeganeh Kordi; Swaroop Mishra; Alisa Liu; Noah A. Smith; Daniel Khashabi; Hannaneh Hajishirzi · 2023 - sdv-dev/CTGAN: ctgan/data_sampler.py @21175b9
sdv-dev/CTGAN contributors - yizhongw/self-instruct: self_instruct/bootstrap_instructions.py @0b26cca
yizhongw/self-instruct contributors
版本1 · 编写状态: 已完成记录中的技术复核
查看技术检查记录(AI)
Codex · 2026-09-20 · Single AI author checked CTGAN v2 sections4.2-4.3, Self-Instruct selected sections2.1-2.3/opening3.3 and fixed sampler/filter code. Six paired equations and four explained exercises; original100-row binary joint model, conditional prior changes,4096-row generated training sets and independent10000-row target tests. Actual independent-sample accuracy0.74 retained versus0.5 population rule; no CTGAN/LLM training or privacy guarantee.
- 核联合/独立/条件混合、原先验与log先验、目标留出和TV界;保留实际.74,不把有限支持100%行匹配当记忆证据。
- 每题四张计算SVG;15数值/几何与36响应式组合通过,实看12张最终中文手机/英文桌面图。初版文案整理和英文手机标题越界修正保留;完整页面验收另记。
核对的来源位置:
- Modeling Tabular Data using Conditional GAN (v2) · Read sections4.2-4.3; PDF3/4 viewed. v2 uses CTGAN; cached v1 still called the model TGAN. No GAN run or real-data benchmark.
- Self-Instruct: Aligning Language Models with Self-Generated Instructions (ACL2023) · Read selected sections2.1-2.3 and opening3.3; PDF4 viewed. Human seeds, filtering and remaining errors distinguished; no API calls or generated dataset reused.
- sdv-dev/CTGAN: ctgan/data_sampler.py @21175b9 · Read54-139: log(count+1) condition sampling. Original-frequency helper reuses that stored table; full caller behavior not audited. BSL1.1 opening parameters only; no reuse.
- yizhongw/self-instruct: self_instruct/bootstrap_instructions.py @0b26cca · Read190-207: reject ROUGE-L above0.7; exact-equality boundary differs from paper wording. Apache2 heading/opening only; no execution.
相关主题