Skip to content

Instantly share code, notes, and snippets.

@f0nzie
Created December 11, 2019 22:39
Show Gist options
  • Save f0nzie/2b3edad1c6ea92147852f0248ee0b13e to your computer and use it in GitHub Desktop.
Save f0nzie/2b3edad1c6ea92147852f0248ee0b13e to your computer and use it in GitHub Desktop.
backward pytorch
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "backward pytorch",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/f0nzie/2b3edad1c6ea92147852f0248ee0b13e/backward-pytorch.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "eFuj7UjM65J6",
"colab_type": "code",
"colab": {}
},
"source": [
"import torch\n",
"import numpy as np "
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "EpbLnkLsEpHl",
"colab_type": "text"
},
"source": [
"## Example 2"
]
},
{
"cell_type": "code",
"metadata": {
"id": "ToFuUaMr6-3T",
"colab_type": "code",
"outputId": "b4a8a9c0-6341-482b-cbce-765a036438da",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 197
}
},
"source": [
"x = np.arange(1,3,1)\n",
"x = torch.from_numpy(x).reshape(len(x), 1)\n",
"x = x.float()\n",
"x.requires_grad = True\n",
"print(x)\n",
"w = torch.randn((2,2), requires_grad = True)\n",
"print(w)\n",
"y = w @ x\n",
"print(y)\n",
"\n",
"# Compute gradients of w1 . x\n",
"y[0].backward(retain_graph=True)\n",
"print(w.grad)\n",
"# tensor([[1., 2.],\n",
"# [0., 0.]])\n",
"y[1].backward(retain_graph=True)\n",
"print(w.grad)"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"tensor([[1.],\n",
" [2.]], requires_grad=True)\n",
"tensor([[ 0.6973, -1.5348],\n",
" [-2.6904, -0.4845]], requires_grad=True)\n",
"tensor([[-2.3724],\n",
" [-3.6594]], grad_fn=<MmBackward>)\n",
"tensor([[1., 2.],\n",
" [0., 0.]])\n",
"tensor([[1., 2.],\n",
" [1., 2.]])\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Wywcl4g7EX6g",
"colab_type": "text"
},
"source": [
"## Example 2"
]
},
{
"cell_type": "code",
"metadata": {
"id": "lyKAHyp46_Hk",
"colab_type": "code",
"outputId": "604fd646-12cd-49f8-83c2-6eab0b4c8175",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 197
}
},
"source": [
"# From an example on linear regression\n",
"x = np.arange(1,16,1)\n",
"x = torch.from_numpy(x)\n",
"x = x.float()\n",
"x = x.view(-1, 3)\n",
"x.requires_grad = True # torch.Size([5, 3])\n",
"print(x)\n",
"# w represents the weights and y the model. very, very simple example\n",
"w = torch.randn((5, 5), requires_grad = True)\n",
"y = w @ x\n",
"print(y)"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"tensor([[ 1., 2., 3.],\n",
" [ 4., 5., 6.],\n",
" [ 7., 8., 9.],\n",
" [10., 11., 12.],\n",
" [13., 14., 15.]], requires_grad=True)\n",
"tensor([[-1.9718, -1.9902, -2.0085],\n",
" [ 0.4344, 0.8532, 1.2720],\n",
" [11.7566, 12.0687, 12.3809],\n",
" [39.5736, 45.3092, 51.0447],\n",
" [-6.0419, -6.4095, -6.7770]], grad_fn=<MmBackward>)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "oNnt1Z4nF9Do",
"colab_type": "code",
"outputId": "3dc53689-1f8b-4d2f-afeb-f81442c7e9ba",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 107
}
},
"source": [
"y[0,0].backward(retain_graph=True) # this computes the derivative\n",
"w.grad"
],
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"tensor([[ 1., 4., 7., 10., 13.],\n",
" [ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.]])"
]
},
"metadata": {
"tags": []
},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "g0uthKUaHJZG",
"colab_type": "code",
"outputId": "bf0b3f74-edf1-4852-8149-eec4d032d71d",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 107
}
},
"source": [
"y[0,1].backward(retain_graph=True) # this computes\n",
"w.grad"
],
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"tensor([[ 3., 9., 15., 21., 27.],\n",
" [ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.]])"
]
},
"metadata": {
"tags": []
},
"execution_count": 5
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "iWUqFCMCIyYg",
"colab_type": "code",
"outputId": "e948e4a0-509a-411b-f3a8-ece497b90d6f",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 107
}
},
"source": [
"\n",
"y[1,1].backward(retain_graph=True) # this doesn't\n",
"w.grad.data"
],
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"tensor([[ 3., 9., 15., 21., 27.],\n",
" [ 8., 20., 32., 44., 56.],\n",
" [ 3., 6., 9., 12., 15.],\n",
" [ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.]])"
]
},
"metadata": {
"tags": []
},
"execution_count": 10
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "ekJdNRXNI-h0",
"colab_type": "code",
"outputId": "ad0dfe31-4058-4b72-ba01-cb9ed5995f8f",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 107
}
},
"source": [
"# this doesn't compute any derivative\n",
"y[2,2].backward(retain_graph=True)\n",
"w.grad"
],
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"tensor([[ 3., 9., 15., 21., 27.],\n",
" [ 2., 5., 8., 11., 14.],\n",
" [ 3., 6., 9., 12., 15.],\n",
" [ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.]])"
]
},
"metadata": {
"tags": []
},
"execution_count": 7
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment