Last active
December 2, 2020 01:06
-
-
Save prl900/1ab297cd5905ad3b33eba7ceb3a1975c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Three methods of backpropagation with PyTorch\n", | |
"\n", | |
"#### 1.- Using loss function's `.backward()` call" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 124, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"19 tensor(1.1563, grad_fn=<SumBackward0>)\n", | |
"tensor([-1.3625, -3.2816, -0.8294])\n", | |
"tensor([1.3188, 2.5898, 2.9539], requires_grad=True)\n", | |
"39 tensor(0.2308, grad_fn=<SumBackward0>)\n", | |
"tensor([-0.9096, -0.6192, -0.0157])\n", | |
"tensor([1.5452, 2.9226, 2.9991], requires_grad=True)\n", | |
"59 tensor(0.0930, grad_fn=<SumBackward0>)\n", | |
"tensor([-6.0725e-01, -1.1684e-01, -2.9755e-04])\n", | |
"tensor([1.6964, 2.9854, 3.0000], requires_grad=True)\n", | |
"79 tensor(0.0411, grad_fn=<SumBackward0>)\n", | |
"tensor([-4.0541e-01, -2.2047e-02, -1.1444e-05])\n", | |
"tensor([1.7973, 2.9972, 3.0000], requires_grad=True)\n", | |
"99 tensor(0.0183, grad_fn=<SumBackward0>)\n", | |
"tensor([-2.7065e-01, -4.1580e-03, -1.1444e-05])\n", | |
"tensor([1.8647, 2.9995, 3.0000], requires_grad=True)\n", | |
"119 tensor(0.0082, grad_fn=<SumBackward0>)\n", | |
"tensor([-1.8069e-01, -7.8392e-04, -1.1444e-05])\n", | |
"tensor([1.9097, 2.9999, 3.0000], requires_grad=True)\n", | |
"139 tensor(0.0036, grad_fn=<SumBackward0>)\n", | |
"tensor([-1.2063e-01, -1.4877e-04, -1.1444e-05])\n", | |
"tensor([1.9397, 3.0000, 3.0000], requires_grad=True)\n", | |
"159 tensor(0.0016, grad_fn=<SumBackward0>)\n", | |
"tensor([-8.0533e-02, -2.8610e-05, -1.1444e-05])\n", | |
"tensor([1.9597, 3.0000, 3.0000], requires_grad=True)\n", | |
"179 tensor(0.0007, grad_fn=<SumBackward0>)\n", | |
"tensor([-5.3765e-02, -1.1444e-05, -1.1444e-05])\n", | |
"tensor([1.9731, 3.0000, 3.0000], requires_grad=True)\n", | |
"199 tensor(0.0003, grad_fn=<SumBackward0>)\n", | |
"tensor([-3.5894e-02, -1.1444e-05, -1.1444e-05])\n", | |
"tensor([1.9821, 3.0000, 3.0000], requires_grad=True)\n" | |
] | |
} | |
], | |
"source": [ | |
"import torch\n", | |
"\n", | |
"learning_rate = 1e-2\n", | |
"\n", | |
"x = torch.tensor([1.,2.,3.])\n", | |
"w = torch.tensor([1.,1.,1.], requires_grad=True)\n", | |
"y = torch.tensor([2.,6.,9.])\n", | |
"\n", | |
"\n", | |
"for i in range(200): \n", | |
" loss = (x.mul(w) - y).pow(2).sum()\n", | |
"\n", | |
" loss.backward()\n", | |
" if i % 20 == 19:\n", | |
" print(i, loss)\n", | |
" print(w.grad)\n", | |
" print(w)\n", | |
" with torch.no_grad():\n", | |
" w -= learning_rate * w.grad\n", | |
"\n", | |
" w.grad.zero_()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### 2.- Using `autograd.backward()` function\n", | |
"\n", | |
"*Method 1 is just a method that wraps this one*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 126, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"19 tensor(1.1563, grad_fn=<SumBackward0>)\n", | |
"tensor([-1.3625, -3.2816, -0.8294])\n", | |
"tensor([1.3188, 2.5898, 2.9539], requires_grad=True)\n", | |
"39 tensor(0.2308, grad_fn=<SumBackward0>)\n", | |
"tensor([-0.9096, -0.6192, -0.0157])\n", | |
"tensor([1.5452, 2.9226, 2.9991], requires_grad=True)\n", | |
"59 tensor(0.0930, grad_fn=<SumBackward0>)\n", | |
"tensor([-6.0725e-01, -1.1684e-01, -2.9755e-04])\n", | |
"tensor([1.6964, 2.9854, 3.0000], requires_grad=True)\n", | |
"79 tensor(0.0411, grad_fn=<SumBackward0>)\n", | |
"tensor([-4.0541e-01, -2.2047e-02, -1.1444e-05])\n", | |
"tensor([1.7973, 2.9972, 3.0000], requires_grad=True)\n", | |
"99 tensor(0.0183, grad_fn=<SumBackward0>)\n", | |
"tensor([-2.7065e-01, -4.1580e-03, -1.1444e-05])\n", | |
"tensor([1.8647, 2.9995, 3.0000], requires_grad=True)\n", | |
"119 tensor(0.0082, grad_fn=<SumBackward0>)\n", | |
"tensor([-1.8069e-01, -7.8392e-04, -1.1444e-05])\n", | |
"tensor([1.9097, 2.9999, 3.0000], requires_grad=True)\n", | |
"139 tensor(0.0036, grad_fn=<SumBackward0>)\n", | |
"tensor([-1.2063e-01, -1.4877e-04, -1.1444e-05])\n", | |
"tensor([1.9397, 3.0000, 3.0000], requires_grad=True)\n", | |
"159 tensor(0.0016, grad_fn=<SumBackward0>)\n", | |
"tensor([-8.0533e-02, -2.8610e-05, -1.1444e-05])\n", | |
"tensor([1.9597, 3.0000, 3.0000], requires_grad=True)\n", | |
"179 tensor(0.0007, grad_fn=<SumBackward0>)\n", | |
"tensor([-5.3765e-02, -1.1444e-05, -1.1444e-05])\n", | |
"tensor([1.9731, 3.0000, 3.0000], requires_grad=True)\n", | |
"199 tensor(0.0003, grad_fn=<SumBackward0>)\n", | |
"tensor([-3.5894e-02, -1.1444e-05, -1.1444e-05])\n", | |
"tensor([1.9821, 3.0000, 3.0000], requires_grad=True)\n" | |
] | |
} | |
], | |
"source": [ | |
"x = torch.tensor([1.,2.,3.])\n", | |
"w = torch.tensor([1.,1.,1.], requires_grad=True)\n", | |
"y = torch.tensor([2.,6.,9.])\n", | |
"\n", | |
"for i in range(200):\n", | |
" loss = (x.mul(w) - y).pow(2).sum()\n", | |
" \n", | |
" torch.autograd.backward(loss)\n", | |
" if i % 20 == 19:\n", | |
" print(i, loss)\n", | |
" print(w.grad)\n", | |
" print(w)\n", | |
" with torch.no_grad():\n", | |
" w -= learning_rate * w.grad\n", | |
" w.grad.zero_()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### 3.- Using `autograd.grad()` function\n", | |
"\n", | |
"*Method 2 uses this one iterating through each variable that needs a gradient*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 125, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"19 tensor(1.1563, grad_fn=<SumBackward0>)\n", | |
"tensor([-1.3625, -3.2816, -0.8294])\n", | |
"tensor([1.3188, 2.5898, 2.9539], requires_grad=True)\n", | |
"39 tensor(0.2308, grad_fn=<SumBackward0>)\n", | |
"tensor([-0.9096, -0.6192, -0.0157])\n", | |
"tensor([1.5452, 2.9226, 2.9991], requires_grad=True)\n", | |
"59 tensor(0.0930, grad_fn=<SumBackward0>)\n", | |
"tensor([-6.0725e-01, -1.1684e-01, -2.9755e-04])\n", | |
"tensor([1.6964, 2.9854, 3.0000], requires_grad=True)\n", | |
"79 tensor(0.0411, grad_fn=<SumBackward0>)\n", | |
"tensor([-4.0541e-01, -2.2047e-02, -1.1444e-05])\n", | |
"tensor([1.7973, 2.9972, 3.0000], requires_grad=True)\n", | |
"99 tensor(0.0183, grad_fn=<SumBackward0>)\n", | |
"tensor([-2.7065e-01, -4.1580e-03, -1.1444e-05])\n", | |
"tensor([1.8647, 2.9995, 3.0000], requires_grad=True)\n", | |
"119 tensor(0.0082, grad_fn=<SumBackward0>)\n", | |
"tensor([-1.8069e-01, -7.8392e-04, -1.1444e-05])\n", | |
"tensor([1.9097, 2.9999, 3.0000], requires_grad=True)\n", | |
"139 tensor(0.0036, grad_fn=<SumBackward0>)\n", | |
"tensor([-1.2063e-01, -1.4877e-04, -1.1444e-05])\n", | |
"tensor([1.9397, 3.0000, 3.0000], requires_grad=True)\n", | |
"159 tensor(0.0016, grad_fn=<SumBackward0>)\n", | |
"tensor([-8.0533e-02, -2.8610e-05, -1.1444e-05])\n", | |
"tensor([1.9597, 3.0000, 3.0000], requires_grad=True)\n", | |
"179 tensor(0.0007, grad_fn=<SumBackward0>)\n", | |
"tensor([-5.3765e-02, -1.1444e-05, -1.1444e-05])\n", | |
"tensor([1.9731, 3.0000, 3.0000], requires_grad=True)\n", | |
"199 tensor(0.0003, grad_fn=<SumBackward0>)\n", | |
"tensor([-3.5894e-02, -1.1444e-05, -1.1444e-05])\n", | |
"tensor([1.9821, 3.0000, 3.0000], requires_grad=True)\n" | |
] | |
} | |
], | |
"source": [ | |
"import torch\n", | |
"\n", | |
"learning_rate = 1e-2\n", | |
"\n", | |
"x = torch.tensor([1.,2.,3.])\n", | |
"w = torch.tensor([1.,1.,1.], requires_grad=True)\n", | |
"y = torch.tensor([2.,6.,9.])\n", | |
"\n", | |
"for i in range(200):\n", | |
" loss = (x.mul(w) - y).pow(2).sum()\n", | |
" \n", | |
" w.grad, = torch.autograd.grad(loss, w)\n", | |
" if i % 20 == 19:\n", | |
" print(i, loss)\n", | |
" print(w.grad)\n", | |
" print(w)\n", | |
" with torch.no_grad():\n", | |
" w -= learning_rate * w.grad" | |
] | |
}, | |
{ | |
"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.8.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment