{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "43f7-wG-Tjg_" }, "source": [ "# FineTuning NLP Models with FLAML Library\n", "\n", "\n", "## 1. Introduction\n", "\n", "FLAML is a Python library (https://github.com/microsoft/FLAML) designed to automatically produce accurate machine learning models \n", "with low computational cost. It is fast and economical. The simple and lightweight design makes it easy to use and extend, such as adding new learners. FLAML can \n", "- serve as an economical AutoML engine,\n", "- be used as a fast hyperparameter tuning tool, or \n", "- be embedded in self-tuning software that requires low latency & resource in repetitive\n", " tuning tasks.\n", "\n", "In this notebook, we demonstrate how to use the FLAML library to fine tune an NLP language model with hyperparameter search. We will use [flaml.tune](https://microsoft.github.io/FLAML/docs/Use-Cases/Tune-User-Defined-Function) with the built in GPU in colab for the tuning. However, if you have a machine with more than 1 GPU, you can also use FLAML's [parallel tuning](https://microsoft.github.io/FLAML/docs/Use-Cases/Task-Oriented-AutoML#parallel-tuning) with the ray tune option. \n", "\n", "FLAML requires `Python>=3.7`. To run this notebook example, please install flaml with the `nlp,notebook` and `blendsearch` option:\n", "```bash\n", "pip install flaml[nlp,notebook,blendsearch]; \n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Q8c3VMy6TjhC", "outputId": "3584a81d-f26e-4eb9-9929-629cfff97ee9" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Collecting flaml[blendsearch,notebook,ray]\n", " Downloading FLAML-1.2.0-py3-none-any.whl (250 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m250.4/250.4 kB\u001b[0m \u001b[31m4.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: scikit-learn>=0.24 in /usr/local/lib/python3.9/dist-packages (from flaml[blendsearch,notebook,ray]) (1.2.2)\n", "Requirement already satisfied: xgboost>=0.90 in /usr/local/lib/python3.9/dist-packages (from flaml[blendsearch,notebook,ray]) (1.7.5)\n", "Requirement already satisfied: NumPy>=1.17.0rc1 in /usr/local/lib/python3.9/dist-packages (from flaml[blendsearch,notebook,ray]) (1.22.4)\n", "Requirement already satisfied: pandas>=1.1.4 in /usr/local/lib/python3.9/dist-packages (from flaml[blendsearch,notebook,ray]) (1.5.3)\n", "Requirement already satisfied: lightgbm>=2.3.1 in /usr/local/lib/python3.9/dist-packages (from flaml[blendsearch,notebook,ray]) (3.3.5)\n", "Requirement already satisfied: scipy>=1.4.1 in /usr/local/lib/python3.9/dist-packages (from flaml[blendsearch,notebook,ray]) (1.10.1)\n", "Collecting optuna==2.8.0\n", " Downloading optuna-2.8.0-py3-none-any.whl (301 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m302.0/302.0 kB\u001b[0m \u001b[31m17.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting ray[tune]~=1.13\n", " Downloading ray-1.13.0-cp39-cp39-manylinux2014_x86_64.whl (54.3 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m54.3/54.3 MB\u001b[0m \u001b[31m12.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting openml==0.10.2\n", " Downloading openml-0.10.2.tar.gz (158 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m159.0/159.0 kB\u001b[0m \u001b[31m9.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Collecting jupyter\n", " Downloading jupyter-1.0.0-py2.py3-none-any.whl (2.7 kB)\n", "Requirement already satisfied: matplotlib in /usr/local/lib/python3.9/dist-packages (from flaml[blendsearch,notebook,ray]) (3.7.1)\n", "Collecting liac-arff>=2.4.0\n", " Downloading liac-arff-2.5.0.tar.gz (13 kB)\n", " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Collecting xmltodict\n", " Downloading xmltodict-0.13.0-py2.py3-none-any.whl (10.0 kB)\n", "Requirement already satisfied: requests in /usr/local/lib/python3.9/dist-packages (from openml==0.10.2->flaml[blendsearch,notebook,ray]) (2.27.1)\n", "Requirement already satisfied: python-dateutil in /usr/local/lib/python3.9/dist-packages (from openml==0.10.2->flaml[blendsearch,notebook,ray]) (2.8.2)\n", "Collecting alembic\n", " Downloading alembic-1.10.3-py3-none-any.whl (212 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m212.3/212.3 kB\u001b[0m \u001b[31m5.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting cliff\n", " Downloading cliff-4.2.0-py3-none-any.whl (81 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m81.0/81.0 kB\u001b[0m \u001b[31m2.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: sqlalchemy>=1.1.0 in /usr/local/lib/python3.9/dist-packages (from optuna==2.8.0->flaml[blendsearch,notebook,ray]) (2.0.9)\n", "Collecting cmaes>=0.8.2\n", " Downloading cmaes-0.9.1-py3-none-any.whl (21 kB)\n", "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.9/dist-packages (from optuna==2.8.0->flaml[blendsearch,notebook,ray]) (23.0)\n", "Collecting colorlog\n", " Downloading colorlog-6.7.0-py2.py3-none-any.whl (11 kB)\n", "Requirement already satisfied: tqdm in /usr/local/lib/python3.9/dist-packages (from optuna==2.8.0->flaml[blendsearch,notebook,ray]) (4.65.0)\n", "Requirement already satisfied: wheel in /usr/local/lib/python3.9/dist-packages (from lightgbm>=2.3.1->flaml[blendsearch,notebook,ray]) (0.40.0)\n", "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.9/dist-packages (from pandas>=1.1.4->flaml[blendsearch,notebook,ray]) (2022.7.1)\n", "Requirement already satisfied: jsonschema in /usr/local/lib/python3.9/dist-packages (from ray[tune]~=1.13->flaml[blendsearch,notebook,ray]) (4.3.3)\n", "Requirement already satisfied: filelock in /usr/local/lib/python3.9/dist-packages (from ray[tune]~=1.13->flaml[blendsearch,notebook,ray]) (3.11.0)\n", "Collecting click<=8.0.4,>=7.0\n", " Downloading click-8.0.4-py3-none-any.whl (97 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m97.5/97.5 kB\u001b[0m \u001b[31m11.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting frozenlist\n", " Downloading frozenlist-1.3.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (158 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m158.8/158.8 kB\u001b[0m \u001b[31m20.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting grpcio<=1.43.0,>=1.28.1\n", " Downloading grpcio-1.43.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m4.1/4.1 MB\u001b[0m \u001b[31m45.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting virtualenv\n", " Downloading virtualenv-20.21.0-py3-none-any.whl (8.7 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m8.7/8.7 MB\u001b[0m \u001b[31m43.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: msgpack<2.0.0,>=1.0.0 in /usr/local/lib/python3.9/dist-packages (from ray[tune]~=1.13->flaml[blendsearch,notebook,ray]) (1.0.5)\n", "Requirement already satisfied: protobuf<4.0.0,>=3.15.3 in /usr/local/lib/python3.9/dist-packages (from ray[tune]~=1.13->flaml[blendsearch,notebook,ray]) (3.20.3)\n", "Requirement already satisfied: pyyaml in /usr/local/lib/python3.9/dist-packages (from ray[tune]~=1.13->flaml[blendsearch,notebook,ray]) (6.0)\n", "Requirement already satisfied: attrs in /usr/local/lib/python3.9/dist-packages (from ray[tune]~=1.13->flaml[blendsearch,notebook,ray]) (22.2.0)\n", "Collecting aiosignal\n", " Downloading aiosignal-1.3.1-py3-none-any.whl (7.6 kB)\n", "Requirement already satisfied: tabulate in /usr/local/lib/python3.9/dist-packages (from ray[tune]~=1.13->flaml[blendsearch,notebook,ray]) (0.8.10)\n", "Collecting tensorboardX>=1.9\n", " Downloading tensorboardX-2.6-py2.py3-none-any.whl (114 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m114.5/114.5 kB\u001b[0m \u001b[31m16.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: joblib>=1.1.1 in /usr/local/lib/python3.9/dist-packages (from scikit-learn>=0.24->flaml[blendsearch,notebook,ray]) (1.2.0)\n", "Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.9/dist-packages (from scikit-learn>=0.24->flaml[blendsearch,notebook,ray]) (3.1.0)\n", "Requirement already satisfied: jupyter-console in /usr/local/lib/python3.9/dist-packages (from jupyter->flaml[blendsearch,notebook,ray]) (6.1.0)\n", "Collecting qtconsole\n", " Downloading qtconsole-5.4.2-py3-none-any.whl (121 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m121.2/121.2 kB\u001b[0m \u001b[31m9.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: nbconvert in /usr/local/lib/python3.9/dist-packages (from jupyter->flaml[blendsearch,notebook,ray]) (6.5.4)\n", "Requirement already satisfied: ipywidgets in /usr/local/lib/python3.9/dist-packages (from jupyter->flaml[blendsearch,notebook,ray]) (7.7.1)\n", "Requirement already satisfied: ipykernel in /usr/local/lib/python3.9/dist-packages (from jupyter->flaml[blendsearch,notebook,ray]) (5.5.6)\n", "Requirement already satisfied: notebook in /usr/local/lib/python3.9/dist-packages (from jupyter->flaml[blendsearch,notebook,ray]) (6.4.8)\n", "Requirement already satisfied: importlib-resources>=3.2.0 in /usr/local/lib/python3.9/dist-packages (from matplotlib->flaml[blendsearch,notebook,ray]) (5.12.0)\n", "Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.9/dist-packages (from matplotlib->flaml[blendsearch,notebook,ray]) (8.4.0)\n", "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.9/dist-packages (from matplotlib->flaml[blendsearch,notebook,ray]) (3.0.9)\n", "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.9/dist-packages (from matplotlib->flaml[blendsearch,notebook,ray]) (0.11.0)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.9/dist-packages (from matplotlib->flaml[blendsearch,notebook,ray]) (1.4.4)\n", "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.9/dist-packages (from matplotlib->flaml[blendsearch,notebook,ray]) (1.0.7)\n", "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.9/dist-packages (from matplotlib->flaml[blendsearch,notebook,ray]) (4.39.3)\n", "Requirement already satisfied: six>=1.5.2 in /usr/local/lib/python3.9/dist-packages (from grpcio<=1.43.0,>=1.28.1->ray[tune]~=1.13->flaml[blendsearch,notebook,ray]) (1.16.0)\n", "Requirement already satisfied: zipp>=3.1.0 in /usr/local/lib/python3.9/dist-packages (from importlib-resources>=3.2.0->matplotlib->flaml[blendsearch,notebook,ray]) (3.15.0)\n", "Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.9/dist-packages (from sqlalchemy>=1.1.0->optuna==2.8.0->flaml[blendsearch,notebook,ray]) (2.0.2)\n", "Requirement already satisfied: typing-extensions>=4.2.0 in /usr/local/lib/python3.9/dist-packages (from sqlalchemy>=1.1.0->optuna==2.8.0->flaml[blendsearch,notebook,ray]) (4.5.0)\n", "Collecting Mako\n", " Downloading Mako-1.2.4-py3-none-any.whl (78 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m78.7/78.7 kB\u001b[0m \u001b[31m9.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: PrettyTable>=0.7.2 in /usr/local/lib/python3.9/dist-packages (from cliff->optuna==2.8.0->flaml[blendsearch,notebook,ray]) (0.7.2)\n", "Collecting autopage>=0.4.0\n", " Downloading autopage-0.5.1-py3-none-any.whl (29 kB)\n", "Collecting cmd2>=1.0.0\n", " Downloading cmd2-2.4.3-py3-none-any.whl (147 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m147.2/147.2 kB\u001b[0m \u001b[31m19.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting stevedore>=2.0.1\n", " Downloading stevedore-5.0.0-py3-none-any.whl (49 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.6/49.6 kB\u001b[0m \u001b[31m6.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: importlib-metadata>=4.4 in /usr/local/lib/python3.9/dist-packages (from cliff->optuna==2.8.0->flaml[blendsearch,notebook,ray]) (6.2.0)\n", "Requirement already satisfied: tornado>=4.2 in /usr/local/lib/python3.9/dist-packages (from ipykernel->jupyter->flaml[blendsearch,notebook,ray]) (6.2)\n", "Requirement already satisfied: ipython>=5.0.0 in /usr/local/lib/python3.9/dist-packages (from ipykernel->jupyter->flaml[blendsearch,notebook,ray]) (7.34.0)\n", "Requirement already satisfied: traitlets>=4.1.0 in /usr/local/lib/python3.9/dist-packages (from ipykernel->jupyter->flaml[blendsearch,notebook,ray]) (5.7.1)\n", "Requirement already satisfied: ipython-genutils in /usr/local/lib/python3.9/dist-packages (from ipykernel->jupyter->flaml[blendsearch,notebook,ray]) (0.2.0)\n", "Requirement already satisfied: jupyter-client in /usr/local/lib/python3.9/dist-packages (from ipykernel->jupyter->flaml[blendsearch,notebook,ray]) (6.1.12)\n", "Requirement already satisfied: jupyterlab-widgets>=1.0.0 in /usr/local/lib/python3.9/dist-packages (from ipywidgets->jupyter->flaml[blendsearch,notebook,ray]) (3.0.7)\n", "Requirement already satisfied: widgetsnbextension~=3.6.0 in /usr/local/lib/python3.9/dist-packages (from ipywidgets->jupyter->flaml[blendsearch,notebook,ray]) (3.6.4)\n", "Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /usr/local/lib/python3.9/dist-packages (from jsonschema->ray[tune]~=1.13->flaml[blendsearch,notebook,ray]) (0.19.3)\n", "Requirement already satisfied: pygments in /usr/local/lib/python3.9/dist-packages (from jupyter-console->jupyter->flaml[blendsearch,notebook,ray]) (2.14.0)\n", "Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /usr/local/lib/python3.9/dist-packages (from jupyter-console->jupyter->flaml[blendsearch,notebook,ray]) (3.0.38)\n", "Requirement already satisfied: lxml in /usr/local/lib/python3.9/dist-packages (from nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (4.9.2)\n", "Requirement already satisfied: jinja2>=3.0 in /usr/local/lib/python3.9/dist-packages (from nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (3.1.2)\n", "Requirement already satisfied: jupyter-core>=4.7 in /usr/local/lib/python3.9/dist-packages (from nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (5.3.0)\n", "Requirement already satisfied: nbclient>=0.5.0 in /usr/local/lib/python3.9/dist-packages (from nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (0.7.3)\n", "Requirement already satisfied: entrypoints>=0.2.2 in /usr/local/lib/python3.9/dist-packages (from nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (0.4)\n", "Requirement already satisfied: pandocfilters>=1.4.1 in /usr/local/lib/python3.9/dist-packages (from nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (1.5.0)\n", "Requirement already satisfied: bleach in /usr/local/lib/python3.9/dist-packages (from nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (6.0.0)\n", "Requirement already satisfied: defusedxml in /usr/local/lib/python3.9/dist-packages (from nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (0.7.1)\n", "Requirement already satisfied: tinycss2 in /usr/local/lib/python3.9/dist-packages (from nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (1.2.1)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.9/dist-packages (from nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (2.1.2)\n", "Requirement already satisfied: jupyterlab-pygments in /usr/local/lib/python3.9/dist-packages (from nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (0.2.2)\n", "Requirement already satisfied: mistune<2,>=0.8.1 in /usr/local/lib/python3.9/dist-packages (from nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (0.8.4)\n", "Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.9/dist-packages (from nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (4.11.2)\n", "Requirement already satisfied: nbformat>=5.1 in /usr/local/lib/python3.9/dist-packages (from nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (5.8.0)\n", "Requirement already satisfied: nest-asyncio>=1.5 in /usr/local/lib/python3.9/dist-packages (from notebook->jupyter->flaml[blendsearch,notebook,ray]) (1.5.6)\n", "Requirement already satisfied: Send2Trash>=1.8.0 in /usr/local/lib/python3.9/dist-packages (from notebook->jupyter->flaml[blendsearch,notebook,ray]) (1.8.0)\n", "Requirement already satisfied: prometheus-client in /usr/local/lib/python3.9/dist-packages (from notebook->jupyter->flaml[blendsearch,notebook,ray]) (0.16.0)\n", "Requirement already satisfied: pyzmq>=17 in /usr/local/lib/python3.9/dist-packages (from notebook->jupyter->flaml[blendsearch,notebook,ray]) (23.2.1)\n", "Requirement already satisfied: argon2-cffi in /usr/local/lib/python3.9/dist-packages (from notebook->jupyter->flaml[blendsearch,notebook,ray]) (21.3.0)\n", "Requirement already satisfied: terminado>=0.8.3 in /usr/local/lib/python3.9/dist-packages (from notebook->jupyter->flaml[blendsearch,notebook,ray]) (0.17.1)\n", "Collecting qtpy>=2.0.1\n", " Downloading QtPy-2.3.1-py3-none-any.whl (84 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m84.9/84.9 kB\u001b[0m \u001b[31m11.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.9/dist-packages (from requests->openml==0.10.2->flaml[blendsearch,notebook,ray]) (3.4)\n", "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.9/dist-packages (from requests->openml==0.10.2->flaml[blendsearch,notebook,ray]) (2.0.12)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.9/dist-packages (from requests->openml==0.10.2->flaml[blendsearch,notebook,ray]) (2022.12.7)\n", "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.9/dist-packages (from requests->openml==0.10.2->flaml[blendsearch,notebook,ray]) (1.26.15)\n", "Collecting distlib<1,>=0.3.6\n", " Downloading distlib-0.3.6-py2.py3-none-any.whl (468 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m468.5/468.5 kB\u001b[0m \u001b[31m21.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: platformdirs<4,>=2.4 in /usr/local/lib/python3.9/dist-packages (from virtualenv->ray[tune]~=1.13->flaml[blendsearch,notebook,ray]) (3.2.0)\n", "Requirement already satisfied: wcwidth>=0.1.7 in /usr/local/lib/python3.9/dist-packages (from cmd2>=1.0.0->cliff->optuna==2.8.0->flaml[blendsearch,notebook,ray]) (0.2.6)\n", "Collecting pyperclip>=1.6\n", " Downloading pyperclip-1.8.2.tar.gz (20 kB)\n", " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Requirement already satisfied: pickleshare in /usr/local/lib/python3.9/dist-packages (from ipython>=5.0.0->ipykernel->jupyter->flaml[blendsearch,notebook,ray]) (0.7.5)\n", "Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.9/dist-packages (from ipython>=5.0.0->ipykernel->jupyter->flaml[blendsearch,notebook,ray]) (67.6.1)\n", "Requirement already satisfied: backcall in /usr/local/lib/python3.9/dist-packages (from ipython>=5.0.0->ipykernel->jupyter->flaml[blendsearch,notebook,ray]) (0.2.0)\n", "Requirement already satisfied: matplotlib-inline in /usr/local/lib/python3.9/dist-packages (from ipython>=5.0.0->ipykernel->jupyter->flaml[blendsearch,notebook,ray]) (0.1.6)\n", "Requirement already satisfied: pexpect>4.3 in /usr/local/lib/python3.9/dist-packages (from ipython>=5.0.0->ipykernel->jupyter->flaml[blendsearch,notebook,ray]) (4.8.0)\n", "Requirement already satisfied: decorator in /usr/local/lib/python3.9/dist-packages (from ipython>=5.0.0->ipykernel->jupyter->flaml[blendsearch,notebook,ray]) (4.4.2)\n", "Collecting jedi>=0.16\n", " Downloading jedi-0.18.2-py2.py3-none-any.whl (1.6 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.6/1.6 MB\u001b[0m \u001b[31m82.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: fastjsonschema in /usr/local/lib/python3.9/dist-packages (from nbformat>=5.1->nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (2.16.3)\n", "Collecting pbr!=2.1.0,>=2.0.0\n", " Downloading pbr-5.11.1-py2.py3-none-any.whl (112 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m112.7/112.7 kB\u001b[0m \u001b[31m18.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: ptyprocess in /usr/local/lib/python3.9/dist-packages (from terminado>=0.8.3->notebook->jupyter->flaml[blendsearch,notebook,ray]) (0.7.0)\n", "Requirement already satisfied: argon2-cffi-bindings in /usr/local/lib/python3.9/dist-packages (from argon2-cffi->notebook->jupyter->flaml[blendsearch,notebook,ray]) (21.2.0)\n", "Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.9/dist-packages (from beautifulsoup4->nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (2.4)\n", "Requirement already satisfied: webencodings in /usr/local/lib/python3.9/dist-packages (from bleach->nbconvert->jupyter->flaml[blendsearch,notebook,ray]) (0.5.1)\n", "Requirement already satisfied: parso<0.9.0,>=0.8.0 in /usr/local/lib/python3.9/dist-packages (from jedi>=0.16->ipython>=5.0.0->ipykernel->jupyter->flaml[blendsearch,notebook,ray]) (0.8.3)\n", "Requirement already satisfied: cffi>=1.0.1 in /usr/local/lib/python3.9/dist-packages (from argon2-cffi-bindings->argon2-cffi->notebook->jupyter->flaml[blendsearch,notebook,ray]) (1.15.1)\n", "Requirement already satisfied: pycparser in /usr/local/lib/python3.9/dist-packages (from cffi>=1.0.1->argon2-cffi-bindings->argon2-cffi->notebook->jupyter->flaml[blendsearch,notebook,ray]) (2.21)\n", "Building wheels for collected packages: openml, liac-arff, pyperclip\n", " Building wheel for openml (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for openml: filename=openml-0.10.2-py3-none-any.whl size=190321 sha256=6384a6a98dcf21a054e2457f2a12e83e7f09122e873ed8dab894d7a4649b869b\n", " Stored in directory: /root/.cache/pip/wheels/90/70/b9/37e0bd30dd46291f37d970e2032d557d7eb36b6ccabe47419c\n", " Building wheel for liac-arff (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for liac-arff: filename=liac_arff-2.5.0-py3-none-any.whl size=11732 sha256=45f0543f0ec70558329ca4338de37f0feb6b093e730eed20921f38040916fbf3\n", " Stored in directory: /root/.cache/pip/wheels/08/82/8b/5c514221984e88c059b94e36a71d4722e590acaae04deab22e\n", " Building wheel for pyperclip (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for pyperclip: filename=pyperclip-1.8.2-py3-none-any.whl size=11135 sha256=b59846b5e39f6f668d74e06e57b7ceaded7c46beffc70dc391b71c02c6425afb\n", " Stored in directory: /root/.cache/pip/wheels/0c/09/9e/49e21a6840ef7955b06d47394afef0058f0378c0914e48b8b8\n", "Successfully built openml liac-arff pyperclip\n", "Installing collected packages: pyperclip, distlib, xmltodict, virtualenv, tensorboardX, qtpy, pbr, Mako, liac-arff, jedi, grpcio, frozenlist, colorlog, cmd2, cmaes, click, autopage, stevedore, alembic, aiosignal, ray, openml, cliff, qtconsole, optuna, flaml, jupyter\n", " Attempting uninstall: grpcio\n", " Found existing installation: grpcio 1.53.0\n", " Uninstalling grpcio-1.53.0:\n", " Successfully uninstalled grpcio-1.53.0\n", " Attempting uninstall: click\n", " Found existing installation: click 8.1.3\n", " Uninstalling click-8.1.3:\n", " Successfully uninstalled click-8.1.3\n", "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", "tensorboard 2.12.1 requires grpcio>=1.48.2, but you have grpcio 1.43.0 which is incompatible.\n", "grpcio-status 1.48.2 requires grpcio>=1.48.2, but you have grpcio 1.43.0 which is incompatible.\n", "google-cloud-bigquery 3.9.0 requires grpcio<2.0dev,>=1.47.0, but you have grpcio 1.43.0 which is incompatible.\u001b[0m\u001b[31m\n", "\u001b[0mSuccessfully installed Mako-1.2.4 aiosignal-1.3.1 alembic-1.10.3 autopage-0.5.1 click-8.0.4 cliff-4.2.0 cmaes-0.9.1 cmd2-2.4.3 colorlog-6.7.0 distlib-0.3.6 flaml-1.2.0 frozenlist-1.3.3 grpcio-1.43.0 jedi-0.18.2 jupyter-1.0.0 liac-arff-2.5.0 openml-0.10.2 optuna-2.8.0 pbr-5.11.1 pyperclip-1.8.2 qtconsole-5.4.2 qtpy-2.3.1 ray-1.13.0 stevedore-5.0.0 tensorboardX-2.6 virtualenv-20.21.0 xmltodict-0.13.0\n" ] }, { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" }, "text/plain": [ "'1.2.0'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%pip install flaml[notebook,blendsearch,ray]\n", "import flaml\n", "flaml.__version__" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "lo1id59ntQX_", "outputId": "692c860d-d498-48f5-d983-f2d850f64bbb" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Collecting transformers\n", " Downloading transformers-4.27.4-py3-none-any.whl (6.8 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.8/6.8 MB\u001b[0m \u001b[31m67.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting huggingface-hub<1.0,>=0.11.0\n", " Downloading huggingface_hub-0.13.4-py3-none-any.whl (200 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m200.1/200.1 kB\u001b[0m \u001b[31m11.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.9/dist-packages (from transformers) (1.22.4)\n", "Requirement already satisfied: requests in /usr/local/lib/python3.9/dist-packages (from transformers) (2.27.1)\n", "Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.9/dist-packages (from transformers) (2022.10.31)\n", "Collecting tokenizers!=0.11.3,<0.14,>=0.11.1\n", " Downloading tokenizers-0.13.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.8 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m7.8/7.8 MB\u001b[0m \u001b[31m65.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.9/dist-packages (from transformers) (6.0)\n", "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.9/dist-packages (from transformers) (23.0)\n", "Requirement already satisfied: filelock in /usr/local/lib/python3.9/dist-packages (from transformers) (3.11.0)\n", "Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.9/dist-packages (from transformers) (4.65.0)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.9/dist-packages (from huggingface-hub<1.0,>=0.11.0->transformers) (4.5.0)\n", "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.9/dist-packages (from requests->transformers) (3.4)\n", "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.9/dist-packages (from requests->transformers) (2.0.12)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.9/dist-packages (from requests->transformers) (2022.12.7)\n", "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.9/dist-packages (from requests->transformers) (1.26.15)\n", "Installing collected packages: tokenizers, huggingface-hub, transformers\n", "Successfully installed huggingface-hub-0.13.4 tokenizers-0.13.3 transformers-4.27.4\n", "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Collecting datasets\n", " Downloading datasets-2.11.0-py3-none-any.whl (468 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m468.7/468.7 kB\u001b[0m \u001b[31m36.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: pandas in /usr/local/lib/python3.9/dist-packages (from datasets) (1.5.3)\n", "Collecting responses<0.19\n", " Downloading responses-0.18.0-py3-none-any.whl (38 kB)\n", "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.9/dist-packages (from datasets) (6.0)\n", "Collecting dill<0.3.7,>=0.3.0\n", " Downloading dill-0.3.6-py3-none-any.whl (110 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m110.5/110.5 kB\u001b[0m \u001b[31m11.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: pyarrow>=8.0.0 in /usr/local/lib/python3.9/dist-packages (from datasets) (9.0.0)\n", "Collecting xxhash\n", " Downloading xxhash-3.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (212 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m212.2/212.2 kB\u001b[0m \u001b[31m27.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting multiprocess\n", " Downloading multiprocess-0.70.14-py39-none-any.whl (132 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m132.9/132.9 kB\u001b[0m \u001b[31m20.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: huggingface-hub<1.0.0,>=0.11.0 in /usr/local/lib/python3.9/dist-packages (from datasets) (0.13.4)\n", "Requirement already satisfied: packaging in /usr/local/lib/python3.9/dist-packages (from datasets) (23.0)\n", "Collecting aiohttp\n", " Downloading aiohttp-3.8.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m58.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.9/dist-packages (from datasets) (1.22.4)\n", "Requirement already satisfied: tqdm>=4.62.1 in /usr/local/lib/python3.9/dist-packages (from datasets) (4.65.0)\n", "Requirement already satisfied: fsspec[http]>=2021.11.1 in /usr/local/lib/python3.9/dist-packages (from datasets) (2023.3.0)\n", "Requirement already satisfied: requests>=2.19.0 in /usr/local/lib/python3.9/dist-packages (from datasets) (2.27.1)\n", "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.9/dist-packages (from aiohttp->datasets) (1.3.1)\n", "Collecting async-timeout<5.0,>=4.0.0a3\n", " Downloading async_timeout-4.0.2-py3-none-any.whl (5.8 kB)\n", "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.9/dist-packages (from aiohttp->datasets) (22.2.0)\n", "Collecting multidict<7.0,>=4.5\n", " Downloading multidict-6.0.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (114 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m114.2/114.2 kB\u001b[0m \u001b[31m14.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: charset-normalizer<4.0,>=2.0 in /usr/local/lib/python3.9/dist-packages (from aiohttp->datasets) (2.0.12)\n", "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.9/dist-packages (from aiohttp->datasets) (1.3.3)\n", "Collecting yarl<2.0,>=1.0\n", " Downloading yarl-1.8.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (264 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m264.6/264.6 kB\u001b[0m \u001b[31m30.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.9/dist-packages (from huggingface-hub<1.0.0,>=0.11.0->datasets) (4.5.0)\n", "Requirement already satisfied: filelock in /usr/local/lib/python3.9/dist-packages (from huggingface-hub<1.0.0,>=0.11.0->datasets) (3.11.0)\n", "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.9/dist-packages (from requests>=2.19.0->datasets) (3.4)\n", "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.9/dist-packages (from requests>=2.19.0->datasets) (1.26.15)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.9/dist-packages (from requests>=2.19.0->datasets) (2022.12.7)\n", "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.9/dist-packages (from pandas->datasets) (2022.7.1)\n", "Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.9/dist-packages (from pandas->datasets) (2.8.2)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.9/dist-packages (from python-dateutil>=2.8.1->pandas->datasets) (1.16.0)\n", "Installing collected packages: xxhash, multidict, dill, async-timeout, yarl, responses, multiprocess, aiohttp, datasets\n", "Successfully installed aiohttp-3.8.4 async-timeout-4.0.2 datasets-2.11.0 dill-0.3.6 multidict-6.0.4 multiprocess-0.70.14 responses-0.18.0 xxhash-3.2.0 yarl-1.8.2\n", "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Collecting rouge_score\n", " Downloading rouge_score-0.1.2.tar.gz (17 kB)\n", " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Requirement already satisfied: absl-py in /usr/local/lib/python3.9/dist-packages (from rouge_score) (1.4.0)\n", "Requirement already satisfied: nltk in /usr/local/lib/python3.9/dist-packages (from rouge_score) (3.8.1)\n", "Requirement already satisfied: numpy in /usr/local/lib/python3.9/dist-packages (from rouge_score) (1.22.4)\n", "Requirement already satisfied: six>=1.14.0 in /usr/local/lib/python3.9/dist-packages (from rouge_score) (1.16.0)\n", "Requirement already satisfied: click in /usr/local/lib/python3.9/dist-packages (from nltk->rouge_score) (8.0.4)\n", "Requirement already satisfied: tqdm in /usr/local/lib/python3.9/dist-packages (from nltk->rouge_score) (4.65.0)\n", "Requirement already satisfied: joblib in /usr/local/lib/python3.9/dist-packages (from nltk->rouge_score) (1.2.0)\n", "Requirement already satisfied: regex>=2021.8.3 in /usr/local/lib/python3.9/dist-packages (from nltk->rouge_score) (2022.10.31)\n", "Building wheels for collected packages: rouge_score\n", " Building wheel for rouge_score (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for rouge_score: filename=rouge_score-0.1.2-py3-none-any.whl size=24954 sha256=4032d06ff03906dbf10b9d7bae49035b4d76498d3b86b286e1472939d2ee09b0\n", " Stored in directory: /root/.cache/pip/wheels/9b/3d/39/09558097d3119ca0a4d462df68f22c6f3c1b345ac63a09b86e\n", "Successfully built rouge_score\n", "Installing collected packages: rouge_score\n", "Successfully installed rouge_score-0.1.2\n" ] } ], "source": [ "%pip install transformers\n", "%pip install datasets\n", "%pip install rouge_score" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "j24pfyQktbln", "outputId": "29aa3747-5597-4528-b82a-95567b9020b9" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" }, "text/plain": [ "'4.27.4'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import transformers\n", "transformers.__version__" ] }, { "cell_type": "markdown", "metadata": { "id": "efPlAWTdTjhD" }, "source": [ "Let's run some examples. To use CoLab's built in GPU, you need to select Runtime -> Change runtime type and select GPU. Then you can print the device information using:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "2kx9QbI7uaU8", "outputId": "c9ad909f-a2fe-4d4f-aabd-552c2505f09e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "import torch\n", "print([torch.cuda.device(i) for i in range(torch.cuda.device_count())])" ] }, { "cell_type": "markdown", "metadata": { "id": "-yEuLXoHua-f" }, "source": [ "Note: throughout this notebook, you may see a few ModuleNotFoundErrors. As long as the cell successfully executes, you can ignore that error." ] }, { "cell_type": "markdown", "metadata": { "id": "ZBr83DYlTjhD" }, "source": [ "## 2. Sentiment Classification Example\n", "### Load data and preprocess\n", "\n", "The Stanford Sentiment treebank (SST-2) dataset is a dataset for sentiment classification. First, let's load this dataset into pandas dataframes:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "hGP2eqTBTjhD", "outputId": "2028b124-d720-49b6-ad8f-7cdf64d3f2bf" }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9eb9517f746b49c69728f32c8a420816", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Downloading builder script: 0%| | 0.00/28.8k [00:00\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sentencelabelidx
0hide new secretions from the parental units00
1contains no wit , only labored gags01
2that loves its characters and communicates som...12
3remains utterly satisfied to remain the same t...03
4on the worst revenge-of-the-nerds clichés the ...04
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", " \n", " " ], "text/plain": [ " sentence label idx\n", "0 hide new secretions from the parental units 0 0\n", "1 contains no wit , only labored gags 0 1\n", "2 that loves its characters and communicates som... 1 2\n", "3 remains utterly satisfied to remain the same t... 0 3\n", "4 on the worst revenge-of-the-nerds clichés the ... 0 4" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "train_dataset.head(5)" ] }, { "cell_type": "markdown", "metadata": { "id": "ENcUQbOgTjhE" }, "source": [ "Separate the data into X and y:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "GA0VH9URTjhF" }, "outputs": [], "source": [ "custom_sent_keys = [\"sentence\"] # specify the column names of the input sentences\n", "label_key = \"label\" # specify the column name of the label\n", "\n", "X_train, y_train = train_dataset[custom_sent_keys], train_dataset[label_key]\n", "X_val, y_val = dev_dataset[custom_sent_keys], dev_dataset[label_key]\n", "X_test = test_dataset[custom_sent_keys]" ] }, { "cell_type": "markdown", "metadata": { "id": "NpRqB153TjhF" }, "source": [ "### Run FLAML" ] }, { "cell_type": "markdown", "metadata": { "id": "2kXabqxZuzQl" }, "source": [ "Now we can run AutoML with FLAML:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "asYbkzrXTjhF" }, "outputs": [], "source": [ "from flaml import AutoML\n", "automl = AutoML()\n" ] }, { "cell_type": "markdown", "metadata": { "id": "2XZmrBRru_A0" }, "source": [ "Let's run FLAML for 30 mins. Here we use Electra's [small model](https://huggingface.co/google/electra-small-discriminator) for the tuning. We set gpu_per_trial to 1, and n_concurrent_trials to 1 (the number of trials running at the same time). Make sure gpu_per_trial * n_concurrent_trials does not exceed the GPU number you have. While running you can observe the resource usage (including the GPU) on the right. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "QEvR2bZiTjhG" }, "outputs": [], "source": [ "MAX_ITER=20\n", "automl_settings = {\n", " \"max_iter\": MAX_ITER, # setting the time budget\n", " \"task\": \"seq-classification\", # setting the task as seq-classification\n", " \"fit_kwargs_by_estimator\": {\n", " \"transformer\": {\n", " \"output_dir\": \"data/output/\", # setting the output directory\n", " \"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\n", " }\n", " },\n", " \"gpu_per_trial\": 1, # using 1 GPU for each trial\n", " \"log_file_name\": \"seqclass.log\", # set the file to save the log for HPO\n", " \"log_type\": \"all\", # the log type for trials: \"all\" if logging all the trials, \"better\" if only keeping the better trials\n", " \"use_ray\": False, # If parallel tuning, set \"use_ray\" to {\"local_dir\": \"data/output/\"}\n", " \"n_concurrent_trials\": 1, # How many trials to run at the same time, n_concurrent_trials * gpu_per_trial must not exceed the total number of GPUs\n", " \"keep_search_state\": True, # keeping the search state\n", " # \"fp16\": False # whether to use fp16, this option is True by default. \n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "EXjF65hOTjhG", "outputId": "b7c524a1-3da1-49ae-caf2-9aec208ffc69" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[flaml.automl.logger: 04-12 02:51:07] {1768} INFO - task = seq-classification\n", "[flaml.automl.logger: 04-12 02:51:07] {1775} INFO - Data split method: stratified\n", "[flaml.automl.logger: 04-12 02:51:07] {1778} INFO - Evaluation method: holdout\n", "[flaml.automl.logger: 04-12 02:51:07] {1891} INFO - Minimizing error metric: 1-accuracy\n", "[flaml.automl.logger: 04-12 02:51:07] {2011} INFO - List of ML learners in AutoML Run: ['transformer']\n", "[flaml.automl.logger: 04-12 02:51:07] {2341} INFO - iteration 0, current learner transformer\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.9/dist-packages/flaml/automl/data.py:297: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " X[str_columns] = X[str_columns].astype(\"string\")\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9c7c478356f54c8d915d64dba5fa4f7e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Downloading (…)okenizer_config.json: 0%| | 0.00/29.0 [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "plt.title('Learning Curve')\n", "plt.xlabel('Wall Clock Time (s)')\n", "plt.ylabel('Validation Accuracy')\n", "print(len(valid_loss_history))\n", "plt.scatter(time_history, 1 - np.array(valid_loss_history))\n", "plt.step(time_history, 1 - np.array(best_valid_loss_history), where='post')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "xudzM73mTjhI" }, "source": [ "## 3. Model selection" ] }, { "cell_type": "markdown", "metadata": { "id": "A3gC3u_E4cO1" }, "source": [ "Given a dataset, which language model should you use for the fine tuning? It appears this is a simple question: just choose the best model according to the benchmarks such as [GLUE](https://gluebenchmark.com/leaderboard). However, we will see that under the resource constraints, the model selection is non trivial. \n", "\n", "In this example, we will tune the [spooky-author-identification](https://www.kaggle.com/competitions/spooky-author-identification/data?select=train.zip) dataset from kaggle. You can download the dataset from the [here](https://drive.google.com/file/d/1Jk-_Vg_SxOUDfFVzF7S85oBasY8fFvOY/view?usp=sharing) and upload it to Colab. The following command also downloads the file. We run FLAML for 30 mins using bert." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Bty5Qz3x_OzJ", "outputId": "8a135114-7367-40a3-a383-ebb891e1f019" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Downloading...\n", "From: https://drive.google.com/uc?id=1Jk-_Vg_SxOUDfFVzF7S85oBasY8fFvOY\n", "To: /content/spooky-author-identification.csv\n", "\r", " 0% 0.00/3.30M [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from flaml.data import get_output_from_log\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "axs = []\n", "for each_file_name in ['bert', 'roberta', 'ms']:\n", " time_history, best_valid_loss_history, valid_loss_history, config_history, metric_history = \\\n", " get_output_from_log(filename='spooky_' + each_file_name + '.log', time_budget=4000)\n", " print(len(valid_loss_history))\n", " axs.append(plt.scatter(time_history, 1 - np.array(valid_loss_history)))\n", " plt.step(time_history, 1 - np.array(best_valid_loss_history), where='post')\n", "\n", "plt.legend(handles=axs, labels=['bert', 'roberta', 'ms'])\n", "plt.ylim([0.6, 0.9])\n", "plt.grid()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "lT7IwNCoTjhJ" }, "source": [ "## 4. Other Tasks" ] }, { "cell_type": "markdown", "metadata": { "id": "Fzkr77iATjhJ" }, "source": [ "Besides sequence classification, FLAML currently also supports four other tasks (more tasks are to be supported, which can be found on [FLAML's documentation website](https://microsoft.github.io/FLAML/docs/Examples/AutoML-NLP)):\n", "\n", "- sequence regression: predicting a float number from the input sequence, e.g., predicting the rating of a hotel review based on the text content;\n", "- token classification: predicting the label of each token in a sequence, e.g., named entity recognition;\n", "- multiple choice: predicting the best second half of a sentence that comes next to the first part of a sentence based on common sensen reasoning. An example is seen below;\n", "- (abstractive) summarization: generating the textual summarization of an input paragraph;\n", "\n", "Here we look into two tasks: multiple choice classification and text summarization. These tasks require significant computational resources, therefore instead of Colab, we run them using 4 NVIDIA V100 GPUs and Ray Tune on our server." ] }, { "cell_type": "markdown", "metadata": { "id": "Y4VgUR5TTjhJ" }, "source": [ "### 4.1 Multiple Choice Example" ] }, { "cell_type": "markdown", "metadata": { "id": "OO8GqaH3TjhJ" }, "source": [ "Multiple choice is a task of predicting the best second half of a sentence that follows the first half based on common sense reasoning. An example of multiple-choice classification problem is:\n", "\n", "On stage, a woman takes a seat at the piano. She\n", "a) sits on a bench as her sister plays with the doll.\n", "b) smiles with someone as the music plays.\n", "c) is in the crowd, watching the dancers.\n", "d) *nervously sets her fingers on the keys*." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "hQ5fX0N3TjhJ", "outputId": "e17bd3ce-9d38-42cf-f3ea-30a0095a34b5" }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "178b92c7a57342ee89b3712e27b80caf", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Downloading builder script: 0%| | 0.00/7.97k [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from flaml.data import get_output_from_log\n", "time_history, best_valid_loss_history, valid_loss_history, config_history, metric_history = \\\n", " get_output_from_log(filename=automl_settings['log_file_name'], time_budget=3000)\n", "for config in config_history:\n", " print(config)\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "plt.title('Learning Curve')\n", "plt.xlabel('Wall Clock Time (s)')\n", "plt.ylabel('Validation Accuracy')\n", "print(len(valid_loss_history))\n", "plt.scatter(time_history, 1 - np.array(valid_loss_history))\n", "plt.step(time_history, 1 - np.array(best_valid_loss_history), where='post')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "664qCdihTjhJ" }, "source": [ "### 4.2 Text Summarization Example" ] }, { "cell_type": "markdown", "metadata": { "id": "kmB4kaF_TjhJ" }, "source": [ "The text summarization task summarizes a long text into a short sentence. For example:\n", "\n", "- Document: Army explosives experts were called out to deal with a suspect package at the offices on the Newtownards Road on Friday night. Roads were sealed off and traffic diverted as a controlled explosion was carried out. The premises, used by East Belfast MP Naomi Long, have been targeted a number of times. Most recently, petrol bomb attacks were carried out on the offices on consecutive nights in April and May. The attacks began following a Belfast City Council vote in December 2012 restricting the flying of the union flag at the City Hall. Condemning the latest hoax, Alliance MLA Chris Lyttle said: \"It is a serious incident for the local area, it causes serious disruption, it puts people's lives at risk, it can prevent emergency services reaching the area. \"Ultimately we need people with information to share that with the police in order for them to do their job and bring these people to justice.\n", "\n", "- Summary: A suspicious package left outside an Alliance Party office in east Belfast has been declared a hoax.\n", "\n", "In this example, we use FLAML to perform *abstractive summarization* using the t5-small language model, i.e., the summary is generated word-by-word. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "amlQnvcxTjhK", "outputId": "e9c0c7fc-25af-4f71-f10d-2ad49bbdf0f7" }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a8a74fbdcfb0446bbd3bed5ff20e019a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Downloading builder script: 0%| | 0.00/5.76k [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "\n", "from flaml.data import get_output_from_log\n", "time_history, best_valid_loss_history, valid_loss_history, config_history, metric_history = \\\n", " get_output_from_log(filename=automl_settings['log_file_name'], time_budget=3000)\n", "for config in config_history:\n", " print(config)\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "plt.title('Learning Curve')\n", "plt.xlabel('Wall Clock Time (s)')\n", "plt.ylabel('Rouge 1')\n", "print(len(valid_loss_history))\n", "plt.scatter(time_history, 1 - np.array(valid_loss_history))\n", "plt.step(time_history, 1 - np.array(best_valid_loss_history), where='post')\n", "plt.show()" ] } ], "metadata": { "accelerator": "GPU", "colab": { "provenance": [] }, "gpuClass": "standard", "interpreter": { "hash": "e9d36fc5b7c3dd4177ff1b60184dd696c0acc18150a44682abca4d769811bd46" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.0" } }, "nbformat": 4, "nbformat_minor": 0 }