Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save crcrpar/2f8ae5dd14dcad4e44ea7d2fea383cbc to your computer and use it in GitHub Desktop.
Save crcrpar/2f8ae5dd14dcad4e44ea7d2fea383cbc to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "aerial-clock",
"metadata": {},
"source": [
"# computational-graph-is-only-built-once/113923"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "distinguished-stopping",
"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": 2,
"id": "sublime-outside",
"metadata": {},
"outputs": [],
"source": [
"m = nn.Linear(4, 2)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "urban-contractor",
"metadata": {},
"outputs": [],
"source": [
"loss = None\n",
"\n",
"for _ in range(5):\n",
" x = torch.rand(4, 4)\n",
" cur_loss = m(x).sum()\n",
" if loss is None:\n",
" loss = cur_loss\n",
" else:\n",
" loss += cur_loss\n",
"loss.backward()"
]
}
],
"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