{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# $sigmoid(x) = \\frac{1}{1+e^{(-x)}}$" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def sigmoid(x):\n", " \"\"\"\n", " Compute the sigmoid function for the input here.\n", "\n", " Arguments:\n", " x -- A scalar or numpy array.\n", "\n", " Return:\n", " s -- sigmoid(x)\n", " \"\"\"\n", "\n", " ### YOUR CODE HERE\n", " s = 1 / (1 + np.exp(-x))\n", " ### END YOUR CODE\n", "\n", " return s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# sigmoid_grad\n", "- $\\sigma(-x) = \\frac{1}{1 + e^{x}} = \\frac{e^x+1}{e^x + 1} - \\frac{e^x}{e^x + 1}=1-\\sigma(x)$\n", "\n", "- $\\sigma' = \\sigma(x) \\times (1 - \\sigma(x))$" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def sigmoid_grad(s):\n", " \"\"\"\n", " Compute the gradient for the sigmoid function here. Note that\n", " for this implementation, the input s should be the sigmoid\n", " function value of your original input x.\n", "\n", " Arguments:\n", " s -- A scalar or numpy array.\n", "\n", " Return:\n", " ds -- Your computed gradient.\n", " \"\"\"\n", "\n", " ### YOUR CODE HERE\n", " ds = s * (1-s)\n", " ### END YOUR CODE\n", "\n", " return ds" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running basic tests...\n", "[[0.73105858 0.88079708]\n", " [0.26894142 0.11920292]]\n", "[[0.19661193 0.10499359]\n", " [0.19661193 0.10499359]]\n", "You should verify these results by hand!\n", "\n" ] } ], "source": [ "def test_sigmoid_basic():\n", " \"\"\"\n", " Some simple tests to get you started.\n", " Warning: these are not exhaustive.\n", " \"\"\"\n", " print(\"Running basic tests...\")\n", " x = np.array([[1, 2], [-1, -2]])\n", " f = sigmoid(x)\n", " g = sigmoid_grad(f)\n", " print(f)\n", " f_ans = np.array([\n", " [0.73105858, 0.88079708],\n", " [0.26894142, 0.11920292]])\n", " assert np.allclose(f, f_ans, rtol=1e-05, atol=1e-06)\n", " print(g)\n", " g_ans = np.array([\n", " [0.19661193, 0.10499359],\n", " [0.19661193, 0.10499359]])\n", " assert np.allclose(g, g_ans, rtol=1e-05, atol=1e-06)\n", " print(\"You should verify these results by hand!\\n\")\n", "\n", "\n", "if __name__ == \"__main__\":\n", " test_sigmoid_basic();" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }