You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openai-cookbook/examples/Visualizing_embeddings_in_3...

266 lines
282 KiB
Plaintext

2 years ago
{
"cells": [
{
"cell_type": "markdown",
"id": "983ef639-fbf4-4912-b593-9cf08aeb11cd",
"metadata": {},
"source": [
"# Visualizing embeddings in 3D"
2 years ago
]
},
{
"cell_type": "markdown",
"id": "9c9ea9a8-675d-4e3a-a8f7-6f4563df84ad",
"metadata": {},
"source": [
"The example uses [PCA](https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html) to reduce the dimensionality fo the embeddings from 2048 to 3. Then we can visualize the data points in a 3D plot. The small dataset `dbpedia_samples.jsonl` is curated by randomly sampling 200 samples from [DBpedia validation dataset](https://www.kaggle.com/danofer/dbpedia-classes?select=DBPEDIA_val.csv)."
]
},
{
"cell_type": "markdown",
"id": "8df5f2c3-ddbb-4cc4-9205-4c0af1670562",
"metadata": {},
"source": [
"### 1. Load the dataset and query embeddings"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "133dfc2a-9dbd-4a5a-96fa-477272f7af5a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Categories of DBpedia samples: Artist 21\n",
"Film 19\n",
"Plant 19\n",
"OfficeHolder 18\n",
"Company 17\n",
"NaturalPlace 16\n",
"Athlete 16\n",
"Village 12\n",
"WrittenWork 11\n",
"Building 11\n",
"Album 11\n",
"Animal 11\n",
"EducationalInstitution 10\n",
"MeanOfTransportation 8\n",
"Name: category, dtype: int64\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>text</th>\n",
" <th>category</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Morada Limited is a textile company based in ...</td>\n",
" <td>Company</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>The Armenian Mirror-Spectator is a newspaper ...</td>\n",
" <td>WrittenWork</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Mt. Kinka (金華山 Kinka-zan) also known as Kinka...</td>\n",
" <td>NaturalPlace</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Planning the Play of a Bridge Hand is a book ...</td>\n",
" <td>WrittenWork</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Wang Yuanping (born 8 December 1976) is a ret...</td>\n",
" <td>Athlete</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" text category\n",
"0 Morada Limited is a textile company based in ... Company\n",
"1 The Armenian Mirror-Spectator is a newspaper ... WrittenWork\n",
"2 Mt. Kinka (金華山 Kinka-zan) also known as Kinka... NaturalPlace\n",
"3 Planning the Play of a Bridge Hand is a book ... WrittenWork\n",
"4 Wang Yuanping (born 8 December 1976) is a ret... Athlete"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"samples = pd.read_json(\"data/dbpedia_samples.jsonl\", lines=True)\n",
2 years ago
"categories = sorted(samples[\"category\"].unique())\n",
"print(\"Categories of DBpedia samples:\", samples[\"category\"].value_counts())\n",
"samples.head()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "19874e3e-a216-48cc-a27b-acb73854d832",
"metadata": {},
"outputs": [],
"source": [
"from openai.embeddings_utils import get_embeddings\n",
"# NOTE: The following code will send a query of batch size 200 to /embeddings, cost about $0.2\n",
"matrix = get_embeddings(samples[\"text\"].to_list(), engine=\"text-similarity-babbage-001\")"
]
},
{
"cell_type": "markdown",
"id": "d410c268-d8a7-4979-887c-45b1d382dda9",
"metadata": {},
"source": [
"### 2. Reduce the embedding dimensionality"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f5410068-f3da-490c-8576-48e84a8728de",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.decomposition import PCA\n",
"pca = PCA(n_components=3)\n",
"vis_dims = pca.fit_transform(matrix)\n",
"samples[\"embed_vis\"] = vis_dims.tolist()"
]
},
{
"cell_type": "markdown",
"id": "b6565f57-59c6-4d36-a094-3cbbd9ddeb4c",
"metadata": {},
"source": [
"### 3. Plot the embeddings of lower dimensionality"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b17caad3-f0de-4115-83eb-55434a132acc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.legend.Legend at 0x117919ac0>"
2 years ago
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d15cdcc507224a81a18a0c1f18292590",
2 years ago
"version_major": 2,
"version_minor": 0
},
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA+gAAAH0CAYAAACuKActAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzdeXycdbn//9c9+5LJZLIvTZum+75vKUuRrYocqxWrgAoi5xwQPIgelaMiesQFxR8iit/jqVZcAI9HkAOKYmVvaQs0aZq0aZqmTZp9mUwyM5n9/v0R5iZJkzTLTJK21/Px4KEkM/d9TzbmfV+fz3UpqqqqCCGEEEIIIYQQYkrppvoChBBCCCGEEEIIIQFdCCGEEEIIIYSYFiSgCyGEEEIIIYQQ04AEdCGEEEIIIYQQYhqQgC6EEEIIIYQQQkwDEtCFEEIIIYQQQohpQAK6EEIIIYQQQggxDUhAF0IIIYQQQgghpgEJ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/Xy5NPPsltt93GNddcw65du7TPvfTSSyiKwu7du1m7di02m42SkhKqqqq0xwxeen7TTTexbds2vv3tb5OTk0NaWhrf/OY3iUQi/Pu//zvp6enMmDGDX/7ylwOu40tf+hLz58/HZrNRXFzM1772Ne1vuRDTgTSJE0IIIcYgGo0OaAQ3VDCPRqMcPXqUpqYm5s6dSyQSwe12U1tbi6IopKWl4XK5cLlc2O32AccYbcU9Htjj+jepC4VCA/a49286JzPYhbiwdfnCVNZ7afWE0Ck
2 years ago
"text/html": [
"\n",
" <div style=\"display: inline-block;\">\n",
" <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
" Figure\n",
" </div>\n",
" <img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+gAAAH0CAYAAACuKActAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzdeXycdbn//9c9+5LJZLIvTZum+75vKUuRrYocqxWrgAoi5xwQPIgelaMiesQFxR8iit/jqVZcAI9HkAOKYmVvaQs0aZq0aZqmTZp9mUwyM5n9/v0R5iZJkzTLTJK21/Px4KEkM/d9TzbmfV+fz3UpqqqqCCGEEEIIIYQQYkrppvoChBBCCCGEEEIIIQFdCCGEEEIIIYSYFiSgCyGEEEIIIYQQ04AEdCGEEEIIIYQQYhqQgC6EEEIIIYQQQkwDEtCFEEIIIYQQQohpQAK6EEIIIYQQQggxDUhAF0IIIYQQQgghpgEJ6EIIIYQQQgghxDQgAV0IIYQQQgghhJgGJKALIYQQQgghhBDTgAR0IYQQQgghhBBiGpCALoQQQgghhBBCTAMS0IUQQgghhBBCiGlAAroQQgghhBBCCDENSEAXQgghhBBCCCGmAQnoQgghhBBCCCHENCABXQghhBBCCCGEmAYkoAshhBBCCCGEENOABHQhhBBCCCGEEGIakIAuhBBCCCGEEEJMAxLQh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e9Ho911xzzVRfihDTlgR0IYQQQgghRNLt3LmTO++8k1deeYXGxsapvhwhpiUJ6EIIIYQQQlwgatq8fPbxgyy/76+s/dYLfOvZSjz+5He59nq9PPnkk9x2221cc8017Nq166zPefrpp5k3bx4Wi4Wrr76a+vp67XM33XQT27ZtG/D4u+66iy1btmj/vmXLFu68807uuusuXC4XOTk5/PznP8fn83HzzTfjcDiYO3cuf/nLXxL0KoWYOAnoQgghhBBCXABq23184JHXea68ie5AhHZviF++Xst1/28PvaFoUs/9+9//noULF7JgwQJuvPFGfvGLX6Cq6rCP9/v93H///Tz22GO8/vrrdHV18dGPfnTM5/3Vr35FZmYm+/fv58477+S2227juuuuo6SkhLfffpurrrqKj3/84/j9/om8PCESRgK6EEIIIYQQF4BH/lFNbzhKNPZuMI6qcKzFy1MHG5J67p07d3LjjTcCsHXrVjweDy+//PKwjw+HwzzyyCNs2rSJNWvW8Ktf/Yo9e/awf//+MZ13xYoVfPWrX2XevHncc889WCwWMjMzufXWW5k3bx733nsvHR0dHDp0aEKvT4hEkYAuhBBCCCHEBeDlY20DwnmcToHXj7cn7bxVVVXs37+fj33sYwAYDAZ27NjBzp07h32OwWBg3bp12r8vXLiQtLQ0jhw5MqZzL1++XPv/er2ejIwMli1bpn0sJycHgNbW1jEdV4hkMUz1BQghhBBCCCGSz2YyAKEzPq4oClaTPmnn3blzJ5FIhPz8fO1jqqpiNpt55JFHxnVMnU53xhL5cPjMvfRGo3HAvyuKMuBjiqIAEIvFxnUdQiSaVNCFEEIIIYS4AHxwVQE65cyPR2Mq167IP/MTCRCJRHjsscd48MEHKS0t1f4pKysjPz+fxx9/fNjnvfnmm9q/V1VV0dXVxaJFiwDIysqiqalpwHNKS0uT8hqEmEwS0IUQQgghhLgA/MulxayYkQaAXqegfyet37BhJpfMy0zKOZ999lncbje33HILS5cuHfDP9u3bh13mbjQaufPOO9m3bx9vvfUWN910Exs3bmT9+vUAvOc97+HNN9/kscceo7q6mq9//escPnw4Ka9BiMkkAV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cLlc2O32AccYbcU9Htjj+je
2 years ago
" </div>\n",
" "
],
"text/plain": [
"Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%matplotlib widget\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"fig = plt.figure(figsize=(10, 5))\n",
"ax = fig.add_subplot(projection='3d')\n",
"cmap = plt.get_cmap(\"tab20\")\n",
"\n",
"# Plot each sample category individually such that we can set label name.\n",
"for i, cat in enumerate(categories):\n",
" sub_matrix = np.array(samples[samples[\"category\"] == cat][\"embed_vis\"].to_list())\n",
" x=sub_matrix[:, 0]\n",
" y=sub_matrix[:, 1]\n",
" z=sub_matrix[:, 2]\n",
" colors = [cmap(i/len(categories))] * len(sub_matrix)\n",
" ax.scatter(x, y, zs=z, zdir='z', c=colors, label=cat)\n",
"\n",
"ax.set_xlabel('x')\n",
"ax.set_ylabel('y')\n",
"ax.set_zlabel('z')\n",
"ax.legend(bbox_to_anchor=(1.1, 1))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.9 ('openai')",
2 years ago
"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.9.9"
},
"vscode": {
"interpreter": {
"hash": "365536dcbde60510dc9073d6b991cd35db2d9bac356a11f5b64279a5e6708b97"
}
2 years ago
}
},
"nbformat": 4,
"nbformat_minor": 5
}