diff --git a/00_pytorch_fundamentals.ipynb b/00_pytorch_fundamentals.ipynb
index 7e81915..deeb06d 100644
--- a/00_pytorch_fundamentals.ipynb
+++ b/00_pytorch_fundamentals.ipynb
@@ -3,11 +3,11 @@
{
"cell_type": "markdown",
"metadata": {
- "colab_type": "text",
- "id": "view-in-github"
+ "id": "view-in-github",
+ "colab_type": "text"
},
"source": [
- "
"
+ "
"
]
},
{
@@ -77,7 +77,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "5v3iRCRUTGeu"
+ },
"source": [
"## TK - Importing PyTorch\n",
"\n",
@@ -91,21 +93,25 @@
"execution_count": 1,
"metadata": {
"colab": {
- "base_uri": "https://localhost:8080/"
+ "base_uri": "https://localhost:8080/",
+ "height": 35
},
"id": "1VxEOik46Y4i",
- "outputId": "8b131929-d8f9-4d1e-ea1b-f2cb0d5447a4"
+ "outputId": "e38e73d1-8a1f-4744-f25a-3747895384bc"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "string"
+ },
"text/plain": [
- "'1.10.0'"
+ "'1.10.0+cu111'"
]
},
- "execution_count": 1,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 1
}
],
"source": [
@@ -115,7 +121,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "_SqvI4S9TGew"
+ },
"source": [
"Wonderful, it looks like we've got PyTorch 1.10.0 (as of Decemeber 2021). This means if you're going through these materials, you'll see most compatability with PyTorch 1.10.0, however if your version number is far higher than that, you might notice some inconsistencies. \n",
"\n",
@@ -170,24 +178,24 @@
},
{
"cell_type": "code",
- "execution_count": 3,
+ "execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "YUDgG2zk7Us5",
- "outputId": "b53cc251-6458-47a2-ae77-3006c045a2da"
+ "outputId": "f5f95f99-a32b-4437-fdff-3019655661c0"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"tensor(7)"
]
},
- "execution_count": 3,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 2
}
],
"source": [
@@ -198,7 +206,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "JqSuhW7rTGey"
+ },
"source": [
"See how the above printed out `tensor(7)`?\n",
"\n",
@@ -209,24 +219,24 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lV98Yz868bav",
- "outputId": "a571d544-d615-48ef-db49-80021dbe8efb"
+ "outputId": "3a5f3239-067d-4fc4-be21-a193685c1a86"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"0"
]
},
- "execution_count": 4,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 3
}
],
"source": [
@@ -235,7 +245,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "ZO2YW_QGTGez"
+ },
"source": [
"What if we wanted to retrieve the number from the tensor?\n",
"\n",
@@ -252,18 +264,18 @@
"base_uri": "https://localhost:8080/"
},
"id": "-k4cyKumPfbE",
- "outputId": "0756a55c-9b9b-4f52-a832-9abbafe689e0"
+ "outputId": "1dd60c0e-1561-4926-960e-731fca76ddbf"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"7"
]
},
- "execution_count": 4,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 4
}
],
"source": [
@@ -273,7 +285,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "qYs7ulrATGe0"
+ },
"source": [
"Okay, now let's see a **vector**.\n",
"\n",
@@ -286,24 +300,24 @@
},
{
"cell_type": "code",
- "execution_count": 16,
+ "execution_count": 5,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "-IZF6ASs8QH9",
- "outputId": "43f9e4b0-e836-4b13-9ebd-94c689a12e86"
+ "outputId": "77fffeee-f26d-4f9c-d5b2-1a784ae77faa"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"tensor([7, 7])"
]
},
- "execution_count": 16,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 5
}
],
"source": [
@@ -314,7 +328,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "mXxRUUW2TGe1"
+ },
"source": [
"Wonderful, `vector` now contains two 7's, my favourite number.\n",
"\n",
@@ -323,24 +339,24 @@
},
{
"cell_type": "code",
- "execution_count": 17,
+ "execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "03hm3VVv8kr4",
- "outputId": "c0b58040-8c82-49b3-a46f-02a80859f4dc"
+ "outputId": "d18fe1b1-9cb9-4eb9-a99d-5e289b850164"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"1"
]
},
- "execution_count": 17,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 6
}
],
"source": [
@@ -350,7 +366,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "W0VYvSGbTGe1"
+ },
"source": [
"Hmm, that's strange, `vector` contains two numbers but only has a single dimension.\n",
"\n",
@@ -367,18 +385,24 @@
},
{
"cell_type": "code",
- "execution_count": 18,
- "metadata": {},
+ "execution_count": 7,
+ "metadata": {
+ "id": "6zREV1bDTGe2",
+ "outputId": "4d81acb6-a6eb-4eff-9748-be6e6370080a",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"torch.Size([2])"
]
},
- "execution_count": 18,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 7
}
],
"source": [
@@ -388,7 +412,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "9aWKppNyTGe2"
+ },
"source": [
"The above returns `torch.Size([2])` which means our vector has a shape of `[2]`. This is because of the two elements we placed inside the square brackets (`[7, 7]`).\n",
"\n",
@@ -397,25 +423,25 @@
},
{
"cell_type": "code",
- "execution_count": 21,
+ "execution_count": 8,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "D5iNwCYL8QO9",
- "outputId": "24e6d331-943c-42dc-e258-6a4780ee97f9"
+ "outputId": "c38ae12b-e2f0-40b7-aaf1-64deeb71384e"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"tensor([[ 7, 8],\n",
" [ 9, 10]])"
]
},
- "execution_count": 21,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 8
}
],
"source": [
@@ -427,7 +453,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "a3U1bCdjTGe3"
+ },
"source": [
"Wow! More numbers! Matrices are as flexible as vectors, except they've got an extra dimension.\n",
"\n"
@@ -435,24 +463,24 @@
},
{
"cell_type": "code",
- "execution_count": 22,
+ "execution_count": 9,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8LREUbeb8r8j",
- "outputId": "575a9f1c-d552-469f-9678-71c17f35794e"
+ "outputId": "9ccce130-e25b-4013-927b-282cac3bf3a0"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"2"
]
},
- "execution_count": 22,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 9
}
],
"source": [
@@ -462,7 +490,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "LhXXgq-dTGe3"
+ },
"source": [
"`MATRIX` has two dimensions (did you count the number of square brakcets on the outside of one side?).\n",
"\n",
@@ -471,18 +501,24 @@
},
{
"cell_type": "code",
- "execution_count": 23,
- "metadata": {},
+ "execution_count": 10,
+ "metadata": {
+ "id": "_TL26I31TGe3",
+ "outputId": "7c8ed83f-19cc-45ce-ec84-499e2071b07c",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"torch.Size([2, 2])"
]
},
- "execution_count": 23,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 10
}
],
"source": [
@@ -491,7 +527,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "dvLpUvrKTGe4"
+ },
"source": [
"We get the output `torch.Size([2, 2])` because `MATRIX` is two elements deep and two elements wide.\n",
"\n",
@@ -500,16 +538,17 @@
},
{
"cell_type": "code",
- "execution_count": 24,
+ "execution_count": 11,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "wEMDQr188QWW",
- "outputId": "0394c6bf-4dde-41e0-80ed-3b0d4ca7c85d"
+ "outputId": "b9ba8fb8-ea5b-4d97-9f41-7d53015c61ff"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"tensor([[[1, 2, 3],\n",
@@ -517,9 +556,8 @@
" [2, 4, 5]]])"
]
},
- "execution_count": 24,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 11
}
],
"source": [
@@ -532,7 +570,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "UmJKkXD7TGe4"
+ },
"source": [
"Woah! What a nice looking tensor.\n",
"\n",
@@ -547,24 +587,24 @@
},
{
"cell_type": "code",
- "execution_count": 29,
+ "execution_count": 12,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8dhuEsjS8QcT",
- "outputId": "2a08eb90-3625-4b03-b004-bc8f3556669b"
+ "outputId": "7d27aa8e-9e0e-4451-cc83-a54b8cdff489"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"3"
]
},
- "execution_count": 29,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 12
}
],
"source": [
@@ -574,25 +614,33 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "ln9dys5VTGe4"
+ },
"source": [
"And what about its shape?"
]
},
{
"cell_type": "code",
- "execution_count": 30,
- "metadata": {},
+ "execution_count": 13,
+ "metadata": {
+ "id": "hdVv4iNRTGe5",
+ "outputId": "45326a47-26b1-4c43-fdba-d84eaf02523d",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"torch.Size([1, 3, 3])"
]
},
- "execution_count": 30,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 13
}
],
"source": [
@@ -602,7 +650,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "zxk8GU7oTGe5"
+ },
"source": [
"Alright, it outputs `torch.Size([1, 3, 3])`.\n",
"\n",
@@ -630,7 +680,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "dms7G4nkTGe5"
+ },
"source": [
"### Random tensors\n",
"\n",
@@ -657,27 +709,26 @@
},
{
"cell_type": "code",
- "execution_count": 31,
+ "execution_count": 14,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "EOJEtDx--GnK",
- "outputId": "a717476d-4199-4ba9-859c-0fd788c456c5"
+ "outputId": "eed68c5c-a13b-46af-e591-3cf1c9c09140"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
- "(tensor([[0.4043, 0.9331, 0.4070, 0.2407],\n",
- " [0.9738, 0.4960, 0.6324, 0.0604],\n",
- " [0.5516, 0.3168, 0.9767, 0.2678]]),\n",
- " torch.float32)"
+ "(tensor([[0.2341, 0.2731, 0.3500, 0.4046],\n",
+ " [0.4396, 0.8131, 0.7654, 0.6895],\n",
+ " [0.4141, 0.1021, 0.1505, 0.8856]]), torch.float32)"
]
},
- "execution_count": 31,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 14
}
],
"source": [
@@ -688,16 +739,58 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "-wB1c_cXTGe5"
+ },
"source": [
- "UPTOHERE... \"we can adjust the size to be whatever we want... "
+ "The flexibility of `torch.rand()` is that we can adjust the `size` to be whatever we want.\n",
+ "\n",
+ "For example, say you wanted a random tensor in the common image shape of `[224, 224, 3]` (`[height, width, color_channels`])."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Create a random tensor of size (224, 224, 3)\n",
+ "random_image_size_tensor = torch.rand(size=(224, 224, 3))\n",
+ "random_image_size_tensor.shape, random_image_size_tensor.ndim"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "xMF_NUp3Ym__",
+ "outputId": "2dcbb67e-c427-4f47-9958-7852a3f50aa7"
+ },
+ "execution_count": 15,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "(torch.Size([224, 224, 3]), 3)"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 15
+ }
]
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "0MQNTY0eTGe6"
+ },
"source": [
- "### Zeros and ones"
+ "### Zeros and ones\n",
+ "\n",
+ "Sometimes you'll just want to fill tensors with zeros or ones.\n",
+ "\n",
+ "This happens a lot with masking (like masking some of the values in one tensor with zeros to let a model know not to learn them).\n",
+ "\n",
+ "Let's create a tensor full of zeros with [`torch.zeros()`](https://pytorch.org/docs/stable/generated/torch.zeros.html)\n",
+ "\n",
+ "Again, the `size` parameter comes into play."
]
},
{
@@ -708,94 +801,142 @@
"base_uri": "https://localhost:8080/"
},
"id": "oCzhd0hl9Vp6",
- "outputId": "ffcbcc86-ef8d-4284-d6ca-c1c6df7c5bff"
+ "outputId": "beeff2e1-42d9-4414-e7d9-7ed381252f21"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"(tensor([[0., 0., 0., 0.],\n",
" [0., 0., 0., 0.],\n",
- " [0., 0., 0., 0.]]),\n",
- " torch.float32)"
+ " [0., 0., 0., 0.]]), torch.float32)"
]
},
- "execution_count": 16,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 16
}
],
"source": [
- "# Zeros\n",
+ "# Create a tensor of all zeros\n",
"zeros = torch.zeros(size=(3, 4))\n",
"zeros, zeros.dtype"
]
},
+ {
+ "cell_type": "markdown",
+ "source": [
+ "We can do the same to create a tensor of all ones except using [`torch.ones()` ](https://pytorch.org/docs/stable/generated/torch.ones.html) instead."
+ ],
+ "metadata": {
+ "id": "WDQBZJRUZWTN"
+ }
+ },
{
"cell_type": "code",
- "execution_count": 17,
- "metadata": {},
+ "execution_count": 18,
+ "metadata": {
+ "id": "HRe6sSXiTGe6",
+ "outputId": "af32dc99-1dde-4b4d-a0b9-916770d35294",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"(tensor([[1., 1., 1., 1.],\n",
" [1., 1., 1., 1.],\n",
- " [1., 1., 1., 1.]]),\n",
- " torch.float32)"
+ " [1., 1., 1., 1.]]), torch.float32)"
]
},
- "execution_count": 17,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 18
}
],
"source": [
- "# Ones\n",
+ "# Create a tensor of all ones\n",
"ones = torch.ones(size=(3, 4))\n",
"ones, ones.dtype"
]
},
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Creating a range and tensors like\n",
+ "\n",
+ "Sometimes you might want a range of numbers, such as 1 to 10 or 0 to 100.\n",
+ "\n",
+ "You can use `torch.arange(start, end, step)` to do so.\n",
+ "\n",
+ "Where:\n",
+ "* `start` = start of range (e.g. 0)\n",
+ "* `end` = end of range (e.g. 10)\n",
+ "* `step` = how many steps in between each value (e.g. 1)\n",
+ "\n",
+ "> **Note:** In Python, you can use `range()` to create a range. However in PyTorch, `torch.range()` is deprecated and may show an error in the future."
+ ],
+ "metadata": {
+ "id": "hib1NYrSarL2"
+ }
+ },
{
"cell_type": "code",
- "execution_count": 20,
+ "execution_count": 19,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1IqUs81d9W4W",
- "outputId": "20c17956-9d58-4757-c786-cb089d2a9551"
+ "outputId": "b195e0c9-9db2-4f65-d4ac-b8c905ee5a49"
},
"outputs": [
{
- "name": "stderr",
"output_type": "stream",
+ "name": "stderr",
"text": [
- "/tmp/ipykernel_327633/58302902.py:2: UserWarning: torch.range is deprecated and will be removed in a future release because its behavior is inconsistent with Python's range builtin. Instead, use torch.arange, which produces values in [start, end).\n",
- " zero_to_ten_deprecated = torch.range(0, 10)\n"
+ "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:2: UserWarning: torch.range is deprecated and will be removed in a future release because its behavior is inconsistent with Python's range builtin. Instead, use torch.arange, which produces values in [start, end).\n",
+ " \n"
]
},
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
- "execution_count": 20,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 19
}
],
"source": [
"# Use torch.arange(), torch.range() is deprecated \n",
"zero_to_ten_deprecated = torch.range(0, 10) # Note: this may return an error in the future\n",
- "zero_to_ten = torch.arange(0, 10)\n",
+ "\n",
+ "# Create a range of values 0 to 10\n",
+ "zero_to_ten = torch.arange(start=0, end=10, step=1)\n",
"zero_to_ten"
]
},
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Sometimes you might want one tensor of a certain type with the same shape as another tensor.\n",
+ "\n",
+ "For example, a tensor of all zeros with the same shape as a previous tensor. \n",
+ "\n",
+ "To do so you can use [`torch.zeros_like(input)`](https://pytorch.org/docs/stable/generated/torch.zeros_like.html) or [`torch.ones_like(input)`](https://pytorch.org/docs/1.9.1/generated/torch.ones_like.html) which return a tensor filled with zeros or ones in the same shape as the `input` respectively."
+ ],
+ "metadata": {
+ "id": "i-bXf0Ugbh-D"
+ }
+ },
{
"cell_type": "code",
- "execution_count": 19,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -817,37 +958,75 @@
],
"source": [
"# Can also create a tensor of zeros similar to another tensor\n",
- "ten_zeros = torch.zeros_like(zero_to_ten) # will have same shape\n",
+ "ten_zeros = torch.zeros_like(input=zero_to_ten) # will have same shape\n",
"ten_zeros"
]
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "huKZ6QlYTGe7"
+ },
"source": [
- "### Tensor datatypes"
+ "### Tensor datatypes\n",
+ "\n",
+ "There are many different [tensor datatypes available in PyTorch](https://pytorch.org/docs/stable/tensors.html#data-types).\n",
+ "\n",
+ "Some are specific for CPU and some are better for GPU.\n",
+ "\n",
+ "Getting to know which is which can take some time.\n",
+ "\n",
+ "Generally if you see `torch.cuda` anywhere, the tensor is being used for GPU (since Nvidia GPUs use a computing toolkit called CUDA).\n",
+ "\n",
+ "The most common type (and generally the default) is `torch.float32` or `torch.float`.\n",
+ "\n",
+ "This is referred to as \"32-bit floating point\".\n",
+ "\n",
+ "But there's also 16-bit floating point (`torch.float16` or `torch.half`) and 64-bit floating point (`torch.float64` or `torch.double`).\n",
+ "\n",
+ "And to confuse things even more there's also 8-bit, 16-bit, 32-bit and 64-bit integers.\n",
+ "\n",
+ "Plus more!\n",
+ "\n",
+ "> **Note:** An integer is a flat round number like `7` whereas a float has a decimal `7.0`.\n",
+ "\n",
+ "The reason for all of these is to do with **precision in computing**.\n",
+ "\n",
+ "Precision is the amount of detail used to describe a number.\n",
+ "\n",
+ "The higher the precision value (8, 16, 32), the more detail and hence data used to express a number.\n",
+ "\n",
+ "This matters in deep learning and numerical computing because you're making so many operations, the more detail you have to calculate on, the more compute you have to use.\n",
+ "\n",
+ "So lower precision datatypes are generally faster to compute on but sacrifice some performance on evaluation metrics like accuracy (faster to compute but less accurate).\n",
+ "\n",
+ "> **Resources:** \n",
+ " * See the [PyTorch documentation for a list of all available tensor datatypes](https://pytorch.org/docs/stable/tensors.html#data-types).\n",
+ " * Read the [Wikipedia page for an overview of what precision in computing](https://en.wikipedia.org/wiki/Precision_(computer_science)#:~:text=In%20computer%20science%2C%20the%20precision,used%20to%20express%20a%20value.) is.\n",
+ "\n",
+ "Let's see how to create some tensors with specific datatypes. We can do so using the `dtype` parameter."
]
},
{
"cell_type": "code",
- "execution_count": 21,
+ "execution_count": 20,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "q3MoGnpw9XaF",
- "outputId": "82963d1f-92ee-484b-cc1c-e4b7a4dda135"
+ "outputId": "5691e19c-071c-4e3b-804e-4b64b9dbbd14"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"(torch.Size([3]), torch.float32, device(type='cpu'))"
]
},
- "execution_count": 21,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 20
}
],
"source": [
@@ -860,31 +1039,48 @@
"float_32_tensor.shape, float_32_tensor.dtype, float_32_tensor.device"
]
},
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Aside from shape issues (tensor shapes don't match up), two of the other most common issues you'll come across in PyTorch are datatype and device issues.\n",
+ "\n",
+ "For example, one of tensors is `torch.float32` and the other is `torch.float16` (PyTorch often likes tensors to be the same format).\n",
+ "\n",
+ "Or one of your tensors is on the CPU and the other is on the GPU (PyTorch likes calculations between tensors to be on the same device).\n",
+ "\n",
+ "We'll see more of this device talk later on.\n",
+ "\n",
+ "For now let's create a tensor with `dtype=torch.float16`."
+ ],
+ "metadata": {
+ "id": "MhP8kzDfe_ty"
+ }
+ },
{
"cell_type": "code",
- "execution_count": 22,
+ "execution_count": 21,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PKSuajld_09s",
- "outputId": "00a90334-bd19-4759-eea9-6f92ec278e92"
+ "outputId": "53cb6928-d278-4c4a-d1f1-d3a0a5dc869f"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"torch.float16"
]
},
- "execution_count": 22,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 21
}
],
"source": [
"float_16_tensor = torch.tensor([3.0, 6.0, 9.0],\n",
- " dtype=torch.float16)\n",
+ " dtype=torch.float16) # torch.half would also work\n",
"\n",
"float_16_tensor.dtype"
]
@@ -895,12 +1091,21 @@
"id": "gUjkB2AX7Upz"
},
"source": [
- "## TK - Getting information from tensors"
+ "## Getting information from tensors\n",
+ "\n",
+ "Once you've created tensors (or someone else or a PyTorch module has created them for you), you might want to get some information from them.\n",
+ "\n",
+ "We've seen these before but three of the most common attributes you'll want to find out about tensors are:\n",
+ "* `shape` - what shape is the tensor? (some operations require specific shape rules)\n",
+ "* `dtype` - what datatype are the elements within the tensor stored in?\n",
+ "* `device` - what device is the tensor stored on? (usually GPU or CPU)\n",
+ "\n",
+ "Let's create a random tensor and find out details about it."
]
},
{
"cell_type": "code",
- "execution_count": 23,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -923,21 +1128,48 @@
}
],
"source": [
+ "# Create a tensor\n",
"some_tensor = torch.rand(3, 4)\n",
"\n",
+ "# Find out details about it\n",
"print(some_tensor)\n",
"print(f\"Shape of tensor: {some_tensor.shape}\")\n",
"print(f\"Datatype of tensor: {some_tensor.dtype}\")\n",
"print(f\"Device tensor is stored on: {some_tensor.device}\") # will default to CPU"
]
},
+ {
+ "cell_type": "markdown",
+ "source": [
+ "> **Note:** When you run into issues in PyTorch, it's very often one to do with one of the three attributes above. So when the error messages show up, sing yourself a little song called \"what, what, where\": \n",
+ " * \"*what shape are my tensors? what datatype are they and where are they stored? what shape, what datatype, where where where*\""
+ ],
+ "metadata": {
+ "id": "45K-E5uPg6cj"
+ }
+ },
{
"cell_type": "markdown",
"metadata": {
"id": "BdiWvoAi7UjL"
},
"source": [
- "## TK - Manipulating tensors (tensor operations)"
+ "## TK - Manipulating tensors (tensor operations)\n",
+ "\n",
+ "In deep learning, data (images, text, video, audio, protein structures, etc) gets represented as tensors.\n",
+ "\n",
+ "A model learns by investigating those tensors and performing a series of operations (could be 1,000,000s+) on tensors to create a representation of the patterns in the input data.\n",
+ "\n",
+ "These operations are often a wonderful dance between:\n",
+ "* Addition\n",
+ "* Substraction\n",
+ "* Multiplication (element-wise)\n",
+ "* Division\n",
+ "* Matrix multiplication\n",
+ "\n",
+ "And that's it. Sure there are a few more here and there but these are the basic building blocks of neural networks.\n",
+ "\n",
+ "Stacking these building blocks in the right way, you can create the most sophisticated of neural networks (just like lego!)."
]
},
{
@@ -946,9 +1178,77 @@
"id": "Sk_6Dd7L7Uce"
},
"source": [
- "### Basic operations"
+ "### Basic operations\n",
+ "\n",
+ "Let's start with a few of the fundamental operations, addition (`+`), subtraction (`-`), mutliplication (`*`).\n",
+ "\n",
+ "They work just as you think they would."
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "X71WpQoPD7a4",
+ "outputId": "758f937d-94bb-418e-cb10-628cb3744507"
+ },
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "tensor([11, 12, 13])"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 22
+ }
+ ],
+ "source": [
+ "# Create a tensor of values and add a number to it\n",
+ "tensor = torch.tensor([1, 2, 3])\n",
+ "tensor + 10"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "Sp4TlTWWEFeO",
+ "outputId": "7398166a-488f-4399-8406-4a9eb252b6ef"
+ },
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "tensor([10, 20, 30])"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 23
+ }
+ ],
+ "source": [
+ "# Multiply it by 10\n",
+ "tensor * 10"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Notice how the tensor values above didn't end up being `tensor([110, 120, 130])`, this is because the values inside the tensor don't change unless they're reassigned."
+ ],
+ "metadata": {
+ "id": "-1VEHnuRkn8Q"
+ }
+ },
{
"cell_type": "code",
"execution_count": 24,
@@ -956,26 +1256,35 @@
"colab": {
"base_uri": "https://localhost:8080/"
},
- "id": "X71WpQoPD7a4",
- "outputId": "f7eab2bf-ba9b-4612-9cfa-e8d80093cc20"
+ "id": "XuB1UjCIEJIA",
+ "outputId": "c6d55128-892a-4efa-e375-9ce45cd12ab2"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
- "tensor([11, 12, 13])"
+ "tensor([1, 2, 3])"
]
},
- "execution_count": 24,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 24
}
],
"source": [
- "tensor = torch.tensor([1, 2, 3])\n",
- "tensor + 10"
+ "# Tensors don't change unless reassigned\n",
+ "tensor"
]
},
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Let's subtract a number and this time we'll reassign the `tensor` variable. "
+ ],
+ "metadata": {
+ "id": "VYvqGpUTk1o6"
+ }
+ },
{
"cell_type": "code",
"execution_count": 25,
@@ -983,23 +1292,25 @@
"colab": {
"base_uri": "https://localhost:8080/"
},
- "id": "Sp4TlTWWEFeO",
- "outputId": "db58e2e0-fe7e-4670-f61a-a6ff00f33250"
+ "id": "U4iWKoLsENry",
+ "outputId": "347927f9-3303-4d30-e04d-77b736039c65"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
- "tensor([10, 20, 30])"
+ "tensor([-9, -8, -7])"
]
},
- "execution_count": 25,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 25
}
],
"source": [
- "tensor * 10"
+ "# Subtract and reassign\n",
+ "tensor = tensor - 10\n",
+ "tensor"
]
},
{
@@ -1009,100 +1320,56 @@
"colab": {
"base_uri": "https://localhost:8080/"
},
- "id": "XuB1UjCIEJIA",
- "outputId": "063b6c17-8d2f-4627-8e7d-932af58b0cd5"
+ "id": "tFgZY-PaFNXa",
+ "outputId": "1624c5a5-e52f-4594-c913-2a3afc507d44"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"tensor([1, 2, 3])"
]
},
- "execution_count": 26,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 26
}
],
"source": [
- "# Tensors don't change unless reassigned\n",
+ "# Add and reassign\n",
+ "tensor = tensor + 10\n",
"tensor"
]
},
+ {
+ "cell_type": "markdown",
+ "source": [
+ "PyTorch also has a bunch of built-in functions like [`torch.mul()`](https://pytorch.org/docs/stable/generated/torch.mul.html#torch.mul) (short for multiplcation) and [`torch.add()`](https://pytorch.org/docs/stable/generated/torch.add.html) to perform basic operations. "
+ ],
+ "metadata": {
+ "id": "CYXDoIOzk-6I"
+ }
+ },
{
"cell_type": "code",
"execution_count": 27,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "U4iWKoLsENry",
- "outputId": "c8751a66-fa34-40bd-db94-2a3c81ebc99a"
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "tensor([-9, -8, -7])"
- ]
- },
- "execution_count": 27,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "tensor = tensor - 10\n",
- "tensor"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "tFgZY-PaFNXa",
- "outputId": "160484d4-768b-4a30-f3a9-c5ae4ded8735"
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "tensor([1, 2, 3])"
- ]
- },
- "execution_count": 28,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "tensor = tensor + 10\n",
- "tensor"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 29,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "uVysdk3kFWbY",
- "outputId": "0c5f56c5-869b-4a05-819c-4467772ee62f"
+ "outputId": "4b50997c-f94d-4a34-9707-ee793c129a64"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"tensor([10, 20, 30])"
]
},
- "execution_count": 29,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 27
}
],
"source": [
@@ -1112,24 +1379,24 @@
},
{
"cell_type": "code",
- "execution_count": 30,
+ "execution_count": 28,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "IxuPJIpNFbqO",
- "outputId": "8c6df0c4-dc15-4540-8dde-1c54a867b986"
+ "outputId": "96724ed4-7462-46d3-c31a-41fbecb2713a"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"tensor([1, 2, 3])"
]
},
- "execution_count": 30,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 28
}
],
"source": [
@@ -1137,44 +1404,14 @@
"tensor"
]
},
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "S5v3RkR0F2Jq",
- "outputId": "9d7a6757-29c3-43e5-8389-f21907bcd89e"
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "tensor([1, 4, 9])"
- ]
- },
- "execution_count": 31,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# Element-wise multiplication\n",
- "tensor * tensor"
- ]
- },
{
"cell_type": "markdown",
- "metadata": {
- "id": "TT5fVuyu7q5z"
- },
"source": [
- "### Matrix multiplcation\n",
- "\n",
- "Neural networks are mostly matrix multiplcation - https://marksaroufim.substack.com/p/working-class-deep-learner\n",
- "\n"
- ]
+ "However, it's more common to use the operator symbols like `*` instead of `torch.mul()`"
+ ],
+ "metadata": {
+ "id": "70UNL33AlVQq"
+ }
},
{
"cell_type": "code",
@@ -1183,129 +1420,215 @@
"colab": {
"base_uri": "https://localhost:8080/"
},
- "id": "ZE7loucmDlEM",
- "outputId": "49dd001e-f472-43c9-cead-8fdb3d380689"
+ "id": "S5v3RkR0F2Jq",
+ "outputId": "315fd11d-e8b7-4476-9a2d-ef5859dc8a7a"
},
"outputs": [
{
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "tensor([1, 2, 3]) * tensor([1, 2, 3])\n",
+ "Equals: tensor([1, 4, 9])\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Element-wise multiplication (each element multiplies its equivalent, index 0->0, 1->1, 2->2)\n",
+ "print(tensor, \"*\", tensor)\n",
+ "print(\"Equals:\", tensor * tensor)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "TT5fVuyu7q5z"
+ },
+ "source": [
+ "### Matrix multiplication (is all you need)\n",
+ "\n",
+ "One of the most common operations in machine learning and deep learning algorithms (like neural networks) is [matrix multiplication](https://www.mathsisfun.com/algebra/matrix-multiplying.html).\n",
+ "\n",
+ "PyTorch implements matrix multiplication functionality in the [`torch.matmul()`](https://pytorch.org/docs/stable/generated/torch.matmul.html) method.\n",
+ "\n",
+ "The main two rules for matrix multiplication to remember are:\n",
+ "1. The **inner dimensions** must match:\n",
+ " * `(3, 2) @ (3, 2)` won't work\n",
+ " * `(2, 3) @ (3, 2)` will work\n",
+ " * `(3, 2) @ (2, 3)` will work\n",
+ "2. The resulting matrix has the shape of the **outer dimensions**:\n",
+ " * `(2, 3) @ (3, 2)` -> `(2, 2)`\n",
+ " * `(3, 2) @ (2, 3)` -> `(3, 3)`\n",
+ "\n",
+ "> **Note:** \"`@`\" in Python is the symbol for matrix multiplication.\n",
+ "\n",
+ "> **Resource:** You can see all of the rules for matrix multiplication using `torch.matmul()` [in the PyTorch documentation](https://pytorch.org/docs/stable/generated/torch.matmul.html).\n",
+ "\n",
+ "* TK - Neural networks are mostly matrix multiplication - https://marksaroufim.substack.com/p/working-class-deep-learner\n",
+ "* TK - image: matrix multiplication is all you need from Mark's post\n",
+ "\n",
+ "Let's create a tensor and perform element-wise multiplication and matrix multiplication on it.\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 43,
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "ZE7loucmDlEM",
+ "outputId": "b1b0cab8-d914-4e79-b841-ac13e4ab3fe2"
+ },
+ "outputs": [
+ {
+ "output_type": "execute_result",
"data": {
"text/plain": [
- "tensor([1, 2, 3])"
+ "torch.Size([3])"
]
},
- "execution_count": 32,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 43
}
],
"source": [
"import torch\n",
"tensor = torch.tensor([1, 2, 3])\n",
- "tensor"
+ "tensor.shape"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The difference between element-wise multiplication and matrix multiplication is the addition of values.\n",
+ "\n",
+ "For our `tensor` variable with values `[1, 2, 3]`:\n",
+ "\n",
+ "* **Element-wise** = `[1*1, 2*2, 3*3]` = `[1, 4, 9]`\n",
+ "* **Matrix multiplication** = `[1*1 + 2*2 + 3*3]` = `[14]`"
+ ],
+ "metadata": {
+ "id": "VUAZ3_b0vOKv"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Element-wise matrix mutlication\n",
+ "tensor * tensor"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "i42gkUeHvI_1",
+ "outputId": "92e58c3e-3c84-4392-c3dd-c8228a4dbba5"
+ },
+ "execution_count": 55,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "tensor([1, 4, 9])"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 55
+ }
]
},
{
"cell_type": "code",
- "execution_count": 33,
+ "execution_count": 56,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PvCBiiTTDk8y",
- "outputId": "6f4633ca-41af-48f7-a664-fd033a3633c1"
+ "outputId": "2bad2e1a-4515-4ae3-d743-92184fcdf15f"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"tensor(14)"
]
},
- "execution_count": 33,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 56
}
],
"source": [
+ "# Matrix multiplication\n",
"torch.matmul(tensor, tensor)"
]
},
{
"cell_type": "code",
- "execution_count": 34,
+ "execution_count": 52,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "m4E_pROBDk2r",
- "outputId": "80bdc547-e6c6-4047-d9f2-e88dc0a26f18"
+ "outputId": "18f5f1df-e208-49a1-ef30-b91cdace2067"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
"tensor(14)"
]
},
- "execution_count": 34,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 52
}
],
"source": [
+ "# Can also use the \"@\" symbol for matrix multiplication, though not recommended\n",
"tensor @ tensor"
]
},
{
- "cell_type": "code",
- "execution_count": 35,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "2u4IZnFSFxKQ",
- "outputId": "e11ee2aa-f22e-4c0d-bb82-7ec83dfd1878"
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "tensor(14)"
- ]
- },
- "execution_count": 35,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
+ "cell_type": "markdown",
"source": [
- "sum(tensor * tensor.T)"
- ]
+ "You can do matrix multiplication by hand but it's not recommended.\n",
+ "\n",
+ "The in-built `torch.matmul()` method is faster."
+ ],
+ "metadata": {
+ "id": "obbginUMv43A"
+ }
},
{
"cell_type": "code",
- "execution_count": 36,
+ "execution_count": 57,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6qMSaLOoJscL",
- "outputId": "debdac67-169b-4217-f654-f9b0fdfc2c87"
+ "outputId": "124df9a5-c34d-4a3c-e742-c18602fe919c"
},
"outputs": [
{
- "data": {
- "text/plain": [
- "tensor(14)"
- ]
- },
- "execution_count": 36,
- "metadata": {},
- "output_type": "execute_result"
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "CPU times: user 174 µs, sys: 27 µs, total: 201 µs\n",
+ "Wall time: 209 µs\n"
+ ]
}
],
"source": [
- "# By hand (avoid doing operations with for loops at all cost, they are computationally expensive)\n",
+ "%%time\n",
+ "# Matrix multiplication by hand \n",
+ "# (avoid doing operations with for loops at all cost, they are computationally expensive)\n",
"value = 0\n",
"for i in range(len(tensor)):\n",
" value += tensor[i] * tensor[i]\n",
@@ -1313,34 +1636,71 @@
]
},
{
- "cell_type": "markdown",
- "metadata": {},
+ "cell_type": "code",
"source": [
- "## One of the most common errors in deep learning\n",
+ "%%time\n",
+ "torch.matmul(tensor, tensor)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "vVWiKB0KwH74",
+ "outputId": "8288e7e4-2f09-4865-8f85-6c836872e4bd"
+ },
+ "execution_count": 58,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "CPU times: user 60 µs, sys: 9 µs, total: 69 µs\n",
+ "Wall time: 74.6 µs\n"
+ ]
+ },
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "tensor(14)"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 58
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "aJ4DDmo1TGe-"
+ },
+ "source": [
+ "## One of the most common errors in deep learning (shape errors)\n",
"\n",
"Because much of deep learning is multiplying and performing operations on matrices and matrices have a strict rule about what shapes and sizes can be combined, one of the most common errors you'll run into in deep learning is shape mismatches."
]
},
{
"cell_type": "code",
- "execution_count": 37,
+ "execution_count": 68,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
- "height": 196
+ "height": 201
},
"id": "rN5RcoD4Jo6y",
- "outputId": "459e3ab8-1233-43ac-d097-aa7ad10d8f58"
+ "outputId": "1d19bea9-4598-4fd2-92a1-a466e7bc90fb"
},
"outputs": [
{
- "ename": "RuntimeError",
- "evalue": "mat1 and mat2 shapes cannot be multiplied (3x2 and 3x2)",
"output_type": "error",
+ "ename": "RuntimeError",
+ "evalue": "ignored",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
- "\u001b[0;32m/tmp/ipykernel_327633/854767186.py\u001b[0m in \u001b[0;36m\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\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 8\u001b[0m [9, 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)"
]
}
@@ -1351,58 +1711,153 @@
" [3, 4],\n",
" [5, 6]], dtype=torch.float32)\n",
"\n",
- "tensor_B = torch.tensor([[7, 8],\n",
- " [9, 10], \n",
- " [11, 12]], dtype=torch.float32)\n",
+ "tensor_B = torch.tensor([[7, 10],\n",
+ " [8, 11], \n",
+ " [9, 12]], dtype=torch.float32)\n",
"\n",
"torch.matmul(tensor_A, tensor_B)"
]
},
+ {
+ "cell_type": "markdown",
+ "source": [
+ "We can make matrix multiplication work between `tensor_A` and `tensor_B` by making their inner dimensions match.\n",
+ "\n",
+ "One of the ways to do this is with a **transpose** (switch the dimensions of a given tensor).\n",
+ "\n",
+ "You can perform transposes in PyTorch using either:\n",
+ "* `torch.transpose(input, dim0, dim1)` - where `input` is the desired tensor to transpose and `dim0` and `dim1` are the dimensions to be swapped.\n",
+ "* `tensor.T` - where `tensor` is the desired tensor to transpose.\n",
+ "\n",
+ "Let's try the latter."
+ ],
+ "metadata": {
+ "id": "HNA6MZEFxWVt"
+ }
+ },
{
"cell_type": "code",
- "execution_count": 39,
+ "source": [
+ "# View tensor_A and tensor_B\n",
+ "print(tensor_A)\n",
+ "print(tensor_B)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "lUqgaANiy1wq",
+ "outputId": "01410652-26f7-4968-cd1e-49def89ae9e2"
+ },
+ "execution_count": 69,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "tensor([[1., 2.],\n",
+ " [3., 4.],\n",
+ " [5., 6.]])\n",
+ "tensor([[ 7., 10.],\n",
+ " [ 8., 11.],\n",
+ " [ 9., 12.]])\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# View tensor_A and tensor_B.T\n",
+ "print(tensor_A)\n",
+ "print(tensor_B.T)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "DveqxO7iy_Fi",
+ "outputId": "4d46ea43-c89c-42ab-8015-8321d1a29b06"
+ },
+ "execution_count": 70,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "tensor([[1., 2.],\n",
+ " [3., 4.],\n",
+ " [5., 6.]])\n",
+ "tensor([[ 7., 8., 9.],\n",
+ " [10., 11., 12.]])\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 77,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "35rEIu-NKtVE",
- "outputId": "b1672c80-9b01-4f97-db34-74403b8a9f40"
+ "outputId": "da3af25b-5753-46c0-dead-0b11fd56e1a4"
},
"outputs": [
{
- "data": {
- "text/plain": [
- "tensor([[ 23., 29., 35.],\n",
- " [ 53., 67., 81.],\n",
- " [ 83., 105., 127.]])"
- ]
- },
- "execution_count": 39,
- "metadata": {},
- "output_type": "execute_result"
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Original shapes: tensor_A = torch.Size([3, 2]), tensor_B = torch.Size([3, 2])\n",
+ "\n",
+ "New shapes: tensor_A = torch.Size([3, 2]) (same as above), tensor_B.T = torch.Size([2, 3])\n",
+ "\n",
+ "Multiplying: torch.Size([3, 2]) * torch.Size([2, 3]) <- inner dimensions match\n",
+ "\n",
+ "Output:\n",
+ "\n",
+ "tensor([[ 27., 30., 33.],\n",
+ " [ 61., 68., 75.],\n",
+ " [ 95., 106., 117.]])\n",
+ "\n",
+ "Output shape: torch.Size([3, 3])\n"
+ ]
}
],
"source": [
"# The operation works when tensor_B is transposed\n",
- "torch.matmul(tensor_A, tensor_B.T)"
+ "print(f\"Original shapes: tensor_A = {tensor_A.shape}, tensor_B = {tensor_B.shape}\\n\")\n",
+ "print(f\"New shapes: tensor_A = {tensor_A.shape} (same as above), tensor_B.T = {tensor_B.T.shape}\\n\")\n",
+ "print(f\"Multiplying: {tensor_A.shape} * {tensor_B.T.shape} <- inner dimensions match\\n\")\n",
+ "print(\"Output:\\n\")\n",
+ "output = torch.matmul(tensor_A, tensor_B.T)\n",
+ "print(output) \n",
+ "print(f\"\\nOutput shape: {output.shape}\")"
]
},
{
"cell_type": "code",
- "execution_count": 40,
- "metadata": {},
+ "execution_count": 72,
+ "metadata": {
+ "id": "x3rJvW_TTGe_",
+ "outputId": "be49e125-ce4a-488d-a0a9-d9a4d608479b",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
- "tensor([[ 23., 29., 35.],\n",
- " [ 53., 67., 81.],\n",
- " [ 83., 105., 127.]])"
+ "tensor([[ 27., 30., 33.],\n",
+ " [ 61., 68., 75.],\n",
+ " [ 95., 106., 117.]])"
]
},
- "execution_count": 40,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 72
}
],
"source": [
@@ -1410,41 +1865,55 @@
"torch.mm(tensor_A, tensor_B.T)"
]
},
+ {
+ "cell_type": "markdown",
+ "source": [
+ "UPTOHERE - insert matrix multiplication gif & continue explaining matrix multiplication \n",
+ "\n",
+ "* TK - matmul gif - \n",
+ "* TK - matmul graphic\n",
+ "\n"
+ ],
+ "metadata": {
+ "id": "bXKozI4T0hFi"
+ }
+ },
{
"cell_type": "code",
- "execution_count": 41,
+ "execution_count": 82,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "mC_MjKW1LX7T",
- "outputId": "78a3c4f6-ddb3-4bf9-f25f-241f6b436192"
+ "outputId": "cea3df2c-2262-4c62-cd71-a3d4dd2ea3af"
},
"outputs": [
{
+ "output_type": "execute_result",
"data": {
"text/plain": [
- "(tensor([[0.3035, 2.4169],\n",
- " [0.8981, 2.5015]], grad_fn=),\n",
- " torch.Size([2, 2]))"
+ "(tensor([[-0.3533, 0.7184, -0.4397, 1.3854, 0.1409, 0.8327],\n",
+ " [ 0.0798, 2.0294, -1.9019, 2.7338, 1.0537, 1.8275],\n",
+ " [ 0.5128, 3.3404, -3.3640, 4.0823, 1.9666, 2.8224]],\n",
+ " grad_fn=), torch.Size([3, 6]))"
]
},
- "execution_count": 41,
"metadata": {},
- "output_type": "execute_result"
+ "execution_count": 82
}
],
"source": [
"# This uses matrix mutliplcation...\n",
- "linear = torch.nn.Linear(in_features=3, out_features=2)\n",
+ "linear = torch.nn.Linear(in_features=2, out_features=6) # in_features = matches inner dimension, out_features = describes outer value\n",
"input = tensor_A\n",
- "output = linear(input.T)\n",
+ "output = linear(input)\n",
"output, output.shape"
]
},
{
"cell_type": "code",
- "execution_count": 42,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1482,7 +1951,7 @@
},
{
"cell_type": "code",
- "execution_count": 43,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1509,7 +1978,7 @@
},
{
"cell_type": "code",
- "execution_count": 44,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1538,7 +2007,7 @@
},
{
"cell_type": "code",
- "execution_count": 45,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1573,7 +2042,7 @@
},
{
"cell_type": "code",
- "execution_count": 46,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1601,7 +2070,7 @@
},
{
"cell_type": "code",
- "execution_count": 47,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1640,7 +2109,7 @@
},
{
"cell_type": "code",
- "execution_count": 48,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1667,7 +2136,7 @@
},
{
"cell_type": "code",
- "execution_count": 49,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1694,7 +2163,7 @@
},
{
"cell_type": "code",
- "execution_count": 50,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1736,7 +2205,7 @@
},
{
"cell_type": "code",
- "execution_count": 52,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1764,7 +2233,7 @@
},
{
"cell_type": "code",
- "execution_count": 53,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1792,8 +2261,11 @@
},
{
"cell_type": "code",
- "execution_count": 54,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "WDN2BNe5TGfB",
+ "outputId": "6e4df071-86db-43b7-a86f-607e30d2f5a9"
+ },
"outputs": [
{
"data": {
@@ -1815,8 +2287,11 @@
},
{
"cell_type": "code",
- "execution_count": 55,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "2DxURVvXTGfC",
+ "outputId": "f2710ddd-7caa-433f-eba6-32bc7c445908"
+ },
"outputs": [
{
"data": {
@@ -1837,7 +2312,7 @@
},
{
"cell_type": "code",
- "execution_count": 56,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1868,7 +2343,7 @@
},
{
"cell_type": "code",
- "execution_count": 57,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1896,7 +2371,7 @@
},
{
"cell_type": "code",
- "execution_count": 58,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -1924,8 +2399,11 @@
},
{
"cell_type": "code",
- "execution_count": 63,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "fCRGCX8DTGfC",
+ "outputId": "ca52f97f-b310-4731-df1f-afb7f65bd6fb"
+ },
"outputs": [
{
"data": {
@@ -1947,7 +2425,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "nEPqVL7fTGfC"
+ },
"source": [
"## TK - Indexing (selecting data from tensors)"
]
@@ -1955,7 +2435,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "id": "oSXzdxCQTGfD"
+ },
"outputs": [],
"source": [
"# Indexing goes outer to inner...\n",
@@ -1964,8 +2446,11 @@
},
{
"cell_type": "code",
- "execution_count": 64,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "zv_Z3IAzTGfD",
+ "outputId": "7dd2d749-7c2f-4fbf-f16b-b705ed24b770"
+ },
"outputs": [
{
"data": {
@@ -1988,8 +2473,11 @@
},
{
"cell_type": "code",
- "execution_count": 65,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "BuKrGOvBTGfD",
+ "outputId": "a73878e7-ef80-4da9-8d38-ac20a3cc2085"
+ },
"outputs": [
{
"data": {
@@ -2010,8 +2498,11 @@
},
{
"cell_type": "code",
- "execution_count": 66,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "gCT09pqeTGfD",
+ "outputId": "621490ff-773f-450f-99df-dad374819eb4"
+ },
"outputs": [
{
"data": {
@@ -2030,8 +2521,11 @@
},
{
"cell_type": "code",
- "execution_count": 67,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "dwDx_gMsTGfD",
+ "outputId": "9cde0d76-4254-476d-d287-287bbbb8f787"
+ },
"outputs": [
{
"data": {
@@ -2050,8 +2544,11 @@
},
{
"cell_type": "code",
- "execution_count": 68,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "xiw3_1E3TGfD",
+ "outputId": "3164c108-4d2a-44cf-b0cf-da84de63aea3"
+ },
"outputs": [
{
"data": {
@@ -2070,8 +2567,11 @@
},
{
"cell_type": "code",
- "execution_count": 69,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "XFVEgrKhTGfD",
+ "outputId": "24500ee4-f6d9-47f7-dbd9-1e24b5a9993f"
+ },
"outputs": [
{
"data": {
@@ -2090,7 +2590,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "L6vsBjE2TGfE"
+ },
"source": [
"## TK - Changing values\n",
"\n",
@@ -2102,7 +2604,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "id": "1rjbN9T2TGfE"
+ },
"outputs": [],
"source": [
"# TODO - add in example of scatter\n"
@@ -2119,7 +2623,7 @@
},
{
"cell_type": "code",
- "execution_count": 130,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -2149,7 +2653,7 @@
},
{
"cell_type": "code",
- "execution_count": 131,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -2177,7 +2681,7 @@
},
{
"cell_type": "code",
- "execution_count": 134,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -2207,7 +2711,7 @@
},
{
"cell_type": "code",
- "execution_count": 135,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -2250,8 +2754,11 @@
},
{
"cell_type": "code",
- "execution_count": 32,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "eSwxnwEbTGfF",
+ "outputId": "f0e7734a-0e53-429c-a234-f4e28d4af087"
+ },
"outputs": [
{
"data": {
@@ -2278,8 +2785,11 @@
},
{
"cell_type": "code",
- "execution_count": 40,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "sB6d1GfYTGfF",
+ "outputId": "6241f9c6-570a-4355-f92f-34a9f3608011"
+ },
"outputs": [
{
"data": {
@@ -2325,7 +2835,7 @@
},
{
"cell_type": "code",
- "execution_count": 70,
+ "execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
@@ -2353,9 +2863,10 @@
},
{
"cell_type": "code",
- "execution_count": 71,
+ "execution_count": null,
"metadata": {
- "id": "j92HBCKB7rYa"
+ "id": "j92HBCKB7rYa",
+ "outputId": "fe66f5be-bb9e-4f0e-e1b3-bc170bf64d07"
},
"outputs": [
{
@@ -2377,8 +2888,11 @@
},
{
"cell_type": "code",
- "execution_count": 72,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "MArsn0DFTGfG",
+ "outputId": "c47a1042-c4b8-435d-db61-e14de35d8b3a"
+ },
"outputs": [
{
"data": {
@@ -2413,9 +2927,10 @@
},
{
"cell_type": "code",
- "execution_count": 73,
+ "execution_count": null,
"metadata": {
- "id": "FhI3srFXEHfP"
+ "id": "FhI3srFXEHfP",
+ "outputId": "c7ca99dd-75dc-4a2a-edb8-65e49fc85ee0"
},
"outputs": [
{
@@ -2450,7 +2965,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "DxXeRKO0TGfG"
+ },
"source": [
"What if we wanted to move the tensor back to CPU?\n",
"\n",
@@ -2461,8 +2978,11 @@
},
{
"cell_type": "code",
- "execution_count": 74,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "3ChSLJgPTGfG",
+ "outputId": "b6f12556-d62a-48c2-9f49-2da7443a6f2f"
+ },
"outputs": [
{
"ename": "TypeError",
@@ -2483,7 +3003,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "LhymtkRDTGfG"
+ },
"source": [
"Instead, to get a tensor back to CPU and usable with NumPy we can use [`Tensor.cpu()`](https://pytorch.org/docs/stable/generated/torch.Tensor.cpu.html).\n",
"\n",
@@ -2492,8 +3014,11 @@
},
{
"cell_type": "code",
- "execution_count": 75,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "gN15s-NdTGfG",
+ "outputId": "81859bb0-2f72-45b8-b1f3-c7d25db8a52c"
+ },
"outputs": [
{
"data": {
@@ -2514,15 +3039,20 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "qyzNH5lrTGfH"
+ },
"source": [
"The above returns a copy of the GPU tensor in CPU memory so the original tensor is still on GPU."
]
},
{
"cell_type": "code",
- "execution_count": 76,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "S5u83PCRTGfH",
+ "outputId": "c3e44958-7d04-4d37-fe5f-d70918d05f7b"
+ },
"outputs": [
{
"data": {
@@ -2541,7 +3071,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "LAzUIcXcTGfH"
+ },
"source": [
"You can also use `detach()` to cut the connection between the tensor on GPU and CPU.\n",
"\n",
@@ -2552,8 +3084,11 @@
},
{
"cell_type": "code",
- "execution_count": 78,
- "metadata": {},
+ "execution_count": null,
+ "metadata": {
+ "id": "SiFK3TibTGfH",
+ "outputId": "2c1f969d-73de-4e6b-8408-e74621a18b29"
+ },
"outputs": [
{
"data": {
@@ -2585,7 +3120,9 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "id": "xlmBpnuPTGfH"
+ },
"source": [
"## TK - Exercises\n",
"## TK - Extra-curriculum\n",
@@ -2596,17 +3133,20 @@
},
{
"cell_type": "markdown",
- "metadata": {},
- "source": []
+ "metadata": {
+ "id": "DAeNflY9TGfI"
+ },
+ "source": [
+ ""
+ ]
}
],
"metadata": {
"colab": {
- "authorship_tag": "ABX9TyPOZajF83061txEjr/v7czv",
"collapsed_sections": [],
- "include_colab_link": true,
"name": "00_pytorch_fundamentals.ipynb",
- "provenance": []
+ "provenance": [],
+ "include_colab_link": true
},
"interpreter": {
"hash": "3fbe1355223f7b2ffc113ba3ade6a2b520cadace5d5ec3e828c83ce02eb221bf"
@@ -2630,4 +3170,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
-}
+}
\ No newline at end of file