openai-cookbook/examples/Zero-shot_classification_with_embeddings.ipynb

231 lines
101 KiB
Plaintext
Raw Normal View History

2022-03-11 02:08:53 +00:00
{
"cells": [
{
"attachments": {},
2022-03-11 02:08:53 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
"## Zero-shot classification with embeddings\n",
"\n",
"In this notebook we will classify the sentiment of reviews using embeddings and zero labeled data! The dataset is created in the [Get_embeddings_from_dataset Notebook](Get_embeddings_from_dataset.ipynb).\n",
2022-03-11 02:08:53 +00:00
"\n",
"We'll define positive sentiment to be 4- and 5-star reviews, and negative sentiment to be 1- and 2-star reviews. 3-star reviews are considered neutral and we won't use them for this example.\n",
2022-03-11 02:08:53 +00:00
"\n",
"We will perform zero-shot classification by embedding descriptions of each class and then comparing new samples to those class embeddings."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from ast import literal_eval\n",
2022-03-11 02:08:53 +00:00
"\n",
"from sklearn.metrics import classification_report\n",
"\n",
2024-01-25 17:59:05 +00:00
"EMBEDDING_MODEL = \"text-embedding-3-small\"\n",
"\n",
"datafile_path = \"data/fine_food_reviews_with_embeddings_1k.csv\"\n",
"\n",
"df = pd.read_csv(datafile_path)\n",
"df[\"embedding\"] = df.embedding.apply(literal_eval).apply(np.array)\n",
2022-03-11 02:08:53 +00:00
"\n",
"# convert 5-star rating to binary sentiment\n",
"df = df[df.Score != 3]\n",
"df[\"sentiment\"] = df.Score.replace({1: \"negative\", 2: \"negative\", 4: \"positive\", 5: \"positive\"})\n"
2022-03-11 02:08:53 +00:00
]
},
{
"attachments": {},
2022-03-11 02:08:53 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
"### Zero-Shot Classification\n",
"To perform zero shot classification, we want to predict labels for our samples without any training. To do this, we can simply embed short descriptions of each label, such as positive and negative, and then compare the cosine distance between embeddings of samples and label descriptions. \n",
"\n",
"The highest similarity label to the sample input is the predicted label. We can also define a prediction score to be the difference between the cosine distance to the positive and to the negative label. This score can be used for plotting a precision-recall curve, which can be used to select a different tradeoff between precision and recall, by selecting a different threshold."
]
},
{
"cell_type": "code",
"execution_count": 2,
2022-03-11 02:08:53 +00:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" precision recall f1-score support\n",
"\n",
2024-01-25 17:59:05 +00:00
" negative 0.54 0.92 0.68 136\n",
" positive 0.98 0.87 0.92 789\n",
2022-03-11 02:08:53 +00:00
"\n",
2024-01-25 17:59:05 +00:00
" accuracy 0.87 925\n",
" macro avg 0.76 0.89 0.80 925\n",
"weighted avg 0.92 0.87 0.89 925\n",
2022-03-11 02:08:53 +00:00
"\n"
]
},
{
"data": {
2024-01-25 17:59:05 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAcAAAAHHCAYAAAAoIIjLAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8g+/7EAAAACXBIWXMAAA9hAAAPYQGoP6dpAABfC0lEQVR4nO3dd1QUVxsG8GeXsvSiNFEUC9ZYMSoYSwyGqMESC1FjwW5skSQqsWCJLUajMZbE2GM+NEZjRxF7iUaJFcUGQlRsCEiRsnu/PwwTV4q7uEtxn985e2Tv3Jn7zmXdl5m5d0YmhBAgIiIyMPLiDoCIiKg4MAESEZFBYgIkIiKDxARIREQGiQmQiIgMEhMgEREZJCZAIiIySEyARERkkJgAiYjIIDEBkt60bt0arVu3Lu4wSrSpU6dCJpNptU5MTAxkMhnWrFmjn6BKuf79+8Pd3V2tTCaTYerUqcUSD5VcTIAG6K+//sLIkSNRp04dWFpaomLFiujRoweuXbtW3KHp3aFDhyCTyaSXiYkJqlSpgr59++LWrVvFHV6p8GL/yWQy2NjYoFWrVti1a1dxh0akFePiDoCK3ty5c3H8+HF0794d9erVQ3x8PH744Qc0atQIf/75J956663iDlHvRo8ejbfffhtZWVmIiIjATz/9hF27duHixYtw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CxYsyLMtNzc3jeNNTExEq1atYGNjg+nTp6Nq1aowMzNDREQExo8fr/HR8uvK70gwv4E
2022-03-11 02:08:53 +00:00
"text/plain": [
2024-01-25 17:59:05 +00:00
"<Figure size 640x480 with 1 Axes>"
2022-03-11 02:08:53 +00:00
]
},
2024-01-25 17:59:05 +00:00
"metadata": {},
2022-03-11 02:08:53 +00:00
"output_type": "display_data"
}
],
"source": [
"from utils.embeddings_utils import cosine_similarity, get_embedding\n",
2022-03-11 02:08:53 +00:00
"from sklearn.metrics import PrecisionRecallDisplay\n",
"\n",
"def evaluate_embeddings_approach(\n",
" labels = ['negative', 'positive'],\n",
" model = EMBEDDING_MODEL,\n",
2022-03-11 02:08:53 +00:00
"):\n",
" label_embeddings = [get_embedding(label, model=model) for label in labels]\n",
2022-03-11 02:08:53 +00:00
"\n",
" def label_score(review_embedding, label_embeddings):\n",
" return cosine_similarity(review_embedding, label_embeddings[1]) - cosine_similarity(review_embedding, label_embeddings[0])\n",
"\n",
" probas = df[\"embedding\"].apply(lambda x: label_score(x, label_embeddings))\n",
2022-03-11 02:08:53 +00:00
" preds = probas.apply(lambda x: 'positive' if x>0 else 'negative')\n",
"\n",
" report = classification_report(df.sentiment, preds)\n",
" print(report)\n",
"\n",
" display = PrecisionRecallDisplay.from_predictions(df.sentiment, probas, pos_label='positive')\n",
" _ = display.ax_.set_title(\"2-class Precision-Recall curve\")\n",
"\n",
"evaluate_embeddings_approach(labels=['negative', 'positive'], model=EMBEDDING_MODEL)\n"
2022-03-11 02:08:53 +00:00
]
},
{
"attachments": {},
2022-03-11 02:08:53 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that this classifier already performs extremely well. We used similarity embeddings, and the simplest possible label name. Let's try to improve on this by using more descriptive label names, and search embeddings."
]
},
{
"cell_type": "code",
2024-01-25 17:59:05 +00:00
"execution_count": 4,
2022-03-11 02:08:53 +00:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" precision recall f1-score support\n",
"\n",
2024-01-25 17:59:05 +00:00
" negative 0.76 0.96 0.85 136\n",
" positive 0.99 0.95 0.97 789\n",
2022-03-11 02:08:53 +00:00
"\n",
2024-01-25 17:59:05 +00:00
" accuracy 0.95 925\n",
" macro avg 0.88 0.96 0.91 925\n",
"weighted avg 0.96 0.95 0.95 925\n",
2022-03-11 02:08:53 +00:00
"\n"
]
},
{
"data": {
2024-01-25 17:59:05 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAcAAAAHHCAYAAAAoIIjLAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8g+/7EAAAACXBIWXMAAA9hAAAPYQGoP6dpAABcFUlEQVR4nO3dd1QU19sH8O/Sll4UaYqCijUqilHBWGIwRA2WWFCJggU1sZNEJUaxxBYTozGWX4wtxgSN0dgxiNhblGADsaFgwYaAgNL2vn/4MnGluIu7IO73c86ew965M/PMZdmHuXPvjEwIIUBERKRj9Mo7ACIiovLABEhERDqJCZCIiHQSEyAREekkJkAiItJJTIBERKSTmACJiEgnMQESEZFOYgIkIiKdxARIWtO+fXu0b9++vMN4rU2bNg0ymUytda5fvw6ZTIY1a9ZoJ6gKLjAwEC4uLkplMpkM06ZNK5d46PXFBKiD/vnnH4waNQoNGzaEmZkZqlevjj59+uDSpUvlHZrW7d+/HzKZTHoZGhqiZs2aGDhwIK5du1be4VUIz7efTCaDpaUl2rVrh507d5Z3aERqMSjvAKjszZs3D0eOHEHv3r3RuHFjJCcn48cff0SzZs1w/PhxvPXWW+UdotaNGTMGb7/9NnJzcxEdHY2ffvoJO3fuxLlz5+Dk5FRmcXz11VeYNGmSWuvUqFEDT548gaGhoZaiermOHTti4MCBEELgxo0bWLZsGXx9fbF79274+PiUW1xE6mAC1EHBwcH47bffYGRkJJX5+fmhUaNGmDt3Ln799ddyjK5stGnTBr169QIADBo0CHXq1MGYMWOwdu1ahISEFLlOZmYmz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R7169ZTaqqC7uaCtVP08mZiYSD8/fvwYDx48QJs2bZCVlYWLFy+qFHdJ/vzzT9ja2mL06NGFlqk7VeV5n3zySaFt9e7dG7t27UJGRoZUvmHDBlStWlXqao+IiEBqair69eun1H76+vpo2bJlkZcOSD1MgDouOTkZXbp0gZWVFTZt2iR1vQDPurWSk5OlV0pKCoBn3YV6enpo0KCB2vtbu3YtGjduLF23q1KlCnbu3Im0tDSpzqeffoo6deqgU6dOqFatGgYPHozw8HCl7cyYMQOpqamoU6cOGjVqhC+++AJnz55VOY6pU6ciIiIC+/btw9mzZ3H79m0MGDCgUD1XV1el95cvX4YQAm5ubqhSpYrSKy4uDvfu3QPwX5equiNq/fz80Lp1awwdOhT29vbo27cvNm7cWGIyvHHjBgCgbt26hZbVr18fDx48QGZmplJ59erVld4XdF0+evQIAJCSkqL0u3/+9wMA3bp1Q0REBHbu3CldV8zKylJKYJcvX8aFCxcKtVOdOnUAQKmtVPk8XbhwAT169ICVlRUsLS1RpUoVaSDOi/GVxtWrV1G3bl21ByWVxMDAANWqVStU7ufnhydPnmDbtm0AgIyMDOzatQu9e/eWkm3BP3cdOnQo1IZ///231H5UehwFqsPS0tLQqVMnpKam4tChQ4WG/48dOxZr166V3rdr167QxX91/PrrrwgMDET37t3xxRdfwM7ODvr6+pgzZ47S4Ak7OzvExMRgz5492L17N3bv3o3Vq1dj4MCBUjxt27bF1atXsXXrVvz999/4+eef8f3332P58uUYOnToS2Np1KgRvL29X1rv+bMO4NmZq0wmw+7du5X+WShgbm7+0m2+bH8HDx5EVFQUdu7cifDwcGzYsAEdOnTA33//XeQ+S6O47QghAAAfffQRDhw4IJUHBAQoDfCoVq2a1H6dO3eGra0tRo0ahXfffRcfffQRgGdt1ahRIyxYsKDIfTk7O6scb2pqKtq1awdLS0vMmDEDtWrVgrGxMaKjozFx4kSVz5ZfVXFngsU
2022-03-11 02:08:53 +00:00
"text/plain": [
2024-01-25 17:59:05 +00:00
"<Figure size 640x480 with 1 Axes>"
2022-03-11 02:08:53 +00:00
]
},
2024-01-25 17:59:05 +00:00
"metadata": {},
2022-03-11 02:08:53 +00:00
"output_type": "display_data"
}
],
"source": [
"evaluate_embeddings_approach(labels=['An Amazon review with a negative sentiment.', 'An Amazon review with a positive sentiment.'])\n"
2022-03-11 02:08:53 +00:00
]
},
{
"attachments": {},
2022-03-11 02:08:53 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
"Using the search embeddings and descriptive names leads to an additional improvement in performance."
]
},
{
"cell_type": "code",
2024-01-25 17:59:05 +00:00
"execution_count": 5,
2022-03-11 02:08:53 +00:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" precision recall f1-score support\n",
"\n",
2024-01-25 17:59:05 +00:00
" negative 0.76 0.96 0.85 136\n",
" positive 0.99 0.95 0.97 789\n",
2022-03-11 02:08:53 +00:00
"\n",
2024-01-25 17:59:05 +00:00
" accuracy 0.95 925\n",
" macro avg 0.88 0.96 0.91 925\n",
"weighted avg 0.96 0.95 0.95 925\n",
2022-03-11 02:08:53 +00:00
"\n"
]
},
{
"data": {
2024-01-25 17:59:05 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAcAAAAHHCAYAAAAoIIjLAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8g+/7EAAAACXBIWXMAAA9hAAAPYQGoP6dpAABcFUlEQVR4nO3dd1QU19sH8O/Sll4UaYqCijUqilHBWGIwRA2WWFCJggU1sZNEJUaxxBYTozGWX4wtxgSN0dgxiNhblGADsaFgwYaAgNL2vn/4MnGluIu7IO73c86ew965M/PMZdmHuXPvjEwIIUBERKRj9Mo7ACIiovLABEhERDqJCZCIiHQSEyAREekkJkAiItJJTIBERKSTmACJiEgnMQESEZFOYgIkIiKdxARIWtO+fXu0b9++vMN4rU2bNg0ymUytda5fvw6ZTIY1a9ZoJ6gKLjAwEC4uLkplMpkM06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+pgzZ47S4Ak7OzvExMRgz5492L17N3bv3o3Vq1dj4MCBUjxt27bF1atXsXXrVvz999/4+eef8f3332P58uUYOnToS2Np1KgRvL29X1rv+bMO4NmZq0wmw+7du5X+WShgbm7+0m2+bH8HDx5EVFQUdu7cifDwcGzYsAEdOnTA33//XeQ+S6O47QghAAAfffQRDhw4IJUHBAQoDfCoVq2a1H6dO3eGra0tRo0ahXfffRcfffQRgGdt1ahRIyxYsKDIfTk7O6scb2pqKtq1awdLS0vMmDEDtWrVgrGxMaKjozFx4kSVz5ZfVXFngsU
2022-03-11 02:08:53 +00:00
"text/plain": [
2024-01-25 17:59:05 +00:00
"<Figure size 640x480 with 1 Axes>"
2022-03-11 02:08:53 +00:00
]
},
2024-01-25 17:59:05 +00:00
"metadata": {},
2022-03-11 02:08:53 +00:00
"output_type": "display_data"
}
],
"source": [
"evaluate_embeddings_approach(labels=['An Amazon review with a negative sentiment.', 'An Amazon review with a positive sentiment.'])\n"
2022-03-11 02:08:53 +00:00
]
},
{
"attachments": {},
2022-03-11 02:08:53 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
"As shown above, zero-shot classification with embeddings can lead to great results, especially when the labels are more descriptive than just simple words."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "openai",
"language": "python",
"name": "python3"
2022-03-11 02:08:53 +00:00
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
2024-01-25 17:59:05 +00:00
"version": "3.11.5"
2022-03-11 02:08:53 +00:00
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "365536dcbde60510dc9073d6b991cd35db2d9bac356a11f5b64279a5e6708b97"
}
}
2022-03-11 02:08:53 +00:00
},
"nbformat": 4,
"nbformat_minor": 2
}