You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 9.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # BERT Example
  2. ## Description
  3. This example implements pre-training, fine-tuning and evaluation of [BERT-base](https://github.com/google-research/bert)(the base version of BERT model) and [BERT-NEZHA](https://github.com/huawei-noah/Pretrained-Language-Model)(a Chinese pretrained language model developed by Huawei, which introduced a improvement of Functional Relative Positional Encoding as an effective positional encoding scheme).
  4. ## Requirements
  5. - Install [MindSpore](https://www.mindspore.cn/install/en).
  6. - Download the zhwiki dataset for pre-training. Extract and clean text in the dataset with [WikiExtractor](https://github.com/attardi/wikiextractor). Convert the dataset to TFRecord format and move the files to a specified path.
  7. - Download the CLUE dataset for fine-tuning and evaluation.
  8. > Notes:
  9. If you are running a fine-tuning or evaluation task, prepare the corresponding checkpoint file.
  10. ## Running the Example
  11. ### Pre-Training
  12. - Set options in `config.py`, including lossscale, optimizer and network. Click [here](https://www.mindspore.cn/tutorial/zh-CN/master/use/data_preparation/loading_the_datasets.html#tfrecord) for more information about dataset and the json schema file.
  13. - Run `run_standalone_pretrain.sh` for non-distributed pre-training of BERT-base and BERT-NEZHA model.
  14. ``` bash
  15. sh run_standalone_pretrain.sh DEVICE_ID EPOCH_SIZE DATA_DIR SCHEMA_DIR
  16. ```
  17. - Run `run_distribute_pretrain.sh` for distributed pre-training of BERT-base and BERT-NEZHA model.
  18. ``` bash
  19. sh run_distribute_pretrain.sh DEVICE_NUM EPOCH_SIZE DATA_DIR SCHEMA_DIR MINDSPORE_HCCL_CONFIG_PATH
  20. ```
  21. ### Fine-Tuning
  22. - Set options in `finetune_config.py`. Make sure the 'data_file', 'schema_file' and 'pre_training_file' are set to your own path. Set the 'pre_training_ckpt' to a saved checkpoint file generated after pre-training.
  23. - Run `finetune.py` for fine-tuning of BERT-base and BERT-NEZHA model.
  24. ```bash
  25. python finetune.py
  26. ```
  27. ### Evaluation
  28. - Set options in `evaluation_config.py`. Make sure the 'data_file', 'schema_file' and 'finetune_ckpt' are set to your own path.
  29. - Run `evaluation.py` for evaluation of BERT-base and BERT-NEZHA model.
  30. ```bash
  31. python evaluation.py
  32. ```
  33. ## Usage
  34. ### Pre-Training
  35. ```
  36. usage: run_pretrain.py [--distribute DISTRIBUTE] [--epoch_size N] [----device_num N] [--device_id N]
  37. [--enable_task_sink ENABLE_TASK_SINK] [--enable_loop_sink ENABLE_LOOP_SINK]
  38. [--enable_mem_reuse ENABLE_MEM_REUSE] [--enable_save_ckpt ENABLE_SAVE_CKPT]
  39. [--enable_lossscale ENABLE_LOSSSCALE] [--do_shuffle DO_SHUFFLE]
  40. [--enable_data_sink ENABLE_DATA_SINK] [--data_sink_steps N] [--checkpoint_path CHECKPOINT_PATH]
  41. [--save_checkpoint_steps N] [--save_checkpoint_num N]
  42. [--data_dir DATA_DIR] [--schema_dir SCHEMA_DIR]
  43. options:
  44. --distribute pre_training by serveral devices: "true"(training by more than 1 device) | "false", default is "false"
  45. --epoch_size epoch size: N, default is 1
  46. --device_num number of used devices: N, default is 1
  47. --device_id device id: N, default is 0
  48. --enable_task_sink enable task sink: "true" | "false", default is "true"
  49. --enable_loop_sink enable loop sink: "true" | "false", default is "true"
  50. --enable_mem_reuse enable memory reuse: "true" | "false", default is "true"
  51. --enable_save_ckpt enable save checkpoint: "true" | "false", default is "true"
  52. --enable_lossscale enable lossscale: "true" | "false", default is "true"
  53. --do_shuffle enable shuffle: "true" | "false", default is "true"
  54. --enable_data_sink enable data sink: "true" | "false", default is "true"
  55. --data_sink_steps set data sink steps: N, default is 1
  56. --checkpoint_path path to save checkpoint files: PATH, default is ""
  57. --save_checkpoint_steps steps for saving checkpoint files: N, default is 1000
  58. --save_checkpoint_num number for saving checkpoint files: N, default is 1
  59. --data_dir path to dataset directory: PATH, default is ""
  60. --schema_dir path to schema.json file, PATH, default is ""
  61. ```
  62. ## Options and Parameters
  63. It contains of parameters of BERT model and options for training, which is set in file `config.py`, `finetune_config.py` and `evaluation_config.py` respectively.
  64. ### Options:
  65. ```
  66. config.py:
  67. bert_network version of BERT model: base | nezha, default is base
  68. loss_scale_value initial value of loss scale: N, default is 2^32
  69. scale_factor factor used to update loss scale: N, default is 2
  70. scale_window steps for once updatation of loss scale: N, default is 1000
  71. optimizer optimizer used in the network: AdamWerigtDecayDynamicLR | Lamb | Momentum, default is "Lamb"
  72. finetune_config.py:
  73. task task type: NER | XNLI | LCQMC | SENTIi | OTHERS
  74. num_labels number of labels to do classification
  75. data_file dataset file to load: PATH, default is "/your/path/train.tfrecord"
  76. schema_file dataset schema file to load: PATH, default is "/your/path/schema.json"
  77. epoch_num repeat counts of training: N, default is 5
  78. ckpt_prefix prefix used to save checkpoint files: PREFIX, default is "bert"
  79. ckpt_dir path to save checkpoint files: PATH, default is None
  80. pre_training_ckpt checkpoint file to load: PATH, default is "/your/path/pre_training.ckpt"
  81. use_crf whether to use crf for evaluation. use_crf takes effect only when task type is NER, default is False
  82. optimizer optimizer used in fine-tune network: AdamWeigtDecayDynamicLR | Lamb | Momentum, default is "Lamb"
  83. evaluation_config.py:
  84. task task type: NER | XNLI | LCQMC | SENTI | OTHERS
  85. num_labels number of labels to do classsification
  86. data_file dataset file to load: PATH, default is "/your/path/evaluation.tfrecord"
  87. schema_file dataset schema file to load: PATH, default is "/your/path/schema.json"
  88. finetune_ckpt checkpoint file to load: PATH, default is "/your/path/your.ckpt"
  89. use_crf whether to use crf for evaluation. use_crf takes effect only when task type is NER, default is False
  90. clue_benchmark whether to use clue benchmark. clue_benchmark takes effect only when task type is NER, default is False
  91. ```
  92. ### Parameters:
  93. ```
  94. Parameters for dataset and network (Pre-Training/Fine-Tuning/Evaluation):
  95. batch_size batch size of input dataset: N, default is 16
  96. seq_length length of input sequence: N, default is 128
  97. vocab_size size of each embedding vector: N, default is 21136
  98. hidden_size size of bert encoder layers: N, default is 768
  99. num_hidden_layers number of hidden layers: N, default is 12
  100. num_attention_heads number of attention heads: N, default is 12
  101. intermediate_size size of intermediate layer: N, default is 3072
  102. hidden_act activation function used: ACTIVATION, default is "gelu"
  103. hidden_dropout_prob dropout probability for BertOutput: Q, default is 0.1
  104. attention_probs_dropout_prob dropout probability for BertAttention: Q, default is 0.1
  105. max_position_embeddings maximum length of sequences: N, default is 512
  106. type_vocab_size size of token type vocab: N, default is 16
  107. initializer_range initialization value of TruncatedNormal: Q, default is 0.02
  108. use_relative_positions use relative positions or not: True | False, default is False
  109. input_mask_from_dataset use the input mask loaded form dataset or not: True | False, default is True
  110. token_type_ids_from_dataset use the token type ids loaded from dataset or not: True | False, default is True
  111. dtype data type of input: mstype.float16 | mstype.float32, default is mstype.float32
  112. compute_type compute type in BertTransformer: mstype.float16 | mstype.float32, default is mstype.float16
  113. Parameters for optimizer:
  114. AdamWeightDecayDynamicLR:
  115. decay_steps steps of the learning rate decay: N
  116. learning_rate value of learning rate: Q
  117. end_learning_rate value of end learning rate: Q, must be positive
  118. power power: Q
  119. warmup_steps steps of the learning rate warm up: N
  120. weight_decay weight decay: Q
  121. eps term added to the denominator to improve numerical stability: Q
  122. Lamb:
  123. decay_steps steps of the learning rate decay: N
  124. learning_rate value of learning rate: Q
  125. end_learning_rate value of end learning rate: Q
  126. power power: Q
  127. warmup_steps steps of the learning rate warm up: N
  128. weight_decay weight decay: Q
  129. Momentum:
  130. learning_rate value of learning rate: Q
  131. momentum momentum for the moving average: Q
  132. ```