Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge branch dev to main #17

Merged
merged 18 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 60 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,82 +1,28 @@
# 组件简介
# 合成数据生成器 -- 快速生成高质量合成数据!

高质高效的单表和多表数据合成组件
合成数据生成器(Synthetic Data Generator,SDG)是一个专注于快速生成高质量结构化表格数据的组件。支持10余种单表、多表数据合成算法,实现最高120倍性能提升,支持差分隐私等方法,加强合成数据安全性

## 简介

Synthetic Data Generator(SDG)是一个专注于结构化表格数据快速生成合成数据的组件。它支持先进的CTGAN、TVAE、CTAB-GAN、OCTGAN、CorTGAN等10种模型,并跟踪业界最新工作,将新发表的优质算法/模型纳入支持清单。SDG不仅支持单表和多表数据的快速生成,还从生产环境的角度出发,提供多元化的部署和多机多卡训练等特性,帮助组织和企业用户快速生成高质量的合成数据,以支持各种应用场景和需求。
合成数据是由机器根据真实数据与算法生成的,合成数据不含敏感信息,但能保留真实数据中的行为特征。合成数据与真实数据不存在任何对应关系,不受 GDPR 、ADPPA等隐私法规的约束,在实际应用中不需要担心隐私泄漏风险。高质量的合成数据可用于数据安全开放、模型训练调试、系统开发测试等众多领域。

