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": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import torch" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([[1.8293e+25, 4.5808e-41, 2.7649e-37],\n", | |
" [0.0000e+00, 0.0000e+00, 0.0000e+00],\n", | |
" [0.0000e+00, 0.0000e+00, 0.0000e+00],\n", | |
" [0.0000e+00, 0.0000e+00, 0.0000e+00],\n", | |
" [0.0000e+00, 0.0000e+00, 0.0000e+00]])\n" | |
] | |
} | |
], | |
"source": [ | |
"x = torch.empty(5, 3)\n", | |
"print(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([[0.0999, 0.0514, 0.5746],\n", | |
" [0.2492, 0.3858, 0.2630],\n", | |
" [0.2320, 0.5569, 0.0581],\n", | |
" [0.5044, 0.3515, 0.9185],\n", | |
" [0.0800, 0.5181, 0.6103]])\n" | |
] | |
} | |
], | |
"source": [ | |
"x = torch.rand(5, 3)\n", | |
"print(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([[0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0]])\n" | |
] | |
} | |
], | |
"source": [ | |
"x = torch.zeros(5, 3, dtype=torch.long)\n", | |
"print(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([5.5000, 3.0000])\n" | |
] | |
} | |
], | |
"source": [ | |
"x = torch.tensor([5.5, 3])\n", | |
"print(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([[1., 1., 1.],\n", | |
" [1., 1., 1.],\n", | |
" [1., 1., 1.],\n", | |
" [1., 1., 1.],\n", | |
" [1., 1., 1.]], dtype=torch.float64)\n" | |
] | |
} | |
], | |
"source": [ | |
"x = x.new_ones(5, 3, dtype=torch.double)\n", | |
"print(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([[ 0.4409, -1.0526, -0.1251],\n", | |
" [-3.1898, -0.9379, -0.1052],\n", | |
" [ 0.1024, -0.3951, -1.3711],\n", | |
" [ 0.0613, 0.0635, -0.7447],\n", | |
" [-0.1321, -0.1839, -1.8460]])\n" | |
] | |
} | |
], | |
"source": [ | |
"x = torch.randn_like(x, dtype=torch.float)\n", | |
"print(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"torch.Size([5, 3])\n" | |
] | |
} | |
], | |
"source": [ | |
"print(x.size())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([[ 0.5931, -0.9253, 0.3852],\n", | |
" [-2.8199, 0.0564, -0.0479],\n", | |
" [ 1.0666, -0.3886, -0.8085],\n", | |
" [ 0.1084, 1.0452, 0.0571],\n", | |
" [-0.0160, 0.5582, -1.0145]])\n" | |
] | |
} | |
], | |
"source": [ | |
"y = torch.rand(5, 3)\n", | |
"print(x + y)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([[ 0.5931, -0.9253, 0.3852],\n", | |
" [-2.8199, 0.0564, -0.0479],\n", | |
" [ 1.0666, -0.3886, -0.8085],\n", | |
" [ 0.1084, 1.0452, 0.0571],\n", | |
" [-0.0160, 0.5582, -1.0145]])\n" | |
] | |
} | |
], | |
"source": [ | |
"print(torch.add(x, y))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"tensor([[ 0.5931, -0.9253, 0.3852],\n", | |
" [-2.8199, 0.0564, -0.0479],\n", | |
" [ 1.0666, -0.3886, -0.8085],\n", | |
" [ 0.1084, 1.0452, 0.0571],\n", | |
" [-0.0160, 0.5582, -1.0145]])" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"result = torch.empty(5, 3)\n", | |
"torch.add(x, y, out=result)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"tensor([[ 0.5931, -0.9253, 0.3852],\n", | |
" [-2.8199, 0.0564, -0.0479],\n", | |
" [ 1.0666, -0.3886, -0.8085],\n", | |
" [ 0.1084, 1.0452, 0.0571],\n", | |
" [-0.0160, 0.5582, -1.0145]])" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"y.add_(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([-1.0526, -0.9379, -0.3951, 0.0635, -0.1839])\n" | |
] | |
} | |
], | |
"source": [ | |
"print(x[:, 1])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"x = torch.randn(4, 4)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"y = x.view(16)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"z = x.view(-1, 8)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])\n" | |
] | |
} | |
], | |
"source": [ | |
"print(x.size(), y.size(), z.size())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"x = torch.randn(1)\n", | |
"print(x)\n", | |
"print(x.item())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"a = torch.ones(5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([1., 1., 1., 1., 1.])\n" | |
] | |
} | |
], | |
"source": [ | |
"print(a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1. 1. 1. 1. 1.]\n" | |
] | |
} | |
], | |
"source": [ | |
"b = a.numpy()\n", | |
"print(b)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"tensor([2., 2., 2., 2., 2.])" | |
] | |
}, | |
"execution_count": 24, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a.add_(1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"tensor([2., 2., 2., 2., 2.])\n" | |
] | |
} | |
], | |
"source": [ | |
"print(a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[2. 2. 2. 2. 2.]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(b)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[2. 2. 2. 2. 2.]\n", | |
"tensor([2., 2., 2., 2., 2.], dtype=torch.float64)\n" | |
] | |
} | |
], | |
"source": [ | |
"import numpy as np\n", | |
"a = np.ones(5)\n", | |
"b = torch.from_numpy(a)\n", | |
"np.add(a, 1, out=a)\n", | |
"print(a)\n", | |
"print(b)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"False" | |
] | |
}, | |
"execution_count": 28, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"torch.cuda.is_available()" | |
] | |
}, | |
{ | |
"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.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