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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/SQuAD v1.1 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 scripts/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 scripts/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. - NER: Run `evaluation.py` for evaluation of BERT-base and BERT-NEZHA model.
  30. ```bash
  31. python evaluation.py
  32. ```
  33. - SQuAD v1.1: Run `squadeval.py` and `SQuAD_postprocess.py` for evaluation of BERT-base and BERT-NEZHA model.
  34. ```bash
  35. python squadeval.py
  36. ```
  37. ```bash
  38. python SQuAD_postprocess.py
  39. ```
  40. ## Usage
  41. ### Pre-Training
  42. ```
  43. usage: run_pretrain.py [--distribute DISTRIBUTE] [--epoch_size N] [----device_num N] [--device_id N]
  44. [--enable_save_ckpt ENABLE_SAVE_CKPT]
  45. [--enable_lossscale ENABLE_LOSSSCALE] [--do_shuffle DO_SHUFFLE]
  46. [--enable_data_sink ENABLE_DATA_SINK] [--data_sink_steps N] [--checkpoint_path CHECKPOINT_PATH]
  47. [--save_checkpoint_steps N] [--save_checkpoint_num N]
  48. [--data_dir DATA_DIR] [--schema_dir SCHEMA_DIR]
  49. options:
  50. --distribute pre_training by serveral devices: "true"(training by more than 1 device) | "false", default is "false"
  51. --epoch_size epoch size: N, default is 1
  52. --device_num number of used devices: N, default is 1
  53. --device_id device id: N, default is 0
  54. --enable_save_ckpt enable save checkpoint: "true" | "false", default is "true"
  55. --enable_lossscale enable lossscale: "true" | "false", default is "true"
  56. --do_shuffle enable shuffle: "true" | "false", default is "true"
  57. --enable_data_sink enable data sink: "true" | "false", default is "true"
  58. --data_sink_steps set data sink steps: N, default is 1
  59. --checkpoint_path path to save checkpoint files: PATH, default is ""
  60. --save_checkpoint_steps steps for saving checkpoint files: N, default is 1000
  61. --save_checkpoint_num number for saving checkpoint files: N, default is 1
  62. --data_dir path to dataset directory: PATH, default is ""
  63. --schema_dir path to schema.json file, PATH, default is ""
  64. ```
  65. ## Options and Parameters
  66. 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.
  67. ### Options:
  68. ```
  69. config.py:
  70. bert_network version of BERT model: base | nezha, default is base
  71. loss_scale_value initial value of loss scale: N, default is 2^32
  72. scale_factor factor used to update loss scale: N, default is 2
  73. scale_window steps for once updatation of loss scale: N, default is 1000
  74. optimizer optimizer used in the network: AdamWerigtDecayDynamicLR | Lamb | Momentum, default is "Lamb"
  75. finetune_config.py:
  76. task task type: NER | SQUAD | OTHERS
  77. num_labels number of labels to do classification
  78. data_file dataset file to load: PATH, default is "/your/path/train.tfrecord"
  79. schema_file dataset schema file to load: PATH, default is "/your/path/schema.json"
  80. epoch_num repeat counts of training: N, default is 5
  81. ckpt_prefix prefix used to save checkpoint files: PREFIX, default is "bert"
  82. ckpt_dir path to save checkpoint files: PATH, default is None
  83. pre_training_ckpt checkpoint file to load: PATH, default is "/your/path/pre_training.ckpt"
  84. use_crf whether to use crf for evaluation. use_crf takes effect only when task type is NER, default is False
  85. optimizer optimizer used in fine-tune network: AdamWeigtDecayDynamicLR | Lamb | Momentum, default is "Lamb"
  86. evaluation_config.py:
  87. task task type: NER | SQUAD | OTHERS
  88. num_labels number of labels to do classsification
  89. data_file dataset file to load: PATH, default is "/your/path/evaluation.tfrecord"
  90. schema_file dataset schema file to load: PATH, default is "/your/path/schema.json"
  91. finetune_ckpt checkpoint file to load: PATH, default is "/your/path/your.ckpt"
  92. use_crf whether to use crf for evaluation. use_crf takes effect only when task type is NER, default is False
  93. clue_benchmark whether to use clue benchmark. clue_benchmark takes effect only when task type is NER, default is False
  94. ```
  95. ### Parameters:
  96. ```
  97. Parameters for dataset and network (Pre-Training/Fine-Tuning/Evaluation):
  98. batch_size batch size of input dataset: N, default is 16
  99. seq_length length of input sequence: N, default is 128
  100. vocab_size size of each embedding vector: N, default is 21136
  101. hidden_size size of bert encoder layers: N, default is 768
  102. num_hidden_layers number of hidden layers: N, default is 12
  103. num_attention_heads number of attention heads: N, default is 12
  104. intermediate_size size of intermediate layer: N, default is 3072
  105. hidden_act activation function used: ACTIVATION, default is "gelu"
  106. hidden_dropout_prob dropout probability for BertOutput: Q, default is 0.1
  107. attention_probs_dropout_prob dropout probability for BertAttention: Q, default is 0.1
  108. max_position_embeddings maximum length of sequences: N, default is 512
  109. type_vocab_size size of token type vocab: N, default is 16
  110. initializer_range initialization value of TruncatedNormal: Q, default is 0.02
  111. use_relative_positions use relative positions or not: True | False, default is False
  112. input_mask_from_dataset use the input mask loaded form dataset or not: True | False, default is True
  113. token_type_ids_from_dataset use the token type ids loaded from dataset or not: True | False, default is True
  114. dtype data type of input: mstype.float16 | mstype.float32, default is mstype.float32
  115. compute_type compute type in BertTransformer: mstype.float16 | mstype.float32, default is mstype.float16
  116. Parameters for optimizer:
  117. AdamWeightDecayDynamicLR:
  118. decay_steps steps of the learning rate decay: N
  119. learning_rate value of learning rate: Q
  120. end_learning_rate value of end learning rate: Q, must be positive
  121. power power: Q
  122. warmup_steps steps of the learning rate warm up: N
  123. weight_decay weight decay: Q
  124. eps term added to the denominator to improve numerical stability: Q
  125. Lamb:
  126. decay_steps steps of the learning rate decay: N
  127. learning_rate value of learning rate: Q
  128. end_learning_rate value of end learning rate: Q
  129. power power: Q
  130. warmup_steps steps of the learning rate warm up: N
  131. weight_decay weight decay: Q
  132. Momentum:
  133. learning_rate value of learning rate: Q
  134. momentum momentum for the moving average: Q
  135. ```