Last active
November 21, 2019 15:15
-
-
Save MHenderson/9fe8692a661b78115f696572161eabe3 to your computer and use it in GitHub Desktop.
PyTorch tutorial
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": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import torch" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([[1., 1.],\n", | |
" [1., 1.]], requires_grad=True)\n" | |
] | |
} | |
], | |
"source": [ | |
"x = torch.ones(2, 2, requires_grad=True)\n", | |
"print(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([[3., 3.],\n", | |
" [3., 3.]], grad_fn=<AddBackward>)\n" | |
] | |
} | |
], | |
"source": [ | |
"y = x + 2\n", | |
"print(y)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<AddBackward object at 0x7fc43432eda0>\n" | |
] | |
} | |
], | |
"source": [ | |
"print(y.grad_fn)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([[27., 27.],\n", | |
" [27., 27.]], grad_fn=<MulBackward>) tensor(27., grad_fn=<MeanBackward1>)\n" | |
] | |
} | |
], | |
"source": [ | |
"z = y * y * 3\n", | |
"out = z.mean()\n", | |
"\n", | |
"print(z, out)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"False\n", | |
"True\n", | |
"<SumBackward0 object at 0x7fc40a5574a8>\n" | |
] | |
} | |
], | |
"source": [ | |
"a = torch.randn(2, 2)\n", | |
"a = ((a * 3)/ (a - 1))\n", | |
"print(a.requires_grad)\n", | |
"a.requires_grad_(True)\n", | |
"print(a.requires_grad)\n", | |
"b = (a * a).sum()\n", | |
"print(b.grad_fn)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"out.backward()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([[4.5000, 4.5000],\n", | |
" [4.5000, 4.5000]])\n" | |
] | |
} | |
], | |
"source": [ | |
"print(x.grad)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([-1299.6705, -601.4221, 915.6087], grad_fn=<MulBackward>)\n" | |
] | |
} | |
], | |
"source": [ | |
"x = torch.randn(3, requires_grad=True)\n", | |
"\n", | |
"y = x * 2\n", | |
"while y.data.norm() < 1000:\n", | |
" y = y * 2\n", | |
" \n", | |
"print(y)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([ 204.8000, 2048.0000, 0.2048])\n" | |
] | |
} | |
], | |
"source": [ | |
"v = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)\n", | |
"y.backward(v)\n", | |
"\n", | |
"print(x.grad)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"print(x.requires_grad)\n", | |
"print((x ** 2).requires_grad)\n", | |
"\n", | |
"with torch.no_grad():\n", | |
" print()" | |
] | |
} | |
], | |
"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.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment