course/videos/debug_training_pt.ipynb (338 lines of code) (raw):
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook regroups the code sample of the video below, which is a part of the [Hugging Face course](https://huggingface.co/course)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form"
},
"outputs": [
{
"data": {
"text/html": [
"<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/L-WSwUWde1U?rel=0&controls=0&showinfo=0\" frameborder=\"0\" allowfullscreen></iframe>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#@title\n",
"from IPython.display import HTML\n",
"\n",
"HTML('<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/L-WSwUWde1U?rel=0&controls=0&showinfo=0\" frameborder=\"0\" allowfullscreen></iframe>')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Install the Transformers and Datasets libraries to run this notebook."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"! pip install datasets transformers[sentencepiece]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from datasets import load_dataset, load_metric\n",
"from transformers import (\n",
" AutoTokenizer,\n",
" AutoModelForSequenceClassification,\n",
" TrainingArguments,\n",
" Trainer,\n",
")\n",
"\n",
"raw_datasets = load_dataset(\"glue\", \"mnli\")\n",
"model_checkpoint = \"distilbert-base-uncased\"\n",
"tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)\n",
"\n",
"def preprocess_function(examples):\n",
" return tokenizer(examples[\"premise\"], examples[\"hypothesis\"], truncation=True)\n",
"\n",
"tokenized_datasets = raw_datasets.map(preprocess_function, batched=True)\n",
"model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint)\n",
"args = TrainingArguments(\n",
" \"distilbert-finetuned-mnli\",\n",
" evaluation_strategy=\"epoch\",\n",
" save_strategy=\"epoch\",\n",
" learning_rate=2e-5,\n",
" num_train_epochs=3,\n",
" weight_decay=0.01,\n",
")\n",
"metric = load_metric(\"glue\", \"mnli\")\n",
"\n",
"def compute_metrics(eval_pred):\n",
" predictions, labels = eval_pred\n",
" predictions = np.argmax(predictions, axis=1)\n",
" return metric.compute(\n",
" predictions=predictions, references=labels\n",
" )\n",
"\n",
"trainer = Trainer(\n",
" model,\n",
" args,\n",
" train_dataset=raw_datasets[\"train\"],\n",
" eval_dataset=raw_datasets[\"validation_matched\"],\n",
" compute_metrics=compute_metrics,\n",
")\n",
"trainer.train()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"trainer.train_dataset[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from datasets import load_dataset, load_metric\n",
"from transformers import (\n",
" AutoTokenizer,\n",
" AutoModelForSequenceClassification,\n",
" TrainingArguments,\n",
" Trainer,\n",
")\n",
"\n",
"raw_datasets = load_dataset(\"glue\", \"mnli\")\n",
"model_checkpoint = \"distilbert-base-uncased\"\n",
"tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)\n",
"\n",
"def preprocess_function(examples):\n",
" return tokenizer(examples[\"premise\"], examples[\"hypothesis\"], truncation=True)\n",
"\n",
"tokenized_datasets = raw_datasets.map(preprocess_function, batched=True)\n",
"model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint)\n",
"args = TrainingArguments(\n",
" \"distilbert-finetuned-mnli\",\n",
" evaluation_strategy=\"epoch\",\n",
" save_strategy=\"epoch\",\n",
" learning_rate=2e-5,\n",
" num_train_epochs=3,\n",
" weight_decay=0.01,\n",
")\n",
"metric = load_metric(\"glue\", \"mnli\")\n",
"\n",
"def compute_metrics(eval_pred):\n",
" predictions, labels = eval_pred\n",
" predictions = np.argmax(predictions, axis=1)\n",
" return metric.compute(\n",
" predictions=predictions, references=labels\n",
" )\n",
"\n",
"trainer = Trainer(\n",
" model,\n",
" args,\n",
" train_dataset=tokenized_datasets[\"train\"],\n",
" eval_dataset=tokenized_datasets[\"validation_matched\"],\n",
" compute_metrics=compute_metrics,\n",
")\n",
"trainer.train()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for batch in trainer.get_train_dataloader():\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from datasets import load_dataset, load_metric\n",
"from transformers import (\n",
" AutoTokenizer,\n",
" AutoModelForSequenceClassification,\n",
" TrainingArguments,\n",
" Trainer,\n",
")\n",
"\n",
"raw_datasets = load_dataset(\"glue\", \"mnli\")\n",
"model_checkpoint = \"distilbert-base-uncased\"\n",
"tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)\n",
"\n",
"def preprocess_function(examples):\n",
" return tokenizer(examples[\"premise\"], examples[\"hypothesis\"], truncation=True)\n",
"\n",
"tokenized_datasets = raw_datasets.map(preprocess_function, batched=True)\n",
"model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint)\n",
"args = TrainingArguments(\n",
" \"distilbert-finetuned-mnli\",\n",
" evaluation_strategy=\"epoch\",\n",
" save_strategy=\"epoch\",\n",
" learning_rate=2e-5,\n",
" num_train_epochs=3,\n",
" weight_decay=0.01,\n",
")\n",
"metric = load_metric(\"glue\", \"mnli\")\n",
"\n",
"def compute_metrics(eval_pred):\n",
" predictions, labels = eval_pred\n",
" predictions = np.argmax(predictions, axis=1)\n",
" return metric.compute(\n",
" predictions=predictions, references=labels\n",
" )\n",
"\n",
"trainer = Trainer(\n",
" model,\n",
" args,\n",
" train_dataset=tokenized_datasets[\"train\"],\n",
" eval_dataset=tokenized_datasets[\"validation_matched\"],\n",
" compute_metrics=compute_metrics,\n",
" tokenizer=tokenizer,\n",
")\n",
"trainer.train()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for batch in trainer.get_train_dataloader():\n",
" break\n",
"\n",
"outputs = trainer.model.cpu()(**batch)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.num_labels"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from datasets import load_dataset, load_metric\n",
"from transformers import (\n",
" AutoTokenizer,\n",
" AutoModelForSequenceClassification,\n",
" TrainingArguments,\n",
" Trainer,\n",
")\n",
"\n",
"raw_datasets = load_dataset(\"glue\", \"mnli\")\n",
"model_checkpoint = \"distilbert-base-uncased\"\n",
"tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)\n",
"\n",
"def preprocess_function(examples):\n",
" return tokenizer(examples[\"premise\"], examples[\"hypothesis\"], truncation=True)\n",
"\n",
"tokenized_datasets = raw_datasets.map(preprocess_function, batched=True)\n",
"model = AutoModelForSequenceClassification.from_pretrained(\n",
" model_checkpoint, num_labels=3\n",
")\n",
"args = TrainingArguments(\n",
" \"distilbert-finetuned-mnli\",\n",
" evaluation_strategy=\"epoch\",\n",
" save_strategy=\"epoch\",\n",
" learning_rate=2e-5,\n",
" num_train_epochs=3,\n",
" weight_decay=0.01,\n",
")\n",
"metric = load_metric(\"glue\", \"mnli\")\n",
"\n",
"def compute_metrics(eval_pred):\n",
" predictions, labels = eval_pred\n",
" predictions = np.argmax(predictions, axis=1)\n",
" return metric.compute(\n",
" predictions=predictions, references=labels\n",
" )\n",
"\n",
"trainer = Trainer(\n",
" model,\n",
" args,\n",
" train_dataset=tokenized_datasets[\"train\"],\n",
" eval_dataset=tokenized_datasets[\"validation_matched\"],\n",
" compute_metrics=compute_metrics,\n",
" tokenizer=tokenizer,\n",
")\n",
"trainer.train()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for batch in trainer.get_train_dataloader():\n",
" break\n",
"\n",
"outputs = trainer.model.cpu()(**batch)\n",
"loss = outputs.loss\n",
"loss.backward()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"trainer.create_optimizer()\n",
"trainer.optimizer.step()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"name": "Debugging the Training Pipeline (PyTorch)",
"provenance": []
}
},
"nbformat": 4,
"nbformat_minor": 4
}