| 重要链接 | |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- |
| 📖 [文档](https://sgd.github.io/) | 项目API文档 |
| 📖 [文档](https://github.com/hitsz-ids/synthetic-data-generator/tree/main/docs) | 项目API文档 |
| :octocat: [项目仓库](https://github.com/hitsz-ids/synthetic-data-generator) | 项目Github仓库 |
| 📜 [License](https://github.com/hitsz-ids/synthetic-data-generator/blob/main/LICENSE) | Apache-2.0 license |
| 举个例子 🌰 | 在[AI靶场](https://datai.pcl.ac.cn/)上运行SDG示例(TBD) |

## 目录


- [快速开始](#快速开始)
- [主要特性](#主要特性)
- [算法列表](#算法列表)
- [相关论文和数据集链接](#相关论文和数据集链接)
- [安装](#安装)
- [快速开始](#快速开始)
- [API](#API)
- [维护者](#维护者)
- [如何贡献](#如何贡献)
- [许可证](#许可证)

## 主要特性

+ 支持SOTA工作并进行性能优化
+ 已支持单表和多表数据合成的10种先进算法,SDG会持续跟踪学术界和工业界的最新进展,及时引入支持优秀算法和模型;
+ 针对实际生产需求进行优化,提升模型性能,降低内存开销,支持单机多卡、多机多卡等实用特性。
+ 支持生产环境高效使用
+ 提供自动化部署、容器化技术、自动化监控和报警等生产环境所需技术;
+ 针对负载均衡和容错性进行专门优化,提升组件可用性。
+ 支持中文敏感数据自动检测与匿名化
+ 提供中文敏感数据自动识别能力,包括姓名、身份证号、人名等17种常见敏感字段;
+ 实现对敏感字段的匿名化,保证合成数据的安全性。

## 算法列表

### 表1:单表合成算法效果对比(F1-score)

| 模型 | Adult(二分类数据集)(%) | Satellite(多分类数据集)(%) |
| :--------: | :--------------------: | :------------------------: |
| 原始数据集 | 69.5 | 89.23 |
| CTGAN | 60.38 | 69.43 |
| TVAE | 59.52 | 83.58 |
| table-GAN | 63.29 | 79.15 |
| CTAB-GAN | 58.59 | 79.24 |
| OCT-GAN | 55.18 | 80.98 |
| CorTGAN | **67.13** | **84.27** |

### 表2:多表合成算法效果对比

| 模型 | Rossmann(回归数据集)(rmspe) | Telstra(分类数据集)(mlogloss) |
| :--------: | :-------------------------: | :---------------------------: |
| 原始数据集 | 0.2217 | 0.5381 |
| SDV | 0.6897 | 1.1719 |
| CWAMT | **0.4348** | **0.818** |

### 相关论文和数据集链接

#### 论文

- CTGAN:[Modeling Tabular Data using Conditional GAN](https://proceedings.neurips.cc/paper/2019/hash/254ed7d2de3b23ab10936522dd547b78-Abstract.html)
- TVAE:[Modeling Tabular Data using Conditional GAN](https://proceedings.neurips.cc/paper/2019/hash/254ed7d2de3b23ab10936522dd547b78-Abstract.html)
- table-GAN:[Data Synthesis based on Generative Adversarial Networks](https://arxiv.org/pdf/1806.03384.pdf)
- CTAB-GAN:[CTAB-GAN: Effective Table Data Synthesizing](https://proceedings.mlr.press/v157/zhao21a/zhao21a.pdf)
- OCT-GAN: [OCT-GAN: Neural ODE-based Conditional Tabular GANs](https://arxiv.org/pdf/2105.14969.pdf)
- SDV:[The Synthetic data vault](https://sci-hub.se/10.1109/DSAA.2016.49 "多表合成")

#### 数据集

- [Adult数据集](http://archive.ics.uci.edu/ml/datasets/adult)
- [Satellite数据集](http://archive.ics.uci.edu/dataset/146/statlog+landsat+satellite)
- [Rossmann数据集](https://www.kaggle.com/competitions/rossmann-store-sales/data)
- [Telstra数据集](https://www.kaggle.com/competitions/telstra-recruiting-network/data)

## 快速开始

### 从Pypi安装
Expand Down Expand Up @@ -146,6 +92,60 @@ sampled_data = model.generate(1000)
9 28 State-gov 837932 ... 99 United-States <=50K
```

## 主要特性

+ 高性能
+ 支持10余种单表、多表数据合成算法,实现最高120倍性能提升;
+ SDG会持续跟踪学术界和工业界的最新进展,及时引入支持优秀算法和模型。
+ 生产环境快速部署
+ 针对实际生产需求进行优化,提升模型性能,降低内存开销,支持单机多卡、多机多卡等实用特性;
+ 提供自动化部署、容器化技术、自动化监控和报警等生产环境所需技术,支持容器化快速一键部署;
+ 针对负载均衡和容错性进行专门优化,提升组件可用性。
+ 隐私增强:
+ 提供中文敏感数据自动识别能力,包括姓名、身份证号、人名等17种常见敏感字段;
+ 支持差分隐私、匿名化等方法,加强合成数据安全性。

## 算法列表

### 表1:单表合成算法效果对比(F1-score)

| 模型 | Adult(二分类数据集)(%) | Satellite(多分类数据集)(%) |
| :--------: | :--------------------: | :------------------------: |
| 原始数据集 | 69.5 | 89.23 |
| CTGAN | 60.38 | 69.43 |
| TVAE | 59.52 | 83.58 |
| table-GAN | 63.29 | 79.15 |
| CTAB-GAN | 58.59 | 79.24 |
| OCT-GAN | 55.18 | 80.98 |
| CorTGAN | **67.13** | **84.27** |

### 表2:多表合成算法效果对比

| 模型 | Rossmann(回归数据集)(rmspe) | Telstra(分类数据集)(mlogloss) |
| :--------: | :-------------------------: | :---------------------------: |
| 原始数据集 | 0.2217 | 0.5381 |
| SDV | 0.6897 | 1.1719 |
| CWAMT | **0.4348** | **0.818** |

### 相关论文和数据集链接

#### 论文

- CTGAN:[Modeling Tabular Data using Conditional GAN](https://proceedings.neurips.cc/paper/2019/hash/254ed7d2de3b23ab10936522dd547b78-Abstract.html)
- TVAE:[Modeling Tabular Data using Conditional GAN](https://proceedings.neurips.cc/paper/2019/hash/254ed7d2de3b23ab10936522dd547b78-Abstract.html)
- table-GAN:[Data Synthesis based on Generative Adversarial Networks](https://arxiv.org/pdf/1806.03384.pdf)
- CTAB-GAN:[CTAB-GAN: Effective Table Data Synthesizing](https://proceedings.mlr.press/v157/zhao21a/zhao21a.pdf)
- OCT-GAN: [OCT-GAN: Neural ODE-based Conditional Tabular GANs](https://arxiv.org/pdf/2105.14969.pdf)
- SDV:[The Synthetic data vault](https://sci-hub.se/10.1109/DSAA.2016.49 "多表合成")

#### 数据集

- [Adult数据集](http://archive.ics.uci.edu/ml/datasets/adult)
- [Satellite数据集](http://archive.ics.uci.edu/dataset/146/statlog+landsat+satellite)
- [Rossmann数据集](https://www.kaggle.com/competitions/rossmann-store-sales/data)
- [Telstra数据集](https://www.kaggle.com/competitions/telstra-recruiting-network/data)


## API

具体接口参数请参考 [API文档](https://SDG.readthedocs.io/en/latest/api/index.html) 【TBD】。
Expand Down
13 changes: 13 additions & 0 deletions docs/develop/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 开发指南

## 目录说明

本目录中包含针对多种模型/模块的开发文档以及说明:

- 单表模型(基于GAN的):请见 [single_table_GAN.md](single_table_GAN.md);
- 单表模型(基于统计学方法的):TBD
- 多表模型:TBD
- 序列模型:TBD
- 其他:TBD

后续我们将逐渐补全更多模型的开发文档。
152 changes: 152 additions & 0 deletions docs/develop/single_table_GAN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# SDG 模型开发文档

## 为 SDG 开发可运行的单表模型模块

本文档描述了如何开发模型算法模块,使得该模块可以在 sdgx 架构下被调用。

要开发模块,需要执行以下 5 个步骤:

1. 明确需要开发的模块类型;
2. 算法模块需要继承`BaseSynthesizerModel`类,并完成几个指定的函数;
3. 定义模型所需的`Discriminator`类(可选);
4. 定义模型所需的`Generator`类(可选);
5. 本地安装以及测试您的模型。

在以下各节中,我们将通过 `CTGAN` 这一例子详细描述这 5 个步骤。

## 第一步:明确需要开发的模块类型

首先,您需要明确需要开发的模型类型,目前我们支持:

- 单表模型(基于GAN的)
- 单表模型(基于统计学方法的)
- 多表模型
- 序列模型
- 其他

目前,其他模块仍在开发中,我们目前给出了单表模块(基于GAN)的开发文档。

如果您正在开发的基于GAN的单表模块,请继续往下看。


## 第二步:定义您的模型类

大体上讲,定义一个算法模块需要继承`BaseSynthesizerModel`基类,并完成几个指定的函数,即可成为您自己实现的模型模块。

其具体步骤如下:

1. 在 [single_tablem目录](../../sdgx/models/single_table/) 中创建名为 xxx.py 的 Python 脚本文件,其中 xxx 是您打算开发的模块。

2. 继承 `BaseSynthesizerModel`基类 。

- 首先从 `sdgx/models/base.py` 中导入基类,并且导入其他必要的 Python 包,例如:
```python
import warnings
import numpy as np
import pandas as pd
import torch
from torch import optim
from torch.nn import (
BatchNorm1d,
Dropout,
LeakyReLU,
Linear,
Module,
ReLU,
Sequential,
functional,
)
from sdgx.models.base import BaseSynthesizerModel
from sdgx.transform.sampler import DataSamplerCTGAN
from sdgx.transform.transformer import DataTransformerCTGAN
```

- 完成您的模块中的 `__init__` 函数,并定义相应的类变量,以CTGAN为例:

```python
class CTGAN(BaseSynthesizerModel):
def __init__(
self,
embedding_dim=128,
generator_dim=(256, 256),
discriminator_dim=(256, 256),
generator_lr=2e-4,
generator_decay=1e-6
# ...
# 本文档仅为示意,篇幅原因,更多参数已省略
):
assert batch_size % 2 == 0

self._embedding_dim = embedding_dim
self._generator_dim = generator_dim
self._discriminator_dim = discriminator_dim

self._generator_lr = generator_lr
self._generator_decay = generator_decay
self._discriminator_lr = discriminator_lr
self._discriminator_decay = discriminator_decay

# ...
# 本文档仅为示意,篇幅原因,更多参数已省略
```

- 为了顺利使用sdg,您必须完成 `fit` 与 `sample` 这两个方法,它们有关仿真数据模型训练与数据的生成。


## 第三步:定义模型所需的`Discriminator`类(可选)

仅在此模块需要使用生成对抗网络(GAN)技术时候才需要执行此步骤。

您需要在同一个目录中定义`Discriminator`类,并实现 `__init__`、`calc_gradient_penalty``` 以及 `forward`方法。

以CTGAN为例:

```python
class Discriminator(Module):

def __init__(self, input_dim, discriminator_dim, pac=10):
super(Discriminator, self).__init__()

def calc_gradient_penalty(self, real_data, fake_data, device="cpu", pac=10, lambda_=10):
alpha = torch.rand(real_data.size(0) // pac, 1, 1, device=device)
alpha = alpha.repeat(1, pac, real_data.size(1))
# 部分代码被省略

return gradient_penalty

def forward(self, input_):
assert input_.size()[0] % self.pac == 0
return self.seq(input_.view(-1, self.pacdim))
```



## 第四步:定义模型所需的`Generator`类(可选)

仅在此模块需要使用生成对抗网络(GAN)技术时候才需要执行此步骤。

您需要在同一个目录中定义`Generator`类,并实现 `__init__`以及 `forward`方法。

以CTGAN为例:

```python
class Generator(Module):

def __init__(self, embedding_dim, generator_dim, data_dim):
super(Generator, self).__init__()
# 处于文档说明目的,部分代码被省略
self.seq = Sequential(*seq)

def forward(self, input_):
data = self.seq(input_)
return data
```


## 第五步: 本地安装以及测试您的模型

在完成模块的代码编写后,您可以通过 `example/` 目录中的示例代码,将其中模型替换为您开发的模型,初步测试模型是否可用。

单元测试模块将在后续工作中逐步补充。

目前 Log 模块还尚未完成开发,未来,您还可以在以下路径中检查日志:`$PROJECT_DIR/log/{your jobid}`.
7 changes: 4 additions & 3 deletions docs/quick_start.md → docs/readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
sidebar_position: 1
---
# 快速入门

## 快速安装

`pip install sdgx`

## 单表数据快速合成示例

```python
Expand Down
6 changes: 3 additions & 3 deletions example/1_ctgan_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# ipython -i example/1_ctgan_example.py
# 并查看 sampled_data 变量

from sdgx.models.single_table.ctgan import GeneratorCTGAN
from sdgx.models.single_table.ctgan import CTGAN
from sdgx.transform.sampler import DataSamplerCTGAN
from sdgx.transform.transformer import DataTransformerCTGAN
from sdgx.utils.io.csv_utils import *
Expand All @@ -12,8 +12,8 @@
demo_data, discrete_cols = get_demo_single_table()


model = GeneratorCTGAN(epochs=10, transformer=DataTransformerCTGAN, sampler=DataSamplerCTGAN)
model = CTGAN(epochs=10, transformer=DataTransformerCTGAN, sampler=DataSamplerCTGAN)
model.fit(demo_data, discrete_cols)

# sampled
sampled_data = model.generate(1000)
sampled_data = model.sample(1000)
Loading