<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0cdc6818",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot the softmax function.\n",
    "\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# from scipy.misc import logsumexp ##Outdated import doesn't work with newer scipy\n",
    "# from scipy.special import logsumexp\n",
    "\n",
    "\n",
    "try:\n",
    "    import probml_utils as pml\n",
    "except ModuleNotFoundError:\n",
    "    %pip install -qq git+https://github.com/probml/probml-utils.git\n",
    "    import probml_utils as pml\n",
    "\n",
    "\n",
    "def softmax(a):\n",
    "    e = np.exp((1.0 * np.array(a)))\n",
    "    return e / np.sum(e)\n",
    "\n",
    "\n",
    "T = [100, 2, 1]\n",
    "a = np.array([3, 0, 1])\n",
    "ind = [1, 2, 3]\n",
    "\n",
    "plt.figure(figsize=(12, 4))\n",
    "for i in range(len(T)):\n",
    "    plt.subplot(1, 3, i + 1)\n",
    "    plt.bar(ind, softmax(a / T[i]))\n",
    "    plt.title(\"T = %d\" % T[i])\n",
    "    plt.ylim(0, 1)\n",
    "    ax = plt.gca()\n",
    "    ax.set_xticklabels([])\n",
    "\n",
    "\n",
    "fname = \"softmax_temp.pdf\"\n",
    "pml.savefig(fname, dpi=300)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}<commit_msg>updated fig-2.12 of book1 (#915)<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f16eee92",
   "metadata": {},
   "source": [
    "# Sotfmax Distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1b81f0ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "import jax\n",
    "import jax.numpy as jnp\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "try:\n",
    "    from probml_utils import savefig, latexify\n",
    "except ModuleNotFoundError:\n",
    "    %pip install -qq git+https://github.com/probml/probml-utils.git\n",
    "    from probml_utils import savefig, latexify"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1eaba5e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/tensorboy/dev/env/lib/python3.8/site-packages/probml_utils/plotting.py:26: UserWarning: LATEXIFY environment variable not set, not latexifying\n",
      "  warnings.warn(\"LATEXIFY environment variable not set, not latexifying\")\n"
     ]
    }
   ],
   "source": [
    "latexify(fig_height=1.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "67284b1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def softmax(a):\n",
    "    e = jnp.exp((1.0 * jnp.array(a)))\n",
    "    return e / jnp.sum(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f4c29b68",
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_softmax_distribution(T, a, save_name, fig=None, axs=None):\n",
    "    ind = jnp.arange(1, len(a) + 1)\n",
    "    fig, axs = plt.subplots(1, len(T), sharey=\"row\")\n",
    "\n",
    "    axs[0].set_ylabel(r\"$S(a | T)$\")\n",
    "    for i, ax in enumerate(axs):\n",
    "        ax.bar(ind, softmax(a / T[i]))\n",
    "\n",
    "        ax.set_xlabel(\"logits (a)\")\n",
    "        ax.set_ylim(0, 1)\n",
    "        ax.set_title(f\"T = {T[i]}\")\n",
    "\n",
    "    sns.despine()\n",
    "    plt.tight_layout()\n",
    "\n",
    "    if len(save_name) > 0:\n",
    "        savefig(save_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "776702ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:absl:No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)\n",
      "/home/tensorboy/dev/env/lib/python3.8/site-packages/probml_utils/plotting.py:80: UserWarning: set FIG_DIR environment variable to save figures\n",
      "  warnings.warn(\"set FIG_DIR environment variable to save figures\")\n"
     ]
    },
    {
     "data": {
      "image/png": "<KEY>",
      "text/plain": [
       "<Figure size 432x288 with 3 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "T_array = [100, 2, 1]\n",
    "a = jnp.array([3, 0, 1])\n",
    "\n",
    "plot_softmax_distribution(T_array, a, f\"softmax_temp\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2829fa4",
   "metadata": {},
   "source": [
    "### Interactive figure for softmax distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d593fedf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b28b5773406e4b8eb8b59618ed0bf1c1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(IntSlider(value=50, description='T', min=1), Output()), _dom_classes=('widget-interact',…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from ipywidgets import interact\n",
    "\n",
    "\n",
    "@interact(T=(1, 100))\n",
    "def generate_interactinve_graph(T):\n",
    "    a = jnp.array([3, 0, 1])\n",
    "    ind = jnp.arange(1, len(a) + 1)\n",
    "\n",
    "    plt.figure()\n",
    "    plt.bar(ind, softmax(a / T))\n",
    "    plt.title(f\"T = {T}\")\n",
    "    plt.ylim(0, 1)\n",
    "    plt.xlabel(\"logits (a)\")\n",
    "    plt.ylabel(\"$S(a | T)$\")\n",
    "    sns.despine()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "prob_ml",
   "language": "python",
   "name": "prob_ml"
  },
  "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
{'fraction_non_alphanumeric': 0.22429295406244892, 'fraction_numerical': 0.025993133889161354, 'mean_word_length': 2.3304300489929233, 'pattern_counts': {'":': 92, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3383145', 'n_tokens_mistral': 2645, 'n_tokens_neox': 2519, 'n_words': 439}