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 8.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 from <https://dumps.wikimedia.org/zhwiki> for pre-training. Extract and clean text in the dataset with [WikiExtractor](https://github.com/attardi/wiliextractor). Convert the dataset to TFRecord format and move the files to a specified path.
  7. - Download the CLUE dataset from <https://www.cluebenchmarks.com> 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`. Make sure the 'DATA_DIR'(path to the dataset) and 'SCHEMA_DIR'(path to the json schema file) are set to your own path. 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_pretrain.py` for pre-training of BERT-base and BERT-NEZHA model.
  14. ``` bash
  15. python run_pretrain.py --backend=ms
  16. ```
  17. ### Fine-Tuning
  18. - Set options in `finetune_config.py`. Make sure the 'data_file', 'schema_file' and 'ckpt_file' are set to your own path, set the 'pre_training_ckpt' to save the checkpoint files generated.
  19. - Run `finetune.py` for fine-tuning of BERT-base and BERT-NEZHA model.
  20. ```bash
  21. python finetune.py --backend=ms
  22. ```
  23. ### Evaluation
  24. - Set options in `evaluation_config.py`. Make sure the 'data_file', 'schema_file' and 'finetune_ckpt' are set to your own path.
  25. - Run `evaluation.py` for evaluation of BERT-base and BERT-NEZHA model.
  26. ```bash
  27. python evaluation.py --backend=ms
  28. ```
  29. ## Usage
  30. ### Pre-Training
  31. ```
  32. usage: run_pretrain.py [--backend BACKEND]
  33. optional parameters:
  34. --backend, BACKEND MindSpore backend: ms
  35. ```
  36. ## Options and Parameters
  37. 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.
  38. ### Options:
  39. ```
  40. Pre-Training:
  41. bert_network version of BERT model: base | large, default is base
  42. epoch_size repeat counts of training: N, default is 40
  43. dataset_sink_mode use dataset sink mode or not: True | False, default is True
  44. do_shuffle shuffle the dataset or not: True | False, default is True
  45. do_train_with_lossscale use lossscale or not: True | False, default is True
  46. loss_scale_value initial value of loss scale: N, default is 2^32
  47. scale_factor factor used to update loss scale: N, default is 2
  48. scale_window steps for once updatation of loss scale: N, default is 1000
  49. save_checkpoint_steps steps to save a checkpoint: N, default is 2000
  50. keep_checkpoint_max numbers to save checkpoint: N, default is 1
  51. init_ckpt checkpoint file to load: PATH, default is ""
  52. data_dir dataset file to load: PATH, default is "/your/path/cn-wiki-128"
  53. schema_dir dataset schema file to load: PATH, default is "your/path/datasetSchema.json"
  54. optimizer optimizer used in the network: AdamWerigtDecayDynamicLR | Lamb | Momentum, default is "Lamb"
  55. Fine-Tuning:
  56. task task type: NER | XNLI | LCQMC | SENTI
  57. data_file dataset file to load: PATH, default is "/your/path/cn-wiki-128"
  58. schema_file dataset schema file to load: PATH, default is "/your/path/datasetSchema.json"
  59. epoch_num repeat counts of training: N, default is 40
  60. ckpt_prefix prefix used to save checkpoint files: PREFIX, default is "bert"
  61. ckpt_dir path to save checkpoint files: PATH, default is None
  62. pre_training_ckpt checkpoint file to load: PATH, default is "/your/path/pre_training.ckpt"
  63. optimizer optimizer used in the network: AdamWeigtDecayDynamicLR | Lamb | Momentum, default is "Lamb"
  64. Evaluation:
  65. task task type: NER | XNLI | LCQMC | SENTI
  66. data_file dataset file to load: PATH, default is "/your/path/evaluation.tfrecord"
  67. schema_file dataset schema file to load: PATH, default is "/your/path/schema.json"
  68. finetune_ckpt checkpoint file to load: PATH, default is "/your/path/your.ckpt"
  69. ```
  70. ### Parameters:
  71. ```
  72. Parameters for dataset and network (Pre-Training/Fine-Tuning/Evaluation):
  73. batch_size batch size of input dataset: N, default is 16
  74. seq_length length of input sequence: N, default is 128
  75. vocab_size size of each embedding vector: N, default is 21136
  76. hidden_size size of bert encoder layers: N, default is 768
  77. num_hidden_layers number of hidden layers: N, default is 12
  78. num_attention_heads number of attention heads: N, default is 12
  79. intermediate_size size of intermediate layer: N, default is 3072
  80. hidden_act activation function used: ACTIVATION, default is "gelu"
  81. hidden_dropout_prob dropout probability for BertOutput: Q, default is 0.1
  82. attention_probs_dropout_prob dropout probability for BertAttention: Q, default is 0.1
  83. max_position_embeddings maximum length of sequences: N, default is 512
  84. type_vocab_size size of token type vocab: N, default is 16
  85. initializer_range initialization value of TruncatedNormal: Q, default is 0.02
  86. use_relative_positions use relative positions or not: True | False, default is False
  87. input_mask_from_dataset use the input mask loaded form dataset or not: True | False, default is True
  88. token_type_ids_from_dataset use the token type ids loaded from dataset or not: True | False, default is True
  89. dtype data type of input: mstype.float16 | mstype.float32, default is mstype.float32
  90. compute_type compute type in BertTransformer: mstype.float16 | mstype.float32, default is mstype.float16
  91. Parameters for optimizer:
  92. AdamWeightDecayDynamicLR:
  93. decay_steps steps of the learning rate decay: N, default is 12276*3
  94. learning_rate value of learning rate: Q, default is 1e-5
  95. end_learning_rate value of end learning rate: Q, default is 0.0
  96. power power: Q, default is 10.0
  97. warmup_steps steps of the learning rate warm up: N, default is 2100
  98. weight_decay weight decay: Q, default is 1e-5
  99. eps term added to the denominator to improve numerical stability: Q, default is 1e-6
  100. Lamb:
  101. decay_steps steps of the learning rate decay: N, default is 12276*3
  102. learning_rate value of learning rate: Q, default is 1e-5
  103. end_learning_rate value of end learning rate: Q, default is 0.0
  104. power power: Q, default is 5.0
  105. warmup_steps steps of the learning rate warm up: N, default is 2100
  106. weight_decay weight decay: Q, default is 1e-5
  107. decay_filter function to determine whether to apply weight decay on parameters: FUNCTION, default is lambda x: False
  108. Momentum:
  109. learning_rate value of learning rate: Q, default is 2e-5
  110. momentum momentum for the moving average: Q, default is 0.9
  111. ```