openai-cookbook/examples/Visualizing_embeddings_in_3D.ipynb

267 lines
294 KiB
Plaintext
Raw Normal View History

2022-03-11 02:08:53 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "983ef639-fbf4-4912-b593-9cf08aeb11cd",
"metadata": {},
"source": [
"# Visualizing embeddings in 3D"
2022-03-11 02:08:53 +00:00
]
},
{
"attachments": {},
2022-03-11 02:08:53 +00:00
"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 1536 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)."
2022-03-11 02:08:53 +00:00
]
},
{
"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",
2022-03-11 02:08:53 +00:00
"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\n",
"matrix = get_embeddings(samples[\"text\"].to_list(), engine=\"text-embedding-ada-002\")"
2022-03-11 02:08:53 +00:00
]
},
{
"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 0x1622180a0>"
2022-03-11 02:08:53 +00:00
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "771220ea662943a0b06fd6ab908475a1",
2022-03-11 02:08:53 +00:00
"version_major": 2,
"version_minor": 0
},
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA+gAAAH0CAYAAACuKActAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzde3ycZZ3//9c955nMZCbnpmna9JieD/REUxBQFqrIiotsVVAryu6iwBdd18MqCCqucnAR2cXfsoXK6gLqLuiCgoiUU4FyaNImbZMmTZM0TXOcHOaQOd33748wN5mcZ3Ka0s/z8ejj0U7mnvuaSTKd9319rs+laJqmIYQQQgghhBBCiFllmO0BCCGEEEIIIYQQQgK6EEIIIYQQQgiRFiSgCyGEEEIIIYQQaUAC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k5EzTyIRIHxLQhRBCiCTFS9oHrx2fyq3TFEUZFrbjDeXiJfFDA3tfX58+wx4KhSSwCyHSis/n4/HHH+f666/nsssuY8+ePfrX9u7di6IoPP/882zatAmHw0FZWRnV1dX6fYaWnu/atYsrrriCH/7whxQUFODxePje975HNBrln/7pn8jOzmbevHk8/PDDCeP4xje+wbJly3A4HCxatIhbbrlFfy8XIh1IQBdCCCGSEIvF9AA8mZL28YwXrkcK7PELB36/Xw/sfr9fArsQQtftj7DvqJcn32jl9/tbeaeuh/5wbNrP++tf/5rly5dTWlrKNddcw0MPPTTsPenb3/4299xzD2+99RYmk4lrr712zMf8y1/+wqlTp3jppZf4yU9
2022-03-11 02:08:53 +00:00
"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/YYfK9AAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzde3ycZZ3//9c955nMZCbnpmna9JieD/REUxBQFqrIiotsVVAryu6iwBdd18MqCCqucnAR2cXfsoXK6gLqLuiCgoiUU4FyaNImbZMmTZM0TXOcHOaQOd33748wN5mcZ3Ka0s/z8ejj0U7mnvuaSTKd9319rs+laJqmIYQQQgghhBBCiFllmO0BCCGEEEIIIYQQQgK6EEIIIYQQQgiRFiSgCyGEEEIIIYQQaUACuhBCCCGEEEIIkQYkoAshhBBCCCGEEGlAAroQQgghhBBCCJEGJKALIYQQQgghhBBpQAK6EEIIIYQQQgiRBiSgCyGEEEIIIYQQaUACuhBCCCGEEEIIkQYkoAshhBBCCCGEEGlAAroQQgghhBBCCJEGJKALIYQQQgghhBBpQAK6EEIIIYQQQgiRBiSgCyGEEE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Jly3A4HCxatIhbbrlFfy8XIh1IQBdCCCGSEIvF9AA8mZL28YwXrkcK7PELB36/Xw/sfr9fArsQQtftj7DvqJcn32jl9/tbeaeuh/5wbNrP++tf/5rly5dTWlrKNddcw0MPPTTsPenb3/4299xzD2+
2022-03-11 02:08:53 +00:00
" </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')",
2022-03-11 02:08:53 +00:00
"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"
}
2022-03-11 02:08:53 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}