{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "dRLRjX3JxLVf" }, "source": [ "# Softmax" ] }, { "cell_type": "markdown", "metadata": { "id": "nX3hfD6jxLVh" }, "source": [ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/rickiepark/dl-illustrated/blob/master/notebooks/7-1.softmax_demo.ipynb)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "YRBIVdDYxLVh" }, "outputs": [], "source": [ "from math import exp" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "9wNI3mdqxLVi" }, "outputs": [], "source": [ "z = [-1.0, 1.0, 5.0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "S-4rpJKjxLVi", "outputId": "d1bcfa5d-22e2-4b68-fa92-88cb8194fef0" }, "outputs": [ { "data": { "text/plain": [ "0.36787944117144233" ] }, "execution_count": 3, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "exp(z[0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "d0JA2uMBxLVj", "outputId": "cc8b3bd8-77f3-434d-c8fe-61364a348477" }, "outputs": [ { "data": { "text/plain": [ "2.718281828459045" ] }, "execution_count": 4, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "exp(z[1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "JVQN1Vv5xLVj", "outputId": "ac41c43c-593a-4d58-c27f-2e7509aff4f3" }, "outputs": [ { "data": { "text/plain": [ "148.4131591025766" ] }, "execution_count": 5, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "exp(z[2])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "OismZJ9ixLVj" }, "outputs": [], "source": [ "total = exp(z[0]) + exp(z[1]) + exp(z[2])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Ib-HNhFKxLVk", "outputId": "6f1baadf-6b5b-4f80-dd61-1368dfdbc4e9" }, "outputs": [ { "data": { "text/plain": [ "151.49932037220708" ] }, "execution_count": 7, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "total" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "PhKNnVy_xLVk", "outputId": "73ccd4ac-ce01-460f-dc3b-c65cee137fd2" }, "outputs": [ { "data": { "text/plain": [ "0.0024282580295913376" ] }, "execution_count": 8, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "exp(z[0])/total" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "UgCR2NBSxLVk", "outputId": "d2ed3552-3cc2-4596-8ca2-f21c4b5b65dd" }, "outputs": [ { "data": { "text/plain": [ "0.017942534803329194" ] }, "execution_count": 9, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "exp(z[1])/total" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "uGsfVu2kxLVk", "outputId": "2b6c404c-16b7-46b3-ea52-892c488e7c46" }, "outputs": [ { "data": { "text/plain": [ "0.9796292071670795" ] }, "execution_count": 10, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "exp(z[2])/total" ] } ], "metadata": { "colab": { "name": "7-1.softmax_demo.ipynb", "provenance": [] }, "kernelspec": { "display_name": "TensorFlow 2.3 on Python 3.6 (CUDA 10.1)", "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.6.9" } }, "nbformat": 4, "nbformat_minor": 0 }