update fundamentals

This commit is contained in:
Daniel Bourke committed 2021-10-20 11:48:13 +10:00
1 parent 2c17629505
commit 0a1219ca1f
1 file changed
+230 -25
+230 -25
View File
@@ -16,6 +16,8 @@
"id": "jSNK7duj5SeU"
},
"source": [
"# 00. PyTorch Fundamentals\n",
"\n",
"# TK - intro to PyTorch\n",
"\n",
"## TK - What is PyTorch?\n",
@@ -899,7 +901,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -914,19 +916,20 @@
"tensor([1, 2, 3])"
]
},
"execution_count": 50,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import torch\n",
"tensor = torch.tensor([1, 2, 3])\n",
"tensor"
]
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -941,7 +944,7 @@
"tensor(14)"
]
},
"execution_count": 51,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
@@ -952,7 +955,7 @@
},
{
"cell_type": "code",
"execution_count": 54,
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -967,7 +970,7 @@
"tensor(14)"
]
},
"execution_count": 54,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
@@ -978,7 +981,7 @@
},
{
"cell_type": "code",
"execution_count": 58,
"execution_count": 5,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -993,7 +996,7 @@
"tensor(14)"
]
},
"execution_count": 58,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
@@ -1004,7 +1007,7 @@
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1019,7 +1022,7 @@
"tensor(14)"
]
},
"execution_count": 60,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@@ -1034,7 +1037,7 @@
},
{
"cell_type": "code",
"execution_count": 66,
"execution_count": 7,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
@@ -1046,12 +1049,12 @@
"outputs": [
{
"ename": "RuntimeError",
"evalue": "ignored",
"evalue": "mat1 and mat2 shapes cannot be multiplied (3x2 and 3x2)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-66-823bb99b3dae>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 8\u001b[0m [11, 12]], dtype=torch.float32)\n\u001b[1;32m 9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatmul\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtensor_A\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtensor_B\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/tmp/ipykernel_34033/854767186.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 8\u001b[0m [11, 12]], dtype=torch.float32)\n\u001b[1;32m 9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatmul\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtensor_A\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtensor_B\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mRuntimeError\u001b[0m: mat1 and mat2 shapes cannot be multiplied (3x2 and 3x2)"
]
}
@@ -1071,7 +1074,7 @@
},
{
"cell_type": "code",
"execution_count": 67,
"execution_count": 8,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1088,7 +1091,7 @@
" [ 83., 105., 127.]])"
]
},
"execution_count": 67,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@@ -1097,6 +1100,29 @@
"torch.matmul(tensor_A, tensor_B.T)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[ 23., 29., 35.],\n",
" [ 53., 67., 81.],\n",
" [ 83., 105., 127.]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# torch.mm is a shortcut for matmul\n",
"torch.mm(tensor_A, tensor_B.T)"
]
},
{
"cell_type": "code",
"execution_count": 79,
@@ -1422,7 +1448,7 @@
},
{
"cell_type": "code",
"execution_count": 107,
"execution_count": 16,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1437,19 +1463,20 @@
"tensor([1., 2., 3., 4., 5., 6., 7.])"
]
},
"execution_count": 107,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import torch\n",
"x = torch.arange(1., 8.)\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 115,
"execution_count": 17,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1464,7 +1491,7 @@
"(tensor([[1., 2., 3., 4., 5., 6., 7.]]), torch.Size([1, 7]))"
]
},
"execution_count": 115,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
@@ -1477,7 +1504,52 @@
},
{
"cell_type": "code",
"execution_count": 123,
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(tensor([[1., 2., 3., 4., 5., 6., 7.]]), torch.Size([1, 7]))"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Change view (keeps same data as original but changes view)\n",
"# See more: https://stackoverflow.com/a/54507446/7900723\n",
"z = x.view(1, 7)\n",
"z, z.shape"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(tensor([[5., 2., 3., 4., 5., 6., 7.]]), tensor([5., 2., 3., 4., 5., 6., 7.]))"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Changing z changes x\n",
"z[:, 0] = 5\n",
"z, x"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1489,13 +1561,13 @@
{
"data": {
"text/plain": [
"tensor([[1., 2., 3., 4., 5., 6., 7.],\n",
" [1., 2., 3., 4., 5., 6., 7.],\n",
" [1., 2., 3., 4., 5., 6., 7.],\n",
" [1., 2., 3., 4., 5., 6., 7.]])"
"tensor([[5., 2., 3., 4., 5., 6., 7.],\n",
" [5., 2., 3., 4., 5., 6., 7.],\n",
" [5., 2., 3., 4., 5., 6., 7.],\n",
" [5., 2., 3., 4., 5., 6., 7.]])"
]
},
"execution_count": 123,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
@@ -1562,6 +1634,139 @@
"x_unsqueezed, x_unsqueezed.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## TK - Indexing (selecting data from tensors)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[[0, 1, 2],\n",
" [3, 4, 5],\n",
" [6, 7, 8]]])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import torch\n",
"tensor = torch.arange(0, 9, 1).view(1, 3, 3)\n",
"tensor"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[0, 1, 2],\n",
" [3, 4, 5],\n",
" [6, 7, 8]])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tensor[0]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[0, 1, 2]])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tensor[:, 0]"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[1, 4, 7]])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tensor[:, :, 1]"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([4])"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tensor[:, 1, 1]"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[0, 3, 6]])"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tensor[:, :, 0]"
]
},
{
"cell_type": "markdown",
"metadata": {