{ "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": [ "" ], "text/plain": [ "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#@title\n", "from IPython.display import HTML\n", "\n", "HTML('')" ] }, { "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", " TFAutoModelForSequenceClassification,\n", ")\n", "\n", "raw_datasets = load_dataset(\"glue\", \"mnli\")\n", "\n", "model_checkpoint = \"distilbert-base-uncased\"\n", "tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)\n", "\n", "\n", "def preprocess_function(examples):\n", " return tokenizer(examples[\"premise\"], examples[\"hypothesis\"], truncation=True)\n", "\n", "\n", "tokenized_datasets = raw_datasets.map(preprocess_function, batched=True)\n", "model = TFAutoModelForSequenceClassification.from_pretrained(model_checkpoint)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "train_dataset = tokenized_datasets[\"train\"].to_tf_dataset(\n", " columns=[\"input_ids\", \"labels\"], batch_size=16, shuffle=True\n", ")\n", "\n", "validation_dataset = tokenized_datasets[\"validation_matched\"].to_tf_dataset(\n", " columns=[\"input_ids\", \"labels\"], batch_size=16, shuffle=True\n", ")\n", "\n", "model = TFAutoModelForSequenceClassification.from_pretrained(model_checkpoint)\n", "\n", "model.compile(loss=\"sparse_categorical_crossentropy\", optimizer='adam')\n", "\n", "model.fit(train_dataset)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for batch in train_dataset:\n", " break" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.compile(optimizer='adam')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = TFAutoModelForSequenceClassification.from_pretrained(\n", " model_checkpoint,\n", " num_labels=3\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.compile(optimizer='adam')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "colab": { "name": "Debugging the Training Pipeline (TensorFlow)", "provenance": [] } }, "nbformat": 4, "nbformat_minor": 4 }