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.

AutoML-NLP.md 8.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # AutoML - NLP
  2. ### Requirements
  3. This example requires GPU. Install the [nlp] option:
  4. ```python
  5. pip install "flaml[nlp]"
  6. ```
  7. ### A simple sequence classification example
  8. ```python
  9. from flaml import AutoML
  10. from datasets import load_dataset
  11. train_dataset = load_dataset("glue", "mrpc", split="train").to_pandas()
  12. dev_dataset = load_dataset("glue", "mrpc", split="validation").to_pandas()
  13. test_dataset = load_dataset("glue", "mrpc", split="test").to_pandas()
  14. custom_sent_keys = ["sentence1", "sentence2"]
  15. label_key = "label"
  16. X_train, y_train = train_dataset[custom_sent_keys], train_dataset[label_key]
  17. X_val, y_val = dev_dataset[custom_sent_keys], dev_dataset[label_key]
  18. X_test = test_dataset[custom_sent_keys]
  19. automl = AutoML()
  20. automl_settings = {
  21. "time_budget": 100,
  22. "task": "seq-classification",
  23. "fit_kwargs_by_estimator": {
  24. "transformer":
  25. {
  26. "output_dir": "data/output/" # if model_path is not set, the default model is facebook/muppet-roberta-base: https://huggingface.co/facebook/muppet-roberta-base
  27. }
  28. }, # setting the huggingface arguments: output directory
  29. "gpu_per_trial": 1, # set to 0 if no GPU is available
  30. }
  31. automl.fit(X_train=X_train, y_train=y_train, X_val=X_val, y_val=y_val, **automl_settings)
  32. automl.predict(X_test)
  33. ```
  34. #### Sample output
  35. ```
  36. [flaml.automl: 12-06 08:21:39] {1943} INFO - task = seq-classification
  37. [flaml.automl: 12-06 08:21:39] {1945} INFO - Data split method: stratified
  38. [flaml.automl: 12-06 08:21:39] {1949} INFO - Evaluation method: holdout
  39. [flaml.automl: 12-06 08:21:39] {2019} INFO - Minimizing error metric: 1-accuracy
  40. [flaml.automl: 12-06 08:21:39] {2071} INFO - List of ML learners in AutoML Run: ['transformer']
  41. [flaml.automl: 12-06 08:21:39] {2311} INFO - iteration 0, current learner transformer
  42. {'data/output/train_2021-12-06_08-21-53/train_8947b1b2_1_n=1e-06,s=9223372036854775807,e=1e-05,s=-1,s=0.45765,e=32,d=42,o=0.0,y=0.0_2021-12-06_08-21-53/checkpoint-53': 53}
  43. [flaml.automl: 12-06 08:22:56] {2424} INFO - Estimated sufficient time budget=766860s. Estimated necessary time budget=767s.
  44. [flaml.automl: 12-06 08:22:56] {2499} INFO - at 76.7s, estimator transformer's best error=0.1740, best estimator transformer's best error=0.1740
  45. [flaml.automl: 12-06 08:22:56] {2606} INFO - selected model: <flaml.nlp.huggingface.trainer.TrainerForAuto object at 0x7f49ea8414f0>
  46. [flaml.automl: 12-06 08:22:56] {2100} INFO - fit succeeded
  47. [flaml.automl: 12-06 08:22:56] {2101} INFO - Time taken to find the best model: 76.69802761077881
  48. [flaml.automl: 12-06 08:22:56] {2112} WARNING - Time taken to find the best model is 77% of the provided time budget and not all estimators' hyperparameter search converged. Consider increasing the time budget.
  49. ```
  50. ### A simple sequence regression example
  51. ```python
  52. from flaml import AutoML
  53. from datasets import load_dataset
  54. train_dataset = (
  55. load_dataset("glue", "stsb", split="train").to_pandas()
  56. )
  57. dev_dataset = (
  58. load_dataset("glue", "stsb", split="train").to_pandas()
  59. )
  60. custom_sent_keys = ["sentence1", "sentence2"]
  61. label_key = "label"
  62. X_train = train_dataset[custom_sent_keys]
  63. y_train = train_dataset[label_key]
  64. X_val = dev_dataset[custom_sent_keys]
  65. y_val = dev_dataset[label_key]
  66. automl = AutoML()
  67. automl_settings = {
  68. "gpu_per_trial": 0,
  69. "time_budget": 20,
  70. "task": "seq-regression",
  71. "metric": "rmse",
  72. }
  73. automl_settings["fit_kwargs_by_estimator"] = { # setting the huggingface arguments
  74. "transformer": {
  75. "model_path": "google/electra-small-discriminator", # if model_path is not set, the default model is facebook/muppet-roberta-base: https://huggingface.co/facebook/muppet-roberta-base
  76. "output_dir": "data/output/", # setting the output directory
  77. "ckpt_per_epoch": 5, # setting the number of checkpoints per epoch
  78. "fp16": False,
  79. } # setting whether to use FP16
  80. }
  81. automl.fit(
  82. X_train=X_train, y_train=y_train, X_val=X_val, y_val=y_val, **automl_settings
  83. )
  84. ```
  85. #### Sample output
  86. ```
  87. [flaml.automl: 12-20 11:47:28] {1965} INFO - task = seq-regression
  88. [flaml.automl: 12-20 11:47:28] {1967} INFO - Data split method: uniform
  89. [flaml.automl: 12-20 11:47:28] {1971} INFO - Evaluation method: holdout
  90. [flaml.automl: 12-20 11:47:28] {2063} INFO - Minimizing error metric: rmse
  91. [flaml.automl: 12-20 11:47:28] {2115} INFO - List of ML learners in AutoML Run: ['transformer']
  92. [flaml.automl: 12-20 11:47:28] {2355} INFO - iteration 0, current learner transformer
  93. ```
  94. ### A simple summarization example
  95. ```python
  96. from flaml import AutoML
  97. from datasets import load_dataset
  98. train_dataset = (
  99. load_dataset("xsum", split="train").to_pandas()
  100. )
  101. dev_dataset = (
  102. load_dataset("xsum", split="validation").to_pandas()
  103. )
  104. custom_sent_keys = ["document"]
  105. label_key = "summary"
  106. X_train = train_dataset[custom_sent_keys]
  107. y_train = train_dataset[label_key]
  108. X_val = dev_dataset[custom_sent_keys]
  109. y_val = dev_dataset[label_key]
  110. automl = AutoML()
  111. automl_settings = {
  112. "gpu_per_trial": 1,
  113. "time_budget": 20,
  114. "task": "summarization",
  115. "metric": "rouge1",
  116. }
  117. automl_settings["fit_kwargs_by_estimator"] = { # setting the huggingface arguments
  118. "transformer": {
  119. "model_path": "t5-small", # if model_path is not set, the default model is t5-small: https://huggingface.co/t5-small
  120. "output_dir": "data/output/", # setting the output directory
  121. "ckpt_per_epoch": 5, # setting the number of checkpoints per epoch
  122. "fp16": False,
  123. } # setting whether to use FP16
  124. }
  125. automl.fit(
  126. X_train=X_train, y_train=y_train, X_val=X_val, y_val=y_val, **automl_settings
  127. )
  128. ```
  129. #### Sample Output
  130. ```
  131. [flaml.automl: 12-20 11:44:03] {1965} INFO - task = summarization
  132. [flaml.automl: 12-20 11:44:03] {1967} INFO - Data split method: uniform
  133. [flaml.automl: 12-20 11:44:03] {1971} INFO - Evaluation method: holdout
  134. [flaml.automl: 12-20 11:44:03] {2063} INFO - Minimizing error metric: -rouge
  135. [flaml.automl: 12-20 11:44:03] {2115} INFO - List of ML learners in AutoML Run: ['transformer']
  136. [flaml.automl: 12-20 11:44:03] {2355} INFO - iteration 0, current learner transformer
  137. loading configuration file https://huggingface.co/t5-small/resolve/main/config.json from cache at /home/xliu127/.cache/huggingface/transformers/fe501e8fd6425b8ec93df37767fcce78ce626e34cc5edc859c662350cf712e41.406701565c0afd9899544c1cb8b93185a76f00b31e5ce7f6e18bbaef02241985
  138. Model config T5Config {
  139. "_name_or_path": "t5-small",
  140. "architectures": [
  141. "T5WithLMHeadModel"
  142. ],
  143. "d_ff": 2048,
  144. "d_kv": 64,
  145. "d_model": 512,
  146. "decoder_start_token_id": 0,
  147. "dropout_rate": 0.1,
  148. "eos_token_id": 1,
  149. "feed_forward_proj": "relu",
  150. "initializer_factor": 1.0,
  151. "is_encoder_decoder": true,
  152. "layer_norm_epsilon": 1e-06,
  153. "model_type": "t5",
  154. "n_positions": 512,
  155. "num_decoder_layers": 6,
  156. "num_heads": 8,
  157. "num_layers": 6,
  158. "output_past": true,
  159. "pad_token_id": 0,
  160. "relative_attention_num_buckets": 32,
  161. "task_specific_params": {
  162. "summarization": {
  163. "early_stopping": true,
  164. "length_penalty": 2.0,
  165. "max_length": 200,
  166. "min_length": 30,
  167. "no_repeat_ngram_size": 3,
  168. "num_beams": 4,
  169. "prefix": "summarize: "
  170. },
  171. "translation_en_to_de": {
  172. "early_stopping": true,
  173. "max_length": 300,
  174. "num_beams": 4,
  175. "prefix": "translate English to German: "
  176. },
  177. "translation_en_to_fr": {
  178. "early_stopping": true,
  179. "max_length": 300,
  180. "num_beams": 4,
  181. "prefix": "translate English to French: "
  182. },
  183. "translation_en_to_ro": {
  184. "early_stopping": true,
  185. "max_length": 300,
  186. "num_beams": 4,
  187. "prefix": "translate English to Romanian: "
  188. }
  189. },
  190. "transformers_version": "4.14.1",
  191. "use_cache": true,
  192. "vocab_size": 32128
  193. }
  194. ```
  195. For tasks that are not currently supported, use `flaml.tune` for [customized tuning](Tune-HuggingFace).
  196. ### Link to Jupyter notebook
  197. To run these examples in our Jupyter notebook, please go to:
  198. [Link to notebook](https://github.com/microsoft/FLAML/blob/main/notebook/automl_nlp.ipynb) | [Open in colab](https://colab.research.google.com/github/microsoft/FLAML/blob/main/notebook/automl_nlp.ipynb)