{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# This file is part of nannos\n# License: GPLv3\n%matplotlib notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Computing gradients\n\nIn this tutorial we will see how to compute gradients of quantities \nwith respect to input values automatically.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nimport nannos as nn\n\nnn.set_backend(\"torch\")\n# nn.set_backend(\"autograd\")\nfrom nannos import grad\n\nbk = nn.backend" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's define a function that will return the reflection coefficient for\na metasurface:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def f(thickness):\n lattice = nn.Lattice(([1, 0], [0, 1]))\n sup = lattice.Layer(\"Superstrate\")\n sub = lattice.Layer(\"Substrate\", epsilon=2)\n ms = lattice.Layer(\"ms\", thickness=thickness, epsilon=6)\n sim = nn.Simulation(\n [sup, ms, sub],\n nn.PlaneWave(1.5),\n nh=1,\n )\n R, T = sim.diffraction_efficiencies()\n return R\n\n\nx = bk.array([0.3], dtype=bk.float64)\nprint(f(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will compute the finite difference approximation\nof the gradient:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def first_finite_differences(f, x):\n eps = 1e-4\n return nn.backend.array(\n [(f(x + eps * v) - f(x - eps * v)) / (2 * eps) for v in nn.backend.eye(len(x))],\n )\n\n\ndf_fd = first_finite_differences(f, x)\nprint(df_fd)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Automatic differentiation:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "df = grad(f)\ndf_auto = df(x)\nprint(df_auto)\n\nassert nn.backend.allclose(df_fd, df_auto, atol=1e-7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A random pattern:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import random\n\nrandom.seed(2022)\n\ndiscretization = 2**4, 2**4\n\n\ndef f(var):\n lattice = nn.Lattice(([1, 0], [0, 1]), discretization=discretization)\n xa = var.reshape(lattice.discretization)\n sup = lattice.Layer(\"Superstrate\")\n sub = lattice.Layer(\"Substrate\")\n ms = lattice.Layer(\"ms\", 1)\n ms.epsilon = 9 + 1 * xa + 0j\n sim = nn.Simulation(\n [sup, ms, sub],\n nn.PlaneWave(1.5),\n nh=51,\n )\n R, T = sim.diffraction_efficiencies()\n return R\n\n\nnvar = discretization[0] * discretization[1]\nprint(nvar)\n\nxlist = [random.random() for _ in range(nvar)]\nx = bk.array(xlist, dtype=bk.float64)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finite differences:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t0 = nn.tic()\ndf_fd = first_finite_differences(f, x)\ntfd = nn.toc(t0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Automatic differentiation:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "df = grad(f)\nt0 = nn.tic()\ndf_auto = df(x)\ntauto = nn.toc(t0)\n\n\nassert nn.backend.allclose(df_fd, df_auto, atol=1e-7)\n\nprint(\"speedup: \", tfd / tauto)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot gradients\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(1, 2, figsize=(8, 3))\n_ = ax[0].imshow(df_auto.reshape(*discretization).real)\nplt.colorbar(_, ax=ax[0])\nax[0].set_title(\"autodiff\")\n_ = ax[1].imshow(df_fd.reshape(*discretization).real)\nplt.colorbar(_, ax=ax[1])\nax[1].set_title(\"finite differences\")\nplt.tight_layout()\n\n\nnn.set_backend(\"numpy\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import nannos.utils.jupyter\n%nannos_version_table" ] } ], "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.10.13" } }, "nbformat": 4, "nbformat_minor": 0 }