{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running sanity checks...\n", "Gradient check passed!\n", "Gradient check passed!\n", "Gradient check passed!\n", "\n" ] } ], "source": [ "import numpy as np\n", "import random\n", "\n", "\n", "# First implement a gradient checker by filling in the following functions\n", "def gradcheck_naive(f, x):\n", " \"\"\" Gradient check for a function f.\n", "\n", " Arguments:\n", " f -- a function that takes a single argument and outputs the\n", " cost and its gradients\n", " x -- the point (numpy array) to check the gradient at\n", " \"\"\"\n", "\n", " rndstate = random.getstate()\n", " random.setstate(rndstate)\n", " fx, grad = f(x) # Evaluate function value at original point\n", " h = 1e-4 # Do not change this!\n", "\n", " # Iterate over all indexes in x\n", " it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])\n", " while not it.finished:\n", " ix = it.multi_index\n", " # Try modifying x[ix] with h defined above to compute\n", " # numerical gradients. Make sure you call random.setstate(rndstate)\n", " # before calling f(x) each time. This will make it possible\n", " # to test cost functions with built in randomness later.\n", "\n", " ### YOUR CODE HERE:\n", " x[ix] += h\n", " random.setstate(rndstate)\n", " new_f1 = f(x)[0]\n", " x[ix] -= 2*h\n", " random.setstate(rndstate)\n", " new_f2 = f(x)[0]\n", " x[ix] += h\n", " numgrad = (new_f1 - new_f2) / (2 * h)\n", " ### END YOUR CODE\n", "\n", " # Compare gradients\n", " reldiff = abs(numgrad - grad[ix]) / max(1, abs(numgrad), abs(grad[ix]))\n", " if reldiff > 1e-5:\n", " print(\"Gradient check failed.\")\n", " print(\"First gradient error found at index %s\" % str(ix))\n", " print(\"Your gradient: %f \\t Numerical gradient: %f\" % (\n", " grad[ix], numgrad))\n", " return\n", "\n", " it.iternext() # Step to next dimension\n", "\n", " print(\"Gradient check passed!\")\n", "\n", "\n", "def sanity_check():\n", " \"\"\"\n", " Some basic sanity checks.\n", " \"\"\"\n", " quad = lambda x: (np.sum(x ** 2), x * 2)\n", "\n", " print(\"Running sanity checks...\")\n", " gradcheck_naive(quad, np.array(123.456)) # scalar test\n", " gradcheck_naive(quad, np.random.randn(3,)) # 1-D test\n", " gradcheck_naive(quad, np.random.randn(4,5)) # 2-D test\n", " print(\"\")\n", "\n", "\n", "if __name__ == \"__main__\":\n", " sanity_check()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }