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

Layer decay策略

简介

  • Layer decay策略是指分别为模型的每层参数设置一个权重衰减系数,并使用该系数与学习率的乘积作为该层新的学习率,同时该权重衰减系数是根据模型的层数以指数衰减的形式进行计算,具体计算公式如下:
new_lr=lr*decay_rate ** (n_layers + 2 - depth)
  • lr为模型的学习率
  • decay_rate为衰减指数
  • n_layers为模型总层数
  • depth为当前参数所在模型的层数
  • new_lr为当前参数的学习率
  • 目前适配的任务有:

    • 文本分类
    • 文本匹配
  • ⚠️注意:使用Layer decay策略策略时,设置的学习率需要比正常学习率要大,例如不加Layer decay策略训练时学习率为5e-5,那么加上该策略学习率需要设置为1e-4。

文本分类

  • 任务代码位于./wenxin_appzoo/tasks/text_classification目录
cd ./wenxin_appzoo/tasks/text_classification

数据准备

  • 示例数据使用clue-iflytek数据集,使用下面命令下载全量数据集
cd data
sh download_iflytek_data.sh
  • 注:如果只是简单测试,则./data/iflytek目录下自带demo数据能够使用,可跳过本步骤,无需下载全量数据集
  • 数据格式与[文本分类->数据准备]中的数据示例相同

模型准备

  • 示例model使用ERNIE3.0 base
cd ../models_hub
sh download_ernie_3.0_base_ch.sh

开始训练

配置文件

  • 文心中的各种参数都是在json文件中进行配置的,你可以通过修改所加载的json文件来进行参数的自定义配置。json配置文件主要分为三个部分:dataset_reader(数据部分)、model(网络部分)、trainer(训练任务)或inference(预测部分),在模型训练的时候,json文件中需要配置dataset_reader、model和trainer这三个部分;在预测推理的时候,json文件中需要配置dataset_reader、inference这两个部分。
  • 对抗训练的配置文件为./wenxin_appzoo/tasks/text_classification/examples/cls_ernie_fc_ch_with_layer_decay.json,json配置大体与正常的文本分类json配置类似,只针对model部分进行了修改,以下介绍与layer decay相关的json配置。
{
    "dataset_reader": {
      "train_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": 128,
            "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/iflytek/train_data",
          "shuffle": true,
          "batch_size": 16,
          "epoch": 5,
          "sampling_rate": 1.0,
          "need_data_distribute": true,
          "need_generate_examples": false
        }
      },
      "test_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": 128,
            "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/iflytek/test_data",
          "shuffle": false,
          "batch_size": 8,
          "epoch": 1,
          "sampling_rate": 1.0,
          "need_data_distribute": false,
          "need_generate_examples": false
        }
      }
    },
    "model": {
      "type": "ErnieClassification",
      "use_rdrop": false,
      "rdrop_alpha": 1,
      "is_dygraph": 1,
      "use_adv": false,
      "use_alpha": true,
      "adv_alpha": 0.5, 
      "attack_after_drop": true,
      "adv_epsilon": 0.001,
      "small_constant_for_finite_diff": 1e-05,
      "optimization": {
        "learning_rate": 6e-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,
        "use_layer_decay":true,        #开启layer decay策略
        "layer_decay_ratio":0.7        #学习率衰减系数
      },
      "embedding": {
        "config_path": "../../models_hub/ernie_3.0_base_ch_dir/ernie_config.json"
      },
      "num_labels": 119
    },
    "trainer": {
      "type": "CustomDynamicTrainer",
      "PADDLE_PLACE_TYPE": "gpu",
      "PADDLE_IS_FLEET": 0,
      "use_fma":false,
      "fma_use_best_n":true,
      "fma_score":"acc",
      "fma_num_model":8,
      "fma_dev_or_test":"test",
      "train_log_step": 10,
      "use_amp": true,
      "is_eval_dev": 0,
      "is_eval_test": 1,
      "eval_step": 100,
      "save_model_step": 200000,
      "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_layer_decay",
      "extra_param": {
        "meta":{
          "job_type": "text_classification"
        }

      }
    }
  }

启动训练

python run_trainer.py --param_path ./examples/
cls_ernie_fc_ch_with_layer_decay.json
  • 训练运行的日志会自动保存在./log/test.log文件中。
  • 训练中以及结束后产生的模型文件会保存在json配置文件中的output_path字段值的目录下(此处为./output/cls_ernie_3.0_base_fc_ch_dy_layer_decay),其中save_inference_model文件夹会保存用于预测的模型文件,save_checkpoint文件夹会保存用于热启动的模型文件。

开始预测

配置文件

  • 使用./wenxin_appzoo/tasks/text_classification/examples/cls_ernie_fc_ch_infer_with_iflytek.json进行推理配置
  • 注意需要修改cls_ernie_fc_ch_infer_with_iflytek.json里的"inference_model_path"字段,填入上面训练过程中保存的save_inference_model文件夹下的model路径,配置如下
{
  "dataset_reader": {
    "predict_reader": {
      "name": "predict_reader",
      "type": "BasicDataSetReader",
      "fields": [
        {
          "name": "text_a",
          "data_type": "string",
          "reader": {
            "type": "ErnieTextFieldReader"
          },
          "tokenizer": {
            "type": "FullTokenizer",
            "split_char": " ",
            "unk_token": "[UNK]",
            "params": null
          },
          "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,
          "embedding": null
        }
      ],
      "config": {
        "data_path": "./data/iflytek/predict_data",
        "shuffle": false,
        "batch_size": 8,
        "epoch": 1,
        "sampling_rate": 1.0,
        "need_data_distribute": false,
        "need_generate_examples": true
      }
    }
  },

  "inference": {
    "type": "CustomInference",
    "output_path": "./output/predict_result.txt",
    "PADDLE_PLACE_TYPE": "cpu",
    "num_labels": 119,
    "thread_num": 2,
    "inference_model_path": "./output/cls_ernie_3.0_base_fc_ch_dy/save_inference_model/inference_step_126/", ###此处修改
    "extra_param": {
      "meta":{
        "job_type": "text_classification"
      }

    }
  }
}

启动预测

python run_infer.py --param_path ./examples/cls_ernie_fc_ch_infer_with_iflytek.json
  • 预测结果保存于./output/predict_result.txt文件中。

文本匹配

  • 任务代码位于./wenxin_appzoo/tasks/text_matching
cd ./wenxin_appzoo/tasks/text_matching

数据准备

  • 示例数据使用clue-afqmc数据集,使用下面命令下载全量数据集
cd data
sh download_afqmc_data.sh
  • 注:如果只是简单测试,则./data/afqmc目录下自带demo数据能够使用,可跳过本步骤,无需下载全量数据集
  • 数据格式与[文本匹配->数据准备]中的数据示例相同

模型准备

  • 示例model使用ERNIE3.0 base
cd ../models_hub
sh download_ernie_3.0_base_ch.sh

开始训练

配置文件

  • 文心中的各种参数都是在json文件中进行配置的,你可以通过修改所加载的json文件来进行参数的自定义配置。json配置文件主要分为三个部分:dataset_reader(数据部分)、model(网络部分)、trainer(训练任务)或inference(预测部分),在模型训练的时候,json文件中需要配置dataset_reader、model和trainer这三个部分;在预测推理的时候,json文件中需要配置dataset_reader、inference这两个部分。
  • 对抗训练json配置大体与正常的文本匹配json配置类似,以下介绍与对抗训练相关的json配置。
  • 文本匹配分为三类(详细信息见文本匹配任务),不同类别的文本匹配对应着不同类别的json配置文件:

    • 单塔pointwise:./examples/mtch_ernie_fc_pointwise_ch_with_layer_decay.json
    • 双塔pointwise:./examples/mtch_ernie_pointwise_simnet_ch_with_layer_decay.json
    • 双塔pairwise:./examples/mtch_ernie_pairwise_simnet_ch_with_layer_decay.json
  • 单塔pointwise:对应json为./examples/mtch_ernie_fc_pointwise_ch_with_layer_decay.json,相比于./examples/mtch_ernie_fc_pointwise_ch_with_layer_decay.json重点修改了model部分
{
  "dataset_reader": {
    "train_reader": {.....},
    "test_reader": {...},
    "dev_reader": {......}
  },
  "model": {
    "type": "ErnieMatchingFcPointwise",
    "use_rdrop": false,
    "rdrop_alpha": 1,
    "is_dygraph": 1,
    "use_adv": false,
    "use_alpha": true,
    "adv_alpha": 0.5, 
    "attack_after_drop": true,
    "adv_epsilon": 0.001,
    "small_constant_for_finite_diff": 1e-05,
    "optimization": {
      "learning_rate": 3e-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,
      "use_layer_decay":true,   #开启layer decay策略
      "layer_decay_ratio":0.8    #学习率衰减系数
    },
    "embedding": {
      "config_path": "../../models_hub/ernie_3.0_base_ch_dir/ernie_config.json"
    }
  },
  "trainer": {
    ....
    "output_path": "./output/mtch_ernie_3.0_base_fc_pointwise_ch_layer_decay",
    ....
  }
}

启动训练

python run_trainer.py --param_path ./examples/mtch_ernie_fc_pointwise_ch_with_layer_decay.json

开始预测

预测方式与文本匹配的预测方式相同,仅需配置对应的inference_model_path加载推理模型路径和对应的data_path加载预测数据。

  • 修改配置文件:修改cls_ernie_fc_ch_infer_with_iflytek.json 的推理模型路径
{
  "dataset_reader": {
    "predict_reader": {
      "name": "predict_reader",
      "type": "BasicDataSetReader",
      "fields": [
        {
          "name": "text_a",
          "data_type": "string",
          "reader": {
            "type": "ErnieTextFieldReader"
          },
          "tokenizer": {
            "type": "FullTokenizer",
            "split_char": " ",
            "unk_token": "[UNK]",
            "params": null
          },
          "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,
          "embedding": null
        }
      ],
      "config": {
        "data_path": "./data/iflytek/predict_data",
        "shuffle": false,
        "batch_size": 8,
        "epoch": 1,
        "sampling_rate": 1.0,
        "need_data_distribute": false,
        "need_generate_examples": true
      }
    }
  },

  "inference": {
    "type": "CustomInference",
    "output_path": "./output/predict_result.txt", #预测文件路径
    "PADDLE_PLACE_TYPE": "cpu",
    "num_labels": 119,   #预测label数目
    "thread_num": 2,
    "inference_model_path": "./output/cls_ernie_3.0_base_fc_ch_dy/save_inference_model/inference_step_126/", ##修改此处路径为对应的模型路径
    "extra_param": {
      "meta":{
        "job_type": "text_classification"
      }

    }
  }
}
  • 启动预测
python run_infer.py --param_path examples/cls_ernie_fc_ch_infer_with_iflytek.json 
上一篇
Rdop策略
下一篇
Fma策略