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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. "fp16": False,
  78. } # setting whether to use FP16
  79. }
  80. automl.fit(
  81. X_train=X_train, y_train=y_train, X_val=X_val, y_val=y_val, **automl_settings
  82. )
  83. ```
  84. #### Sample output
  85. ```
  86. [flaml.automl: 12-20 11:47:28] {1965} INFO - task = seq-regression
  87. [flaml.automl: 12-20 11:47:28] {1967} INFO - Data split method: uniform
  88. [flaml.automl: 12-20 11:47:28] {1971} INFO - Evaluation method: holdout
  89. [flaml.automl: 12-20 11:47:28] {2063} INFO - Minimizing error metric: rmse
  90. [flaml.automl: 12-20 11:47:28] {2115} INFO - List of ML learners in AutoML Run: ['transformer']
  91. [flaml.automl: 12-20 11:47:28] {2355} INFO - iteration 0, current learner transformer
  92. ```
  93. ### A simple summarization example
  94. ```python
  95. from flaml import AutoML
  96. from datasets import load_dataset
  97. train_dataset = (
  98. load_dataset("xsum", split="train").to_pandas()
  99. )
  100. dev_dataset = (
  101. load_dataset("xsum", split="validation").to_pandas()
  102. )
  103. custom_sent_keys = ["document"]
  104. label_key = "summary"
  105. X_train = train_dataset[custom_sent_keys]
  106. y_train = train_dataset[label_key]
  107. X_val = dev_dataset[custom_sent_keys]
  108. y_val = dev_dataset[label_key]
  109. automl = AutoML()
  110. automl_settings = {
  111. "gpu_per_trial": 1,
  112. "time_budget": 20,
  113. "task": "summarization",
  114. "metric": "rouge1",
  115. }
  116. automl_settings["fit_kwargs_by_estimator"] = { # setting the huggingface arguments
  117. "transformer": {
  118. "model_path": "t5-small", # if model_path is not set, the default model is t5-small: https://huggingface.co/t5-small
  119. "output_dir": "data/output/", # setting the output directory
  120. "fp16": False,
  121. } # setting whether to use FP16
  122. }
  123. automl.fit(
  124. X_train=X_train, y_train=y_train, X_val=X_val, y_val=y_val, **automl_settings
  125. )
  126. ```
  127. #### Sample Output
  128. ```
  129. [flaml.automl: 12-20 11:44:03] {1965} INFO - task = summarization
  130. [flaml.automl: 12-20 11:44:03] {1967} INFO - Data split method: uniform
  131. [flaml.automl: 12-20 11:44:03] {1971} INFO - Evaluation method: holdout
  132. [flaml.automl: 12-20 11:44:03] {2063} INFO - Minimizing error metric: -rouge
  133. [flaml.automl: 12-20 11:44:03] {2115} INFO - List of ML learners in AutoML Run: ['transformer']
  134. [flaml.automl: 12-20 11:44:03] {2355} INFO - iteration 0, current learner transformer
  135. loading configuration file https://huggingface.co/t5-small/resolve/main/config.json from cache at /home/xliu127/.cache/huggingface/transformers/fe501e8fd6425b8ec93df37767fcce78ce626e34cc5edc859c662350cf712e41.406701565c0afd9899544c1cb8b93185a76f00b31e5ce7f6e18bbaef02241985
  136. Model config T5Config {
  137. "_name_or_path": "t5-small",
  138. "architectures": [
  139. "T5WithLMHeadModel"
  140. ],
  141. "d_ff": 2048,
  142. "d_kv": 64,
  143. "d_model": 512,
  144. "decoder_start_token_id": 0,
  145. "dropout_rate": 0.1,
  146. "eos_token_id": 1,
  147. "feed_forward_proj": "relu",
  148. "initializer_factor": 1.0,
  149. "is_encoder_decoder": true,
  150. "layer_norm_epsilon": 1e-06,
  151. "model_type": "t5",
  152. "n_positions": 512,
  153. "num_decoder_layers": 6,
  154. "num_heads": 8,
  155. "num_layers": 6,
  156. "output_past": true,
  157. "pad_token_id": 0,
  158. "relative_attention_num_buckets": 32,
  159. "task_specific_params": {
  160. "summarization": {
  161. "early_stopping": true,
  162. "length_penalty": 2.0,
  163. "max_length": 200,
  164. "min_length": 30,
  165. "no_repeat_ngram_size": 3,
  166. "num_beams": 4,
  167. "prefix": "summarize: "
  168. },
  169. "translation_en_to_de": {
  170. "early_stopping": true,
  171. "max_length": 300,
  172. "num_beams": 4,
  173. "prefix": "translate English to German: "
  174. },
  175. "translation_en_to_fr": {
  176. "early_stopping": true,
  177. "max_length": 300,
  178. "num_beams": 4,
  179. "prefix": "translate English to French: "
  180. },
  181. "translation_en_to_ro": {
  182. "early_stopping": true,
  183. "max_length": 300,
  184. "num_beams": 4,
  185. "prefix": "translate English to Romanian: "
  186. }
  187. },
  188. "transformers_version": "4.14.1",
  189. "use_cache": true,
  190. "vocab_size": 32128
  191. }
  192. ```
  193. ### A simple token classification example
  194. There are two ways to define the label for a token classification task. The first is to define the token labels:
  195. ```python
  196. from flaml import AutoML
  197. import pandas as pd
  198. train_dataset = {
  199. "id": ["0", "1"],
  200. "ner_tags": [
  201. ["B-ORG", "O", "B-MISC", "O", "O", "O", "B-MISC", "O", "O"],
  202. ["B-PER", "I-PER"],
  203. ],
  204. "tokens": [
  205. [
  206. "EU", "rejects", "German", "call", "to", "boycott", "British", "lamb", ".",
  207. ],
  208. ["Peter", "Blackburn"],
  209. ],
  210. }
  211. dev_dataset = {
  212. "id": ["0"],
  213. "ner_tags": [
  214. ["O"],
  215. ],
  216. "tokens": [
  217. ["1996-08-22"]
  218. ],
  219. }
  220. test_dataset = {
  221. "id": ["0"],
  222. "ner_tags": [
  223. ["O"],
  224. ],
  225. "tokens": [
  226. ['.']
  227. ],
  228. }
  229. custom_sent_keys = ["tokens"]
  230. label_key = "ner_tags"
  231. train_dataset = pd.DataFrame(train_dataset)
  232. dev_dataset = pd.DataFrame(dev_dataset)
  233. test_dataset = pd.DataFrame(test_dataset)
  234. X_train, y_train = train_dataset[custom_sent_keys], train_dataset[label_key]
  235. X_val, y_val = dev_dataset[custom_sent_keys], dev_dataset[label_key]
  236. X_test = test_dataset[custom_sent_keys]
  237. automl = AutoML()
  238. automl_settings = {
  239. "time_budget": 10,
  240. "task": "token-classification",
  241. "fit_kwargs_by_estimator": {
  242. "transformer":
  243. {
  244. "output_dir": "data/output/"
  245. # if model_path is not set, the default model is facebook/muppet-roberta-base: https://huggingface.co/facebook/muppet-roberta-base
  246. }
  247. }, # setting the huggingface arguments: output directory
  248. "gpu_per_trial": 1, # set to 0 if no GPU is available
  249. "metric": "seqeval:overall_f1"
  250. }
  251. automl.fit(X_train=X_train, y_train=y_train, X_val=X_val, y_val=y_val, **automl_settings)
  252. automl.predict(X_test)
  253. ```
  254. The second is to define the id labels + a token [label list](https://microsoft.github.io/FLAML/docs/reference/nlp/huggingface/training_args):
  255. ```python
  256. from flaml import AutoML
  257. import pandas as pd
  258. train_dataset = {
  259. "id": ["0", "1"],
  260. "ner_tags": [
  261. [3, 0, 7, 0, 0, 0, 7, 0, 0],
  262. [1, 2],
  263. ],
  264. "tokens": [
  265. [
  266. "EU", "rejects", "German", "call", "to", "boycott", "British", "lamb", ".",
  267. ],
  268. ["Peter", "Blackburn"],
  269. ],
  270. }
  271. dev_dataset = {
  272. "id": ["0"],
  273. "ner_tags": [
  274. [0],
  275. ],
  276. "tokens": [
  277. ["1996-08-22"]
  278. ],
  279. }
  280. test_dataset = {
  281. "id": ["0"],
  282. "ner_tags": [
  283. [0],
  284. ],
  285. "tokens": [
  286. ['.']
  287. ],
  288. }
  289. custom_sent_keys = ["tokens"]
  290. label_key = "ner_tags"
  291. train_dataset = pd.DataFrame(train_dataset)
  292. dev_dataset = pd.DataFrame(dev_dataset)
  293. test_dataset = pd.DataFrame(test_dataset)
  294. X_train, y_train = train_dataset[custom_sent_keys], train_dataset[label_key]
  295. X_val, y_val = dev_dataset[custom_sent_keys], dev_dataset[label_key]
  296. X_test = test_dataset[custom_sent_keys]
  297. automl = AutoML()
  298. automl_settings = {
  299. "time_budget": 10,
  300. "task": "token-classification",
  301. "fit_kwargs_by_estimator": {
  302. "transformer":
  303. {
  304. "output_dir": "data/output/",
  305. # if model_path is not set, the default model is facebook/muppet-roberta-base: https://huggingface.co/facebook/muppet-roberta-base
  306. "label_list": [ "O","B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "B-MISC", "I-MISC" ]
  307. }
  308. }, # setting the huggingface arguments: output directory
  309. "gpu_per_trial": 1, # set to 0 if no GPU is available
  310. "metric": "seqeval:overall_f1"
  311. }
  312. automl.fit(X_train=X_train, y_train=y_train, X_val=X_val, y_val=y_val, **automl_settings)
  313. automl.predict(X_test)
  314. ```
  315. #### Sample Output
  316. ```
  317. [flaml.automl: 06-30 03:10:02] {2423} INFO - task = token-classification
  318. [flaml.automl: 06-30 03:10:02] {2425} INFO - Data split method: stratified
  319. [flaml.automl: 06-30 03:10:02] {2428} INFO - Evaluation method: holdout
  320. [flaml.automl: 06-30 03:10:02] {2497} INFO - Minimizing error metric: seqeval:overall_f1
  321. [flaml.automl: 06-30 03:10:02] {2637} INFO - List of ML learners in AutoML Run: ['transformer']
  322. [flaml.automl: 06-30 03:10:02] {2929} INFO - iteration 0, current learner transformer
  323. ```
  324. For tasks that are not currently supported, use `flaml.tune` for [customized tuning](Tune-HuggingFace).
  325. ### Link to Jupyter notebook
  326. To run more examples, especially examples using Ray Tune, please go to:
  327. [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)