|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 27, |
| 6 | + "id": "67e53669", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "!pip install seaborn" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 1, |
| 16 | + "id": "fccce30e-725a-4c35-9382-b177232de919", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "import sys\n", |
| 21 | + "sys.path.append('..')" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": 128, |
| 27 | + "id": "e4620afc", |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [], |
| 30 | + "source": [ |
| 31 | + "from contexto.guess import Finder\n", |
| 32 | + "import numpy as np\n", |
| 33 | + "import seaborn as sns\n", |
| 34 | + "from sklearn.manifold import TSNE\n", |
| 35 | + "import matplotlib.pyplot as plt\n" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 130, |
| 41 | + "id": "7a99fd91", |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [], |
| 44 | + "source": [ |
| 45 | + "embedding = TSNE(n_components=2, learning_rate='auto', metric=\"cosine\").fit_transform(Finder().embeddings)" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": 281, |
| 51 | + "id": "20bb23f7", |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [], |
| 54 | + "source": [ |
| 55 | + "def plot(scores, save_id=None):\n", |
| 56 | + " is_selected = scores > 0\n", |
| 57 | + "\n", |
| 58 | + " plt.figure(figsize=(15,8))\n", |
| 59 | + "\n", |
| 60 | + " not_selected_embedding = embedding[~is_selected]\n", |
| 61 | + " selected_embedding = embedding[is_selected]\n", |
| 62 | + "\n", |
| 63 | + " top_score = max(scores)\n", |
| 64 | + "\n", |
| 65 | + " top_embedding = embedding[scores == top_score][:1]\n", |
| 66 | + "\n", |
| 67 | + "\n", |
| 68 | + " ax = sns.scatterplot(\n", |
| 69 | + " x=not_selected_embedding[:, 0],\n", |
| 70 | + " y=not_selected_embedding[:, 1],\n", |
| 71 | + " legend=\"\",\n", |
| 72 | + " size=1,\n", |
| 73 | + " hue=0.1,\n", |
| 74 | + " )\n", |
| 75 | + "\n", |
| 76 | + " ax = sns.scatterplot(\n", |
| 77 | + " x=selected_embedding[:, 0],\n", |
| 78 | + " y=selected_embedding[:, 1],\n", |
| 79 | + " size=scores[is_selected],\n", |
| 80 | + " # hue=scores[is_selected],\n", |
| 81 | + " hue=0,\n", |
| 82 | + " legend=\"\",\n", |
| 83 | + " ax=ax\n", |
| 84 | + " )\n", |
| 85 | + "\n", |
| 86 | + " ax = sns.scatterplot(\n", |
| 87 | + " x=top_embedding[:, 0],\n", |
| 88 | + " y=top_embedding[:, 1],\n", |
| 89 | + " ax=ax,\n", |
| 90 | + " )\n", |
| 91 | + "\n", |
| 92 | + " ax = sns.scatterplot(\n", |
| 93 | + " x=top_embedding[:, 0],\n", |
| 94 | + " y=top_embedding[:, 1],\n", |
| 95 | + " ax=ax,\n", |
| 96 | + " )\n", |
| 97 | + "\n", |
| 98 | + " plt.tight_layout()\n", |
| 99 | + "\n", |
| 100 | + "\n", |
| 101 | + " # remove axis\n", |
| 102 | + " ax.set_xticks([])\n", |
| 103 | + " ax.set_yticks([])\n", |
| 104 | + "\n", |
| 105 | + " if save_id is not None:\n", |
| 106 | + " plt.savefig(f'../plots/{save_id:02d}.png')\n", |
| 107 | + " else:\n", |
| 108 | + " plt.show()" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "code", |
| 113 | + "execution_count": 275, |
| 114 | + "id": "8bb22475", |
| 115 | + "metadata": {}, |
| 116 | + "outputs": [], |
| 117 | + "source": [ |
| 118 | + "def score(guesses):\n", |
| 119 | + " finder = Finder()\n", |
| 120 | + " for guess, rank in guesses:\n", |
| 121 | + " finder.add_result(guess, rank)\n", |
| 122 | + " scores = finder.get_score(\n", |
| 123 | + " guesses=guesses,\n", |
| 124 | + " word_to_distances=finder.word_to_distances,\n", |
| 125 | + " min_gap=0.2,\n", |
| 126 | + " num_samples=1000\n", |
| 127 | + " )\n", |
| 128 | + " already_guessed_masked = finder.already_guessed_mask(guesses)\n", |
| 129 | + " scores[already_guessed_masked] = 0\n", |
| 130 | + " best_scores_masked = finder.best_scores(guesses=guesses, word_to_distances=finder.word_to_distances, top=100)\n", |
| 131 | + " scores[~best_scores_masked] = 0\n", |
| 132 | + "\n", |
| 133 | + " top_score = max(scores)\n", |
| 134 | + " closest_ids = list(np.arange(len(finder.words))[scores == top_score])\n", |
| 135 | + " return scores, [finder.words[idx] for idx in closest_ids]\n" |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "code", |
| 140 | + "execution_count": 276, |
| 141 | + "id": "470dc398", |
| 142 | + "metadata": {}, |
| 143 | + "outputs": [], |
| 144 | + "source": [ |
| 145 | + "def process(guesses):\n", |
| 146 | + " scores, suggestion = score(guesses)\n", |
| 147 | + " print(\"Guesses: \", suggestion[:5])\n", |
| 148 | + " plot(scores)\n" |
| 149 | + ] |
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "code", |
| 153 | + "execution_count": null, |
| 154 | + "id": "4dff06d4", |
| 155 | + "metadata": {}, |
| 156 | + "outputs": [], |
| 157 | + "source": [ |
| 158 | + "process([\n", |
| 159 | + " ('human', ...),\n", |
| 160 | + "])" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": null, |
| 166 | + "id": "51a15483", |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [], |
| 169 | + "source": [] |
| 170 | + } |
| 171 | + ], |
| 172 | + "metadata": { |
| 173 | + "kernelspec": { |
| 174 | + "display_name": "Python 3 (ipykernel)", |
| 175 | + "language": "python", |
| 176 | + "name": "python3" |
| 177 | + }, |
| 178 | + "language_info": { |
| 179 | + "codemirror_mode": { |
| 180 | + "name": "ipython", |
| 181 | + "version": 3 |
| 182 | + }, |
| 183 | + "file_extension": ".py", |
| 184 | + "mimetype": "text/x-python", |
| 185 | + "name": "python", |
| 186 | + "nbconvert_exporter": "python", |
| 187 | + "pygments_lexer": "ipython3", |
| 188 | + "version": "3.10.6" |
| 189 | + } |
| 190 | + }, |
| 191 | + "nbformat": 4, |
| 192 | + "nbformat_minor": 5 |
| 193 | +} |
0 commit comments