资讯 文档
技术能力
语音技术
文字识别
人脸与人体
图像技术
语言与知识
视频技术

(New)进阶任务:脏数据识别

任务简介

训练数据标注质量对模型效果有较大影响,但受限于标注人员水平、标注任务难易程度等影响,训练数据中都存在一定比例的标注较差的数据。当标注数据规模较大时,数据标注检查就成为一个难题。这里我们基于可信分析中的实例分析来完成自动化的脏数据识别。

快速开始

代码结构

脏数据识别任务位于/wenxin_appzoo/tasks/text_classification目录下,是分类任务的一个进阶使用,目录结构如下:

.
├── data
│   ├── analysis_data              ## 可信分析(实例分析、特征分析)的demo数据
│   │   ├── example_analysis       ## 实例分析 
│   │   │   └── demo.txt
│   │   └── feature_analysis       ## 特征分析
│   │       └── demo.txt
......
│   ├── dict
│   │   ├── sentencepiece.bpe.model
│   │   └── vocab.txt
......
│   ├── predict_data
│   │   └── infer.txt
│   ├── test_data
│   │   └── test.txt
│   ├── train_data
│   │   └── train.txt
......
├── data_set_reader
│   ......
├── ernie_doc_infer_server.py
├── ernie_run_infer_server.py
├── examples
│   ├── cls_ernie_fc_ch_data_distribution_correct.json   ## 获取数据集样本分布情况任务对应的配置文件
│   ├── cls_ernie_fc_ch_example_analysis.json   ## 实例分析任务的配置文件
│   ├── cls_ernie_fc_ch_feature_analysis.json   ## 特征分析任务的配置文件
│   ├── cls_ernie_fc_ch_find_dirty_data.json    ## 筛选脏数据任务的配置文件
│   ├── cls_ernie_fc_ch_infer.json
│   ├── cls_ernie_fc_ch_infer_with_active_learning.json
│   ├── cls_ernie_fc_ch_infer_with_iflytek.json
│   ├── cls_ernie_fc_ch.json
│   ......
├── find_dirty_data.py              ## 基于实例分析的结果查找脏数据的脚本
├── inference
│   ......
├── model
│   ├── base_cls.py
│   ├── ernie_classification.py
│   ......
├── reader
│   ......
├── run_balance_data.py                  ## 均衡训练集数据分布的脚本
├── run_data_distribution_correct.py     ## 获取数据集样本分布情况的脚本
├── run_example_analysis.py              ## 运行实例分析的启动脚本
├── run_features_analysis.py             ## 运行特征分析的启动脚本
├── ......
├── trainer
│   ......
└── trust_analysis
    ├── gradient_similarity_wenxin.py    ## 基于梯度相似度的实例分析脚本
    ├── integrated_gradients_wenxin.py   ## 基于积分梯度的特征分析脚本
    └── respresenter_point_wenxin.py     ## 基于表示点方法的实例分析脚本

准备工作

在进行脏数据识别之前,首先需要训练出一个可预测的模型,本次文心套件中仅提供在文本分类任务上的脏数据识别功能,所以用户首先需要在分类任务中训练出一个模型,这里以ERNIE 3.0 Base模型举例。

模型准备

预训练模型均存放于wenxin_appzoo/wenxin_appzoo/models_hub文件夹下,进入文件夹下,执行sh download_ernie_3.0_base_ch.sh 即可下载ERNIE 3.0 Base模型的模型参数、字典、网络配置文件。

训练准备

数据准备可以参考:V2.1.0准备工作 训练配置文件如下(examples/cls_ernie_fc_ch.json):

