Created
March 7, 2021 01:58
-
-
Save crcrpar/6032340ba8f1f8a7f8e5260e2f537be1 to your computer and use it in GitHub Desktop.
This file contains 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", | |
"id": "electoral-admission", | |
"metadata": {}, | |
"source": [ | |
"# computational-graph-is-only-built-once/113923" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "fallen-isolation", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1.8.0\n" | |
] | |
} | |
], | |
"source": [ | |
"import torch\n", | |
"import torch.nn as nn\n", | |
"import torch.nn.functional as F\n", | |
"\n", | |
"print(torch.__version__)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "tracked-virtue", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"m = nn.Linear(4, 2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"id": "according-polls", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"m.zero_grad()\n", | |
"loss = None\n", | |
"grads = []\n", | |
"inputs = []\n", | |
"for _ in range(5):\n", | |
" x = torch.rand(4, 4)\n", | |
" inputs.append(x.clone().detach())\n", | |
" cur_loss = m(x).sum()\n", | |
" grads.append(torch.autograd.grad(cur_loss, m.weight)[0])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"id": "higher-arlington", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[tensor([[1.5253, 1.6025, 2.1368, 1.9085],\n", | |
" [1.5253, 1.6025, 2.1368, 1.9085]]),\n", | |
" tensor([[1.9054, 1.6825, 2.2009, 2.3773],\n", | |
" [1.9054, 1.6825, 2.2009, 2.3773]]),\n", | |
" tensor([[2.5990, 2.4845, 2.4768, 2.2761],\n", | |
" [2.5990, 2.4845, 2.4768, 2.2761]]),\n", | |
" tensor([[2.4386, 1.6646, 1.7932, 1.1445],\n", | |
" [2.4386, 1.6646, 1.7932, 1.1445]]),\n", | |
" tensor([[2.8388, 2.5705, 1.8727, 2.1668],\n", | |
" [2.8388, 2.5705, 1.8727, 2.1668]])]" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"grads" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"id": "aboriginal-failing", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"tensor([[11.3070, 10.0046, 10.4804, 9.8733],\n", | |
" [11.3070, 10.0046, 10.4804, 9.8733]])" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sum(grads)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"id": "light-regulation", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"tensor([[11.3070, 10.0046, 10.4804, 9.8733],\n", | |
" [11.3070, 10.0046, 10.4804, 9.8733]])" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"m.zero_grad()\n", | |
"loss = None\n", | |
"for x in inputs:\n", | |
" cur_loss = m(x).sum()\n", | |
" if loss is None:\n", | |
" loss = cur_loss\n", | |
" else:\n", | |
" loss += cur_loss\n", | |
"loss.backward()\n", | |
"m.weight.grad" | |
] | |
} | |
], | |
"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.7.9" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment