{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "7a365fdd", "metadata": {}, "outputs": [], "source": [ "import torch\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "282b233b", "metadata": {}, "source": [ "## Derivatives" ] }, { "cell_type": "code", "execution_count": 9, "id": "77f3c29f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(5., requires_grad=True)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = torch.tensor(5.0, requires_grad=True)\n", "x" ] }, { "cell_type": "markdown", "id": "6ae84c1f", "metadata": {}, "source": [ "$x = 5.0$\n", "\n", "$y = x^2 => f(x) = x^2$\n", "\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "d40fbe0d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(25., grad_fn=)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = x ** 2\n", "y" ] }, { "cell_type": "markdown", "id": "6f474ae0", "metadata": {}, "source": [ "$\\frac{dy}{dx} = 2x$\n", "\n", "$f'(x=5.0) = 2 * 5.0 = 10$" ] }, { "cell_type": "code", "execution_count": 11, "id": "dd8db750", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(10.)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y.backward()\n", "\n", "x.grad" ] }, { "cell_type": "markdown", "id": "27d0695a", "metadata": {}, "source": [ "## Partial derivative" ] }, { "cell_type": "code", "execution_count": 12, "id": "6abe730e", "metadata": {}, "outputs": [], "source": [ "x = torch.tensor(5.0, requires_grad=True)\n", "y = torch.tensor(5.0, requires_grad=True)\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "93926cf7", "metadata": {}, "outputs": [], "source": [ "f = x**2 + y**2" ] }, { "cell_type": "code", "execution_count": 14, "id": "4f329c02", "metadata": {}, "outputs": [], "source": [ "f.backward()" ] }, { "cell_type": "code", "execution_count": 15, "id": "d1cb5deb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.grad_fn" ] }, { "cell_type": "code", "execution_count": 16, "id": "d80038fb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(10.)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.grad # partial derivative wrt x at x = 5 and y =5" ] }, { "cell_type": "code", "execution_count": 17, "id": "6ab20700", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(10.)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y.grad # partial derivative wrt y at x = 5 and y =5" ] }, { "cell_type": "code", "execution_count": 22, "id": "0d44cfc1", "metadata": {}, "outputs": [], "source": [ "x = torch.tensor(5.0, requires_grad=True)\n", "y = torch.tensor(5.0, requires_grad=True)\n" ] }, { "cell_type": "code", "execution_count": 23, "id": "9396e8bf", "metadata": {}, "outputs": [], "source": [ "f2 = x**2 * y**2" ] }, { "cell_type": "markdown", "id": "fdad968c", "metadata": {}, "source": [ "$f2(x, y) = x^2 . y^2$\n", "\n", "$\\frac{\\partial f2}{\\partial x} = 2x.y^2$\n", "\n", "$\\frac{\\partial f2}{\\partial y} = x^2.2y$" ] }, { "cell_type": "code", "execution_count": 24, "id": "4bd23358", "metadata": {}, "outputs": [], "source": [ "f2.backward()" ] }, { "cell_type": "code", "execution_count": 25, "id": "6b101352", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f2.grad_fn" ] }, { "cell_type": "code", "execution_count": 26, "id": "4731a225", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(250.)" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.grad" ] }, { "cell_type": "markdown", "id": "33fba02c", "metadata": {}, "source": [ "## Successive Differentiation" ] }, { "cell_type": "code", "execution_count": 27, "id": "a3f4c644", "metadata": {}, "outputs": [], "source": [ "from torch.autograd import grad\n", "\n", "def nth_derivative(f, wrt, n=2):\n", " \n", " for i in range(n):\n", " grads = grad(f, wrt, create_graph=True)[0]\n", " f = grads.sum()\n", " \n", " return grads" ] }, { "cell_type": "code", "execution_count": 28, "id": "50612387", "metadata": {}, "outputs": [], "source": [ "x = torch.tensor(5.0, requires_grad=True)" ] }, { "cell_type": "markdown", "id": "064bbc40", "metadata": {}, "source": [ "$f(x) = x^2 + x^3$\n", "\n", "$f'(x) = 2x + 3x^2$\n", "\n", "$f''(x) = 2 + 6x$\n", "\n", "$f''(x=5) = 2 + 6*5 = 32$" ] }, { "cell_type": "code", "execution_count": 29, "id": "4bd61626", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(32., grad_fn=)" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = x**2 + x**3\n", "\n", "# double derivative\n", "nth_derivative(f, x)" ] }, { "cell_type": "code", "execution_count": null, "id": "99b5e49d", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.7.11" } }, "nbformat": 4, "nbformat_minor": 5 }