Skip to content

Commit a9bf069

Browse files
committed
fix small typo
1 parent 18e2c28 commit a9bf069

19 files changed

+1643
-484
lines changed

integrations/huggingface-transformers/tutorials/sentiment-analysis/docs-sentiment-analysis-python-custom-teacher-rottentomatoes.ipynb

+56-36
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@
6060
"import numpy as np\n",
6161
"from transformers import (\n",
6262
" AutoModelForSequenceClassification,\n",
63-
" AutoConfig, \n",
64-
" AutoTokenizer, \n",
65-
" EvalPrediction, \n",
66-
" default_data_collator\n",
63+
" AutoConfig,\n",
64+
" AutoTokenizer,\n",
65+
" EvalPrediction,\n",
66+
" default_data_collator,\n",
6767
")\n",
6868
"from datasets import load_dataset, load_metric"
6969
]
@@ -96,8 +96,8 @@
9696
"dataset[\"train\"].to_csv(\"rotten_tomatoes-train.csv\")\n",
9797
"dataset[\"validation\"].to_csv(\"rotten_tomatoes-validation.csv\")\n",
9898
"data_files = {\n",
99-
" \"train\": \"rotten_tomatoes-train.csv\",\n",
100-
" \"validation\": \"rotten_tomatoes-validation.csv\"\n",
99+
" \"train\": \"rotten_tomatoes-train.csv\",\n",
100+
" \"validation\": \"rotten_tomatoes-validation.csv\",\n",
101101
"}\n",
102102
"dataset_from_json = load_dataset(\"csv\", data_files=data_files)"
103103
]
@@ -163,13 +163,14 @@
163163
"source": [
164164
"metric = load_metric(\"accuracy\")\n",
165165
"\n",
166+
"\n",
166167
"def compute_metrics(p: EvalPrediction):\n",
167-
" preds = p.predictions[0] if isinstance(p.predictions, tuple) else p.predictions\n",
168-
" preds = np.argmax(preds, axis=1)\n",
169-
" result = metric.compute(predictions=preds, references=p.label_ids)\n",
170-
" if len(result) > 1:\n",
171-
" result[\"combined_score\"] = np.mean(list(result.values())).item()\n",
172-
" return result"
168+
" preds = p.predictions[0] if isinstance(p.predictions, tuple) else p.predictions\n",
169+
" preds = np.argmax(preds, axis=1)\n",
170+
" result = metric.compute(predictions=preds, references=p.label_ids)\n",
171+
" if len(result) > 1:\n",
172+
" result[\"combined_score\"] = np.mean(list(result.values())).item()\n",
173+
" return result"
173174
]
174175
},
175176
{
@@ -209,12 +210,16 @@
209210
"outputs": [],
210211
"source": [
211212
"# downloads 90% pruned upstream BERT trained on MLM objective\n",
212-
"model_stub = \"zoo:nlp/masked_language_modeling/obert-base/pytorch/huggingface/wikipedia_bookcorpus/pruned90-none\" \n",
213-
"model_path = Model(model_stub, download_path=\"./model\").training.path \n",
213+
"model_stub = \"zoo:nlp/masked_language_modeling/obert-base/pytorch/huggingface/wikipedia_bookcorpus/pruned90-none\"\n",
214+
"model_path = Model(model_stub, download_path=\"./model\").training.path\n",
214215
"\n",
215216
"# downloads transfer recipe for MNLI (pruned90_quant)\n",
216-
"transfer_stub = \"zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/pruned90_quant-none\"\n",
217-
"recipe_path = Model(transfer_stub, download_path=\"./transfer_recipe\").recipes.default.path"
217+
"transfer_stub = (\n",
218+
" \"zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/pruned90_quant-none\"\n",
219+
")\n",
220+
"recipe_path = Model(\n",
221+
" transfer_stub, download_path=\"./transfer_recipe\"\n",
222+
").recipes.default.path"
218223
]
219224
},
220225
{
@@ -363,13 +368,23 @@
363368
"# initialize model using familiar HF AutoModel\n",
364369
"model_kwargs = {\"config\": model_config}\n",
365370
"model_kwargs[\"state_dict\"], s_delayed = SparseAutoModel._loadable_state_dict(model_path)\n",
366-
"model = AutoModelForSequenceClassification.from_pretrained(model_path,**model_kwargs,)\n",
367-
"SparseAutoModel.log_model_load(model, model_path, \"student\", s_delayed) # prints metrics on sparsity profile\n",
371+
"model = AutoModelForSequenceClassification.from_pretrained(\n",
372+
" model_path,\n",
373+
" **model_kwargs,\n",
374+
")\n",
375+
"SparseAutoModel.log_model_load(\n",
376+
" model, model_path, \"student\", s_delayed\n",
377+
") # prints metrics on sparsity profile\n",
368378
"\n",
369379
"# initialize teacher using familiar HF AutoModel\n",
370380
"teacher_kwargs = {\"config\": teacher_config}\n",
371-
"teacher_kwargs[\"state_dict\"], t_delayed = SparseAutoModel._loadable_state_dict(teacher_path)\n",
372-
"teacher = AutoModelForSequenceClassification.from_pretrained(teacher_path,**teacher_kwargs,)\n",
381+
"teacher_kwargs[\"state_dict\"], t_delayed = SparseAutoModel._loadable_state_dict(\n",
382+
" teacher_path\n",
383+
")\n",
384+
"teacher = AutoModelForSequenceClassification.from_pretrained(\n",
385+
" teacher_path,\n",
386+
" **teacher_kwargs,\n",
387+
")\n",
373388
"SparseAutoModel.log_model_load(teacher, teacher_path, \"teacher\", t_delayed)"
374389
]
375390
},
@@ -393,22 +408,25 @@
393408
"outputs": [],
394409
"source": [
395410
"MAX_LEN = 128\n",
411+
"\n",
412+
"\n",
396413
"def preprocess_fn(examples):\n",
397-
" args = None\n",
398-
" if INPUT_COL_2 is None:\n",
399-
" args = (examples[INPUT_COL_1], )\n",
400-
" else:\n",
401-
" args = (examples[INPUT_COL_1], examples[INPUT_COL_2])\n",
402-
" result = tokenizer(*args, \n",
403-
" padding=\"max_length\", \n",
404-
" max_length=min(tokenizer.model_max_length, MAX_LEN), \n",
405-
" truncation=True)\n",
406-
" return result\n",
414+
" args = None\n",
415+
" if INPUT_COL_2 is None:\n",
416+
" args = (examples[INPUT_COL_1],)\n",
417+
" else:\n",
418+
" args = (examples[INPUT_COL_1], examples[INPUT_COL_2])\n",
419+
" result = tokenizer(\n",
420+
" *args,\n",
421+
" padding=\"max_length\",\n",
422+
" max_length=min(tokenizer.model_max_length, MAX_LEN),\n",
423+
" truncation=True,\n",
424+
" )\n",
425+
" return result\n",
426+
"\n",
407427
"\n",
408428
"tokenized_dataset = dataset_from_json.map(\n",
409-
" preprocess_fn,\n",
410-
" batched=True,\n",
411-
" desc=\"Running tokenizer on dataset\"\n",
429+
" preprocess_fn, batched=True, desc=\"Running tokenizer on dataset\"\n",
412430
")"
413431
]
414432
},
@@ -447,20 +465,22 @@
447465
" save_total_limit=1,\n",
448466
" per_device_train_batch_size=32,\n",
449467
" per_device_eval_batch_size=32,\n",
450-
" fp16=True)\n",
468+
" fp16=True,\n",
469+
")\n",
451470
"\n",
452471
"trainer = Trainer(\n",
453472
" model=model,\n",
454473
" model_state_path=model_path,\n",
455474
" recipe=recipe_path,\n",
456475
" teacher=teacher,\n",
457-
" metadata_args=[\"per_device_train_batch_size\",\"per_device_eval_batch_size\",\"fp16\"],\n",
476+
" metadata_args=[\"per_device_train_batch_size\", \"per_device_eval_batch_size\", \"fp16\"],\n",
458477
" args=training_args,\n",
459478
" train_dataset=tokenized_dataset[\"train\"],\n",
460479
" eval_dataset=tokenized_dataset[\"validation\"],\n",
461480
" tokenizer=tokenizer,\n",
462481
" data_collator=default_data_collator,\n",
463-
" compute_metrics=compute_metrics)"
482+
" compute_metrics=compute_metrics,\n",
483+
")"
464484
]
465485
},
466486
{

integrations/huggingface-transformers/tutorials/sentiment-analysis/docs-sentiment-analysis-python-sst2.ipynb

+54-39
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@
7272
"import numpy as np\n",
7373
"from transformers import (\n",
7474
" AutoModelForSequenceClassification,\n",
75-
" AutoConfig, \n",
76-
" AutoTokenizer, \n",
77-
" EvalPrediction, \n",
78-
" default_data_collator\n",
75+
" AutoConfig,\n",
76+
" AutoTokenizer,\n",
77+
" EvalPrediction,\n",
78+
" default_data_collator,\n",
7979
")\n",
8080
"from datasets import load_dataset, load_metric"
8181
]
@@ -129,10 +129,7 @@
129129
"dataset[\"train\"].to_csv(\"sst2-train.csv\")\n",
130130
"dataset[\"validation\"].to_csv(\"sst2-validation.csv\")\n",
131131
"\n",
132-
"data_files = {\n",
133-
" \"train\": \"sst2-train.csv\",\n",
134-
" \"validation\": \"sst2-validation.csv\"\n",
135-
"}\n",
132+
"data_files = {\"train\": \"sst2-train.csv\", \"validation\": \"sst2-validation.csv\"}\n",
136133
"dataset = load_dataset(\"csv\", data_files=data_files)"
137134
]
138135
},
@@ -193,13 +190,14 @@
193190
"source": [
194191
"metric = load_metric(\"glue\", \"sst2\")\n",
195192
"\n",
193+
"\n",
196194
"def compute_metrics(p: EvalPrediction):\n",
197-
" preds = p.predictions[0] if isinstance(p.predictions, tuple) else p.predictions\n",
198-
" preds = np.argmax(preds, axis=1)\n",
199-
" result = metric.compute(predictions=preds, references=p.label_ids)\n",
200-
" if len(result) > 1:\n",
201-
" result[\"combined_score\"] = np.mean(list(result.values())).item()\n",
202-
" return result"
195+
" preds = p.predictions[0] if isinstance(p.predictions, tuple) else p.predictions\n",
196+
" preds = np.argmax(preds, axis=1)\n",
197+
" result = metric.compute(predictions=preds, references=p.label_ids)\n",
198+
" if len(result) > 1:\n",
199+
" result[\"combined_score\"] = np.mean(list(result.values())).item()\n",
200+
" return result"
203201
]
204202
},
205203
{
@@ -249,10 +247,10 @@
249247
"outputs": [],
250248
"source": [
251249
"# downloads pruned-BERT model\n",
252-
"model_stub = \"zoo:nlp/masked_language_modeling/obert-base/pytorch/huggingface/wikipedia_bookcorpus/pruned90-none\" \n",
250+
"model_stub = \"zoo:nlp/masked_language_modeling/obert-base/pytorch/huggingface/wikipedia_bookcorpus/pruned90-none\"\n",
253251
"download_dir = \"./model\"\n",
254252
"zoo_model = Model(model_stub, download_path=download_dir)\n",
255-
"model_path = zoo_model.training.path \n",
253+
"model_path = zoo_model.training.path\n",
256254
"\n",
257255
"print(model_path)"
258256
]
@@ -277,7 +275,9 @@
277275
"outputs": [],
278276
"source": [
279277
"# downloads transfer learning recipe\n",
280-
"transfer_stub = \"zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/pruned90_quant-none\"\n",
278+
"transfer_stub = (\n",
279+
" \"zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/pruned90_quant-none\"\n",
280+
")\n",
281281
"download_dir = \"./transfer_recipe\"\n",
282282
"zoo_model = Model(transfer_stub, download_path=download_dir)\n",
283283
"recipe_path = zoo_model.recipes.default.path\n",
@@ -305,10 +305,12 @@
305305
"outputs": [],
306306
"source": [
307307
"# downloads teacher\n",
308-
"teacher_stub = \"zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/base-none\"\n",
308+
"teacher_stub = (\n",
309+
" \"zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/base-none\"\n",
310+
")\n",
309311
"download_dir = \"./teacher\"\n",
310312
"zoo_model = Model(teacher_stub, download_path=download_dir)\n",
311-
"teacher_path = zoo_model.training.path "
313+
"teacher_path = zoo_model.training.path"
312314
]
313315
},
314316
{
@@ -436,11 +438,19 @@
436438
"\n",
437439
"model_kwargs = {\"config\": model_config}\n",
438440
"model_kwargs[\"state_dict\"], s_delayed = SparseAutoModel._loadable_state_dict(model_path)\n",
439-
"model = AutoModelForSequenceClassification.from_pretrained(model_path, **model_kwargs,)\n",
441+
"model = AutoModelForSequenceClassification.from_pretrained(\n",
442+
" model_path,\n",
443+
" **model_kwargs,\n",
444+
")\n",
440445
"\n",
441-
"teacher_kwargs = {'config':teacher_config}\n",
442-
"teacher_kwargs[\"state_dict\"], t_delayed = SparseAutoModel._loadable_state_dict(teacher_path)\n",
443-
"teacher = AutoModelForSequenceClassification.from_pretrained(teacher_path, **teacher_kwargs,)\n",
446+
"teacher_kwargs = {\"config\": teacher_config}\n",
447+
"teacher_kwargs[\"state_dict\"], t_delayed = SparseAutoModel._loadable_state_dict(\n",
448+
" teacher_path\n",
449+
")\n",
450+
"teacher = AutoModelForSequenceClassification.from_pretrained(\n",
451+
" teacher_path,\n",
452+
" **teacher_kwargs,\n",
453+
")\n",
444454
"\n",
445455
"# optional - prints metrics about sparsity profiles of the models\n",
446456
"SparseAutoModel.log_model_load(model, model_path, \"student\", s_delayed)\n",
@@ -495,22 +505,25 @@
495505
"outputs": [],
496506
"source": [
497507
"MAX_LEN = 128\n",
508+
"\n",
509+
"\n",
498510
"def preprocess_fn(examples):\n",
499-
" args = None\n",
500-
" if INPUT_COL_2 is None:\n",
501-
" args = (examples[INPUT_COL_1], )\n",
502-
" else:\n",
503-
" args = (examples[INPUT_COL_1], examples[INPUT_COL_2])\n",
504-
" result = tokenizer(*args, \n",
505-
" padding=\"max_length\", \n",
506-
" max_length=min(tokenizer.model_max_length, MAX_LEN), \n",
507-
" truncation=True)\n",
508-
" return result\n",
511+
" args = None\n",
512+
" if INPUT_COL_2 is None:\n",
513+
" args = (examples[INPUT_COL_1],)\n",
514+
" else:\n",
515+
" args = (examples[INPUT_COL_1], examples[INPUT_COL_2])\n",
516+
" result = tokenizer(\n",
517+
" *args,\n",
518+
" padding=\"max_length\",\n",
519+
" max_length=min(tokenizer.model_max_length, MAX_LEN),\n",
520+
" truncation=True,\n",
521+
" )\n",
522+
" return result\n",
523+
"\n",
509524
"\n",
510525
"tokenized_dataset = dataset.map(\n",
511-
" preprocess_fn,\n",
512-
" batched=True,\n",
513-
" desc=\"Running tokenizer on dataset\"\n",
526+
" preprocess_fn, batched=True, desc=\"Running tokenizer on dataset\"\n",
514527
")"
515528
]
516529
},
@@ -549,20 +562,22 @@
549562
" save_total_limit=1,\n",
550563
" per_device_train_batch_size=32,\n",
551564
" per_device_eval_batch_size=32,\n",
552-
" fp16=True)\n",
565+
" fp16=True,\n",
566+
")\n",
553567
"\n",
554568
"trainer = Trainer(\n",
555569
" model=model,\n",
556570
" model_state_path=model_path,\n",
557571
" recipe=recipe_path,\n",
558572
" teacher=teacher,\n",
559-
" metadata_args=[\"per_device_train_batch_size\",\"per_device_eval_batch_size\",\"fp16\"],\n",
573+
" metadata_args=[\"per_device_train_batch_size\", \"per_device_eval_batch_size\", \"fp16\"],\n",
560574
" args=training_args,\n",
561575
" train_dataset=tokenized_dataset[\"train\"],\n",
562576
" eval_dataset=tokenized_dataset[\"validation\"],\n",
563577
" tokenizer=tokenizer,\n",
564578
" data_collator=default_data_collator,\n",
565-
" compute_metrics=compute_metrics)"
579+
" compute_metrics=compute_metrics,\n",
580+
")"
566581
]
567582
},
568583
{

0 commit comments

Comments
 (0)