{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "accelerator": "GPU" }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "4Pjmz-RORV8E" }, "source": [ "# Train without labels\n", "\n", "Almost all data available is unlabeled. Labeled data takes effort to manually review and/or takes time to collect. Zero-shot classification takes existing large language models and runs a similarity comparison between candidate text and a list of labels. This has been shown to perform surprisingly well.\n", "\n", "The problem with zero-shot classifiers is that they need to have a large number of parameters (400M+) to perform well against general tasks, which comes with sizable hardware requirements.\n", "\n", "This notebook explores using zero-shot classifiers to build training data for smaller models. A simple form of [knowledge distillation](https://en.wikipedia.org/wiki/Knowledge_distillation). " ] }, { "cell_type": "markdown", "metadata": { "id": "Dk31rbYjSTYm" }, "source": [ "# Install dependencies\n", "\n", "Install `txtai` and all dependencies." ] }, { "cell_type": "code", "metadata": { "id": "XMQuuun2R06J" }, "source": [ "%%capture\n", "!pip install git+https://github.com/neuml/txtai#egg=txtai[pipeline-train] datasets pandas" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "3PUe1OW8IZR5" }, "source": [ "# Apply zero-shot classifier to unlabeled text\n", "\n", "The following section takes a small 1000 record random sample of the sst2 dataset and applies a zero-shot classifer to the text. The labels are ignored. This dataset was chosen only to be able to evaluate the accuracy at then end. " ] }, { "cell_type": "code", "metadata": { "id": "GlrOnS4cmkih" }, "source": [ "import random\n", "\n", "from datasets import load_dataset\n", "\n", "from txtai.pipeline import Labels\n", "\n", "def batch(texts, size):\n", " return [texts[x : x + size] for x in range(0, len(texts), size)]\n", "\n", "# Set random seed for repeatable sampling\n", "random.seed(42)\n", "\n", "ds = load_dataset(\"glue\", \"sst2\")\n", "\n", "sentences = random.sample(ds[\"train\"][\"sentence\"], 1000)\n", "\n", "# Load a zero shot classifier - txtai provides this through the Labels pipeline\n", "labels = Labels(\"microsoft/deberta-large-mnli\")\n", "\n", "train = []\n", "\n", "# Zero-shot prediction using [\"negative\", \"positive\"] labels\n", "for chunk in batch(sentences, 32):\n", " train.extend([{\"text\": chunk[x], \"label\": label[0][0]} for x, label in enumerate(labels(chunk, [\"negative\", \"positive\"]))])" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "TLsZmRpHJGav" }, "source": [ "Next, we'll use the training set we just built to train a smaller Electra model." ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 214 }, "id": "nAt42TIHnfTN", "outputId": "7080b21d-ecf4-459a-c818-11c748e28bb7" }, "source": [ "from txtai.pipeline import HFTrainer\n", "\n", "trainer = HFTrainer()\n", "model, tokenizer = trainer(\"google/electra-base-discriminator\", train, num_train_epochs=5)" ], "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "Some weights of the model checkpoint at google/electra-base-discriminator were not used when initializing ElectraForSequenceClassification: ['discriminator_predictions.dense.bias', 'discriminator_predictions.dense.weight', 'discriminator_predictions.dense_prediction.weight', 'discriminator_predictions.dense_prediction.bias']\n", "- This IS expected if you are initializing ElectraForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n", "- This IS NOT expected if you are initializing ElectraForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n", "Some weights of ElectraForSequenceClassification were not initialized from the model checkpoint at google/electra-base-discriminator and are newly initialized: ['classifier.dense.bias', 'classifier.out_proj.weight', 'classifier.out_proj.bias', 'classifier.dense.weight']\n", "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n" ] }, { "output_type": "display_data", "data": { "text/html": [ "\n", "
| Step | \n", "Training Loss | \n", "
|---|---|
| 500 | \n", "0.282800 | \n", "
"
],
"text/plain": [
"