{
  "dataset_reader": {
    "train_reader": {
      "name": "train_reader",  ## 训练、验证、测试各自基于不同的数据集,数据格式也可能不一样,可以在json中配置不同的reader,此处为训练集的reader。
      "type": "BasicDataSetReader",  ## 采用BasicDataSetReader,其封装了常见的读取tsv、txt文件、组batch等操作。
      "fields": [## 域(field)是文心的高阶封装,对于同一个样本存在不同域的时候,不同域有单独的数据类型(文本、数值、整型、浮点型)、单独的词表(vocabulary)等,可以根据不同域进行语义表示,如文本转id等操作,field_reader是实现这些操作的类。
        {
          "name": "text_a",     ## 文本分类任务的第一个特征域,命名为"text_a"。
          "data_type": "string",
          "reader": {
            "type": "ErnieTextFieldReader"
          },
          "tokenizer": {
            "type": "FullTokenizer",   ## 指定text_a分词器,除ernie-tiny模型之外,其余基本上都固定为FullTokenizer分词器。
            "split_char": " ",
            "unk_token": "[UNK]"
          },
          "need_convert": true,
          "vocab_path": "../../models_hub/ernie_3.0_base_ch_dir/vocab.txt",  ## 词表地址
          "max_seq_len": 512,
          "truncation_type": 0,
          "padding_id": 0
        },
        {
          "name": "label",
          "data_type": "int",
          "reader": {
            "type": "ScalarFieldReader"
          },
          "tokenizer": null,
          "need_convert": false,
          "vocab_path": "",
          "max_seq_len": 1,
          "truncation_type": 0,
          "padding_id": 0,
          "embedding": null
        }
      ],
      "config": {
        "data_path": "./data/train_data",    ## 数据路径。
        "shuffle": true,
        "batch_size": 8,
        "epoch": 10,
        "sampling_rate": 1.0,
        "need_data_distribute": true,
        "need_generate_examples": false
      }
    },
    "test_reader": {    ## 此处为测试集的reader。
      "name": "test_reader",
      "type": "BasicDataSetReader",
      "fields": [
        {
          "name": "text_a",
          "data_type": "string",
          "reader": {
            "type": "ErnieTextFieldReader"
          },
          "tokenizer": {
            "type": "FullTokenizer",
            "split_char": " ",
            "unk_token": "[UNK]"
          },
          "need_convert": true,
          "vocab_path": "../../models_hub/ernie_3.0_base_ch_dir/vocab.txt",
          "max_seq_len": 512,
          "truncation_type": 0,
          "padding_id": 0
        },
        {
          "name": "label",
          "data_type": "int",
          "need_convert": false,
          "reader": {
            "type": "ScalarFieldReader"
          },
          "tokenizer": null,
          "vocab_path": "",
          "max_seq_len": 1,
          "truncation_type": 0,
          "padding_id": 0,
          "embedding": null
        }
      ],
      "config": {
        "data_path": "./data/test_data",
        "shuffle": false,
        "batch_size": 8,
        "epoch": 1,
        "sampling_rate": 1.0,
        "need_data_distribute": false,
        "need_generate_examples": false
      }
    }
  },
  "model": {
    "type": "ErnieClassification",
    "is_dygraph": 1,
    "optimization": {   ## 优化器设置,文心ERNIE推荐的默认设置。
      "learning_rate": 2e-05,
      "use_lr_decay": true,
      "warmup_steps": 0,
      "warmup_proportion": 0.1,
      "weight_decay": 0.01,
      "use_dynamic_loss_scaling": false,
      "init_loss_scaling": 128,
      "incr_every_n_steps": 100,
      "decr_every_n_nan_or_inf": 2,
      "incr_ratio": 2.0,
      "decr_ratio": 0.8
    },
    "embedding": {
      "config_path": "../../models_hub/ernie_3.0_base_ch_dir/ernie_config.json"
    },
    "num_labels": 2
  },
  "trainer": {
    "type": "CustomDynamicTrainer",
    "PADDLE_PLACE_TYPE": "gpu",
    "PADDLE_IS_FLEET": 0,    ## 是否启用fleetrun运行,多卡运行时必须使用fleetrun,单卡时即可以使用fleetrun启动也可以直接python启动
    "train_log_step": 10,
    "use_amp": true,
    "is_eval_dev": 0,
    "is_eval_test": 1,
    "eval_step": 100,
    "save_model_step": 200,
    "load_parameters": "",
    "load_checkpoint": "",
    "pre_train_model": [
      {
        "name": "ernie_3.0_base_ch",
        "params_path": "../../models_hub/ernie_3.0_base_ch_dir/params"
      }
    ],
    "output_path": "./output/cls_ernie_3.0_base_fc_ch_dy",
    "extra_param": {
      "meta":{
        "job_type": "text_classification"
      }
    }
  }
}

开始训练

# 进入指定任务的目录
cd wenxin_appzoo/wenxin_appzoo/tasks/text_classification
# 单卡训练,如果fleetrun设置为0,则使用下面命令
python ./run_trainer.py --param_path "./examples/cls_ernie_fc_ch.json"
# 单卡训练,如果fleetrun设置为1,则使用下面的命令
fleetrun --log_dir log ./run_trainer.py --param_path "./examples/cls_ernie_fc_ch.json" 1>log/lanch.log 2>&1

# 多卡训练,fleetrun必须设置为1,使用下面的命令
fleetrun --log_dir log ./run_trainer.py --param_path "./examples/cls_ernie_fc_ch.json" 1>log/lanch.log 2>&1
  • 通过上述脚本调用json文件开启训练
  • 训练阶段日志文件于log文件夹下,workerlog.N 保存了第N张卡的log日志内容,如遇到程序报错可以通过查看不同卡的workerlog.N定位到有效的报错信息。
  • 训练模型保存于./output/cls_ernie_3.0_base_fc_ch_dy文件夹下,保存好的模型,我们选择checkpoints文件进行下一步的操作。

识别脏数据

脏数据识别是对训练集数据进行识别,基于可信分析工具中的实例分析(进阶任务:模型可解释性-实例证据分析)任务来完成。文心套件中,我们使用实例分析方法中的表示点方法来进行脏数据识别。 数据准备:准备好分类任务用到的训练集和测试集两个数据集合,示例如下:

选择珠江花园的原因就是方便,有电动扶梯直接到达海边,周围餐馆、食廊、商场、超市、摊位一应俱全。酒店装修一般,但还算整洁。         1 
15.4寸笔记本的键盘确实爽,基本跟台式机差不多了,蛮喜欢数字小键盘,输数字特方便,样子也很美观,做工也相当不错         1 
房间太小。其他的都一般。。。。。。。。。         0

运行脚本配置如下(examples/cls_ernie_fc_ch_find_dirty_data.json)

{
  "dataset_reader": {
    "train_reader": {   ## 训练集reader配置
      "name": "train_reader",
      "type": "BasicDataSetReader",
      "fields": [
        {
          "name": "text_a",
          "data_type": "string",
          "reader": {
            "type": "ErnieTextFieldReader"
          },
          "tokenizer": {
            "type": "FullTokenizer",
            "split_char": " ",
            "unk_token": "[UNK]"
          },
          "need_convert": true,
          "vocab_path": "../../models_hub/ernie_3.0_base_ch_dir/vocab.txt",
          "max_seq_len": 512,
          "truncation_type": 0,
          "padding_id": 0
        },
        {
          "name": "label",
          "data_type": "int",
          "reader": {
            "type": "ScalarFieldReader"
          },
          "tokenizer": null,
          "need_convert": false,
          "vocab_path": "",
          "max_seq_len": 1,
          "truncation_type": 0,
          "padding_id": 0,
          "embedding": null
        }
      ],
      "config": {
        "data_path": "./data/train_data",
        "shuffle": false,
        "batch_size": 1,   # batch-size必须是1
        "epoch": 1,
        "sampling_rate": 1.0,
        "need_data_distribute": true,
        "need_generate_examples": false  
      }
    },
    "test_reader": {     # 测试集reader配置
      "name": "test_reader",
      "type": "BasicDataSetReader",
      "fields": [
        {
          "name": "text_a",
          "data_type": "string",
          "reader": {
            "type": "ErnieTextFieldReader"
          },
          "tokenizer": {
            "type": "FullTokenizer",
            "split_char": " ",
            "unk_token": "[UNK]"
          },
          "need_convert": true,
          "vocab_path": "../../models_hub/ernie_3.0_base_ch_dir/vocab.txt",
          "max_seq_len": 512,
          "truncation_type": 0,
          "padding_id": 0
        },
        {
          "name": "label",
          "data_type": "int",
          "need_convert": false,
          "reader": {
            "type": "ScalarFieldReader"
          },
          "tokenizer": null,
          "vocab_path": "",
          "max_seq_len": 1,
          "truncation_type": 0,
          "padding_id": 0,
          "embedding": null
        }
      ],
      "config": {
        "data_path": "./data/analysis_data/example_analysis",
        "shuffle": false,
        "batch_size": 1,
        "epoch": 1,
        "sampling_rate": 1.0,
        "need_data_distribute": false,
        "need_generate_examples": true
      }
    }
  },
  "model": {
    "type": "ErnieClassification",
    "is_dygraph": 1,
    "optimization": {   ## 网络优化器配置,和训练保持一致即可
      "learning_rate": 2e-05,
      "use_lr_decay": false,   ## 这里必须是false
      "warmup_steps": 0,
      "warmup_proportion": 0.1,
      "weight_decay": 0.01,
      "use_dynamic_loss_scaling": false,
      "init_loss_scaling": 128,
      "incr_every_n_steps": 100,
      "decr_every_n_nan_or_inf": 2,
      "incr_ratio": 2.0,
      "decr_ratio": 0.8
    },
    "embedding": {
      "config_path": "../../models_hub/ernie_3.0_base_ch_dir/ernie_config.json"
    },
    "num_labels": 2
  },
  "trainer": {
    "type": "CustomDynamicTrainer",
    "PADDLE_PLACE_TYPE": "gpu",
    "PADDLE_IS_FLEET": 0,
    "train_log_step": 10,
    "use_amp": true,
    "is_eval_dev": 0,
    "is_eval_test": 1,
    "eval_step": 100,
    "save_model_step": 200,
    "load_parameters": "./output/cls_ernie_3.0_base_fc_ch_dy/save_checkpoints/checkpoints_step_501/",  ## 上一步训练出来的模型checkpoints文件
    "load_checkpoint": "",
    "pre_train_model": [],
    "dirty_num": 50,
    "dirty_threshold": 0.0,
    "dirty_data_path": "./output/dirty_data.txt",   # 从训练集数据中识别出来的脏数据
    "rest_data_path": "./output/rest_data.txt",   # 训练集中筛掉脏数据之后保存下来的其余干净数据
    "extra_param": {
      "meta":{
        "job_type": "text_classification"
      }

    }
  }
}

开始运行:

# 使用当前目录下的find_dirty_data.py 脚本进行脏数据识别
python find_dirty_data.py --param_path=./examples/cls_ernie_fc_ch_find_dirty_data.json
# 运行完成后,识别出来的脏数据保存在output/dirty_data.txt中,训练集中筛掉脏数据之后保存下来的其余干净数据保存在output/rest_data.txt中。
上一篇
(New)进阶任务:模型可解释性-实例证据分析
下一篇
(New)进阶任务:训练数据分布修正