diff --git a/00_pytorch_fundamentals.ipynb b/00_pytorch_fundamentals.ipynb index 91d1b8c..d7850c4 100644 --- a/00_pytorch_fundamentals.ipynb +++ b/00_pytorch_fundamentals.ipynb @@ -3,8 +3,8 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", - "id": "view-in-github" + "id": "view-in-github", + "colab_type": "text" }, "source": [ "\"Open" @@ -96,7 +96,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", @@ -107,6 +107,7 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" @@ -115,9 +116,8 @@ "'1.10.0+cu111'" ] }, - "execution_count": 1, "metadata": {}, - "output_type": "execute_result" + "execution_count": 1 } ], "source": [ @@ -184,7 +184,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -194,14 +194,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor(7)" ] }, - "execution_count": 2, "metadata": {}, - "output_type": "execute_result" + "execution_count": 2 } ], "source": [ @@ -225,7 +225,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -235,14 +235,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "0" ] }, - "execution_count": 3, "metadata": {}, - "output_type": "execute_result" + "execution_count": 3 } ], "source": [ @@ -264,7 +264,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -274,14 +274,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "7" ] }, - "execution_count": 4, "metadata": {}, - "output_type": "execute_result" + "execution_count": 4 } ], "source": [ @@ -306,7 +306,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -316,14 +316,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([7, 7])" ] }, - "execution_count": 5, "metadata": {}, - "output_type": "execute_result" + "execution_count": 5 } ], "source": [ @@ -345,7 +345,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -355,14 +355,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "1" ] }, - "execution_count": 6, "metadata": {}, - "output_type": "execute_result" + "execution_count": 6 } ], "source": [ @@ -391,24 +391,24 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { + "id": "6zREV1bDTGe2", + "outputId": "2a6e7ceb-7eb2-422b-b006-2c6e4825272f", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "6zREV1bDTGe2", - "outputId": "2a6e7ceb-7eb2-422b-b006-2c6e4825272f" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "torch.Size([2])" ] }, - "execution_count": 7, "metadata": {}, - "output_type": "execute_result" + "execution_count": 7 } ], "source": [ @@ -429,7 +429,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -439,15 +439,15 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([[ 7, 8],\n", " [ 9, 10]])" ] }, - "execution_count": 8, "metadata": {}, - "output_type": "execute_result" + "execution_count": 8 } ], "source": [ @@ -469,7 +469,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -479,14 +479,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "2" ] }, - "execution_count": 9, "metadata": {}, - "output_type": "execute_result" + "execution_count": 9 } ], "source": [ @@ -507,24 +507,24 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { + "id": "_TL26I31TGe3", + "outputId": "f05ec0b6-0bc1-4381-9474-56cbe6c67139", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "_TL26I31TGe3", - "outputId": "f05ec0b6-0bc1-4381-9474-56cbe6c67139" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "torch.Size([2, 2])" ] }, - "execution_count": 10, "metadata": {}, - "output_type": "execute_result" + "execution_count": 10 } ], "source": [ @@ -544,7 +544,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -554,6 +554,7 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([[[1, 2, 3],\n", @@ -561,9 +562,8 @@ " [2, 4, 5]]])" ] }, - "execution_count": 11, "metadata": {}, - "output_type": "execute_result" + "execution_count": 11 } ], "source": [ @@ -593,7 +593,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -603,14 +603,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "3" ] }, - "execution_count": 12, "metadata": {}, - "output_type": "execute_result" + "execution_count": 12 } ], "source": [ @@ -629,24 +629,24 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": { + "id": "hdVv4iNRTGe5", + "outputId": "d8ac706c-020b-4926-b145-d44e41f35e90", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "hdVv4iNRTGe5", - "outputId": "d8ac706c-020b-4926-b145-d44e41f35e90" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "torch.Size([1, 3, 3])" ] }, - "execution_count": 13, "metadata": {}, - "output_type": "execute_result" + "execution_count": 13 } ], "source": [ @@ -715,7 +715,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -725,6 +725,7 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(tensor([[0.4090, 0.2527, 0.8699, 0.2002],\n", @@ -732,9 +733,8 @@ " [0.2281, 0.0345, 0.6734, 0.3866]]), torch.float32)" ] }, - "execution_count": 14, "metadata": {}, - "output_type": "execute_result" + "execution_count": 14 } ], "source": [ @@ -756,7 +756,11 @@ }, { "cell_type": "code", - "execution_count": 15, + "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/" @@ -764,22 +768,18 @@ "id": "xMF_NUp3Ym__", "outputId": "8346b853-0b1e-481a-d9ee-a410ee21bab0" }, + "execution_count": null, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(torch.Size([224, 224, 3]), 3)" ] }, - "execution_count": 15, "metadata": {}, - "output_type": "execute_result" + "execution_count": 15 } - ], - "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" ] }, { @@ -801,7 +801,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -811,6 +811,7 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(tensor([[0., 0., 0., 0.],\n", @@ -818,9 +819,8 @@ " [0., 0., 0., 0.]]), torch.float32)" ] }, - "execution_count": 16, "metadata": {}, - "output_type": "execute_result" + "execution_count": 16 } ], "source": [ @@ -831,25 +831,26 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "WDQBZJRUZWTN" - }, "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, + "execution_count": null, "metadata": { + "id": "HRe6sSXiTGe6", + "outputId": "3f45b0b8-7f65-423d-c664-f5b5f7866fd2", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "HRe6sSXiTGe6", - "outputId": "3f45b0b8-7f65-423d-c664-f5b5f7866fd2" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(tensor([[1., 1., 1., 1.],\n", @@ -857,9 +858,8 @@ " [1., 1., 1., 1.]]), torch.float32)" ] }, - "execution_count": 17, "metadata": {}, - "output_type": "execute_result" + "execution_count": 17 } ], "source": [ @@ -870,26 +870,6 @@ }, { "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Note**: We've seen how to use `torch.rand`, `torch.zeros`, and `torch.ones` with the `size` parameter, but we can also call them without setting an explicit `size` parameter and just pass the shape we want as integers. For example:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "another_random_tensor = torch.rand(224, 224, 3)\n", - "random_image_size_tensor.shape, random_image_size_tensor.ndim" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "hib1NYrSarL2" - }, "source": [ "### Creating a range and tensors like\n", "\n", @@ -903,11 +883,14 @@ "* `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": 18, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -917,22 +900,22 @@ }, "outputs": [ { - "name": "stderr", "output_type": "stream", + "name": "stderr", "text": [ "/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": 18, "metadata": {}, - "output_type": "execute_result" + "execution_count": 18 } ], "source": [ @@ -946,20 +929,20 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "i-bXf0Ugbh-D" - }, "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/" @@ -969,14 +952,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])" ] }, - "execution_count": 19, "metadata": {}, - "output_type": "execute_result" + "execution_count": 19 } ], "source": [ @@ -1025,14 +1008,14 @@ "\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", + " * Read the [Wikipedia page for an overview of what precision in computing](https://en.wikipedia.org/wiki/Precision_(computer_science)) 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": 20, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1042,14 +1025,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(torch.Size([3]), torch.float32, device(type='cpu'))" ] }, - "execution_count": 20, "metadata": {}, - "output_type": "execute_result" + "execution_count": 20 } ], "source": [ @@ -1064,9 +1047,6 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "MhP8kzDfe_ty" - }, "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", @@ -1077,11 +1057,14 @@ "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": 21, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1091,14 +1074,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "torch.float16" ] }, - "execution_count": 21, "metadata": {}, - "output_type": "execute_result" + "execution_count": 21 } ], "source": [ @@ -1128,7 +1111,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1138,8 +1121,8 @@ }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "tensor([[0.7799, 0.8140, 0.0893, 0.2062],\n", " [0.7525, 0.3845, 0.8207, 0.4587],\n", @@ -1163,13 +1146,13 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "45K-E5uPg6cj" - }, "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", @@ -1210,7 +1193,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1220,14 +1203,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([11, 12, 13])" ] }, - "execution_count": 23, "metadata": {}, - "output_type": "execute_result" + "execution_count": 23 } ], "source": [ @@ -1238,7 +1221,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1248,14 +1231,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([10, 20, 30])" ] }, - "execution_count": 24, "metadata": {}, - "output_type": "execute_result" + "execution_count": 24 } ], "source": [ @@ -1265,16 +1248,16 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "-1VEHnuRkn8Q" - }, "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": 25, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1284,14 +1267,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([1, 2, 3])" ] }, - "execution_count": 25, "metadata": {}, - "output_type": "execute_result" + "execution_count": 25 } ], "source": [ @@ -1301,16 +1284,16 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "VYvqGpUTk1o6" - }, "source": [ "Let's subtract a number and this time we'll reassign the `tensor` variable. " - ] + ], + "metadata": { + "id": "VYvqGpUTk1o6" + } }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1320,14 +1303,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([-9, -8, -7])" ] }, - "execution_count": 26, "metadata": {}, - "output_type": "execute_result" + "execution_count": 26 } ], "source": [ @@ -1338,7 +1321,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1348,14 +1331,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([1, 2, 3])" ] }, - "execution_count": 27, "metadata": {}, - "output_type": "execute_result" + "execution_count": 27 } ], "source": [ @@ -1366,16 +1349,16 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "CYXDoIOzk-6I" - }, "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": 28, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1385,24 +1368,24 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([10, 20, 30])" ] }, - "execution_count": 28, "metadata": {}, - "output_type": "execute_result" + "execution_count": 28 } ], "source": [ "# Can also use torch functions\n", - "torch.mul(tensor, 10)" + "torch.multiply(tensor, 10)" ] }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1412,14 +1395,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([1, 2, 3])" ] }, - "execution_count": 29, "metadata": {}, - "output_type": "execute_result" + "execution_count": 29 } ], "source": [ @@ -1429,16 +1412,16 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "70UNL33AlVQq" - }, "source": [ "However, it's more common to use the operator symbols like `*` instead of `torch.mul()`" - ] + ], + "metadata": { + "id": "70UNL33AlVQq" + } }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1448,8 +1431,8 @@ }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "tensor([1, 2, 3]) * tensor([1, 2, 3])\n", "Equals: tensor([1, 4, 9])\n" @@ -1493,7 +1476,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1503,26 +1486,24 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "torch.Size([3])" ] }, - "execution_count": 31, "metadata": {}, - "output_type": "execute_result" + "execution_count": 31 } ], "source": [ + "import torch\n", "tensor = torch.tensor([1, 2, 3])\n", "tensor.shape" ] }, { "cell_type": "markdown", - "metadata": { - "id": "VUAZ3_b0vOKv" - }, "source": [ "The difference between element-wise multiplication and matrix multiplication is the addition of values.\n", "\n", @@ -1532,11 +1513,17 @@ "| ----- | ----- | ----- |\n", "| **Element-wise multiplication** | `[1*1, 2*2, 3*3]` = `[1, 4, 9]` | `tensor * tensor` |\n", "| **Matrix multiplication** | `[1*1 + 2*2 + 3*3]` = `[14]` | `tensor.matmul(tensor)` |\n" - ] + ], + "metadata": { + "id": "VUAZ3_b0vOKv" + } }, { "cell_type": "code", - "execution_count": 32, + "source": [ + "# Element-wise matrix mutlication\n", + "tensor * tensor" + ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1544,26 +1531,23 @@ "id": "i42gkUeHvI_1", "outputId": "18a630ce-bb56-4c40-81b4-9fdbb2ed7a4f" }, + "execution_count": null, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([1, 4, 9])" ] }, - "execution_count": 32, "metadata": {}, - "output_type": "execute_result" + "execution_count": 32 } - ], - "source": [ - "# Element-wise matrix mutlication\n", - "tensor * tensor" ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1573,14 +1557,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor(14)" ] }, - "execution_count": 33, "metadata": {}, - "output_type": "execute_result" + "execution_count": 33 } ], "source": [ @@ -1590,7 +1574,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1600,14 +1584,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor(14)" ] }, - "execution_count": 34, "metadata": {}, - "output_type": "execute_result" + "execution_count": 34 } ], "source": [ @@ -1617,18 +1601,18 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "obbginUMv43A" - }, "source": [ "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": 35, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1638,8 +1622,8 @@ }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "CPU times: user 146 µs, sys: 38 µs, total: 184 µs\n", "Wall time: 227 µs\n" @@ -1658,7 +1642,10 @@ }, { "cell_type": "code", - "execution_count": 36, + "source": [ + "%%time\n", + "torch.matmul(tensor, tensor)" + ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1666,29 +1653,26 @@ "id": "vVWiKB0KwH74", "outputId": "fce58235-5c09-49ec-f34b-a90e5640281e" }, + "execution_count": null, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "CPU times: user 27 µs, sys: 7 µs, total: 34 µs\n", "Wall time: 36.7 µs\n" ] }, { + "output_type": "execute_result", "data": { "text/plain": [ "tensor(14)" ] }, - "execution_count": 36, "metadata": {}, - "output_type": "execute_result" + "execution_count": 36 } - ], - "source": [ - "%%time\n", - "torch.matmul(tensor, tensor)" ] }, { @@ -1704,7 +1688,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", @@ -1715,9 +1699,9 @@ }, "outputs": [ { + "output_type": "error", "ename": "RuntimeError", "evalue": "ignored", - "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", @@ -1741,9 +1725,6 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "HNA6MZEFxWVt" - }, "source": [ "We can make matrix multiplication work between `tensor_A` and `tensor_B` by making their inner dimensions match.\n", "\n", @@ -1754,11 +1735,18 @@ "* `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": 38, + "source": [ + "# View tensor_A and tensor_B\n", + "print(tensor_A)\n", + "print(tensor_B)" + ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1766,10 +1754,11 @@ "id": "lUqgaANiy1wq", "outputId": "e48bbf0c-8008-434e-d372-caa658b2f36b" }, + "execution_count": null, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "tensor([[1., 2.],\n", " [3., 4.],\n", @@ -1779,16 +1768,15 @@ " [ 9., 12.]])\n" ] } - ], - "source": [ - "# View tensor_A and tensor_B\n", - "print(tensor_A)\n", - "print(tensor_B)" ] }, { "cell_type": "code", - "execution_count": 39, + "source": [ + "# View tensor_A and tensor_B.T\n", + "print(tensor_A)\n", + "print(tensor_B.T)" + ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1796,10 +1784,11 @@ "id": "DveqxO7iy_Fi", "outputId": "1bd2e85b-ea4d-4948-c408-8eb46ef3534c" }, + "execution_count": null, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "tensor([[1., 2.],\n", " [3., 4.],\n", @@ -1808,16 +1797,11 @@ " [10., 11., 12.]])\n" ] } - ], - "source": [ - "# View tensor_A and tensor_B.T\n", - "print(tensor_A)\n", - "print(tensor_B.T)" ] }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1827,8 +1811,8 @@ }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Original shapes: tensor_A = torch.Size([3, 2]), tensor_B = torch.Size([3, 2])\n", "\n", @@ -1850,7 +1834,7 @@ "# The operation works when tensor_B is transposed\n", "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(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", @@ -1859,25 +1843,26 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "MfcFEqfLjN24" - }, "source": [ "You can also use [`torch.mm()`](https://pytorch.org/docs/stable/generated/torch.mm.html) which is a short for `torch.matmul()`." - ] + ], + "metadata": { + "id": "MfcFEqfLjN24" + } }, { "cell_type": "code", - "execution_count": 41, + "execution_count": null, "metadata": { + "id": "x3rJvW_TTGe_", + "outputId": "2c501972-20bf-4a83-ad4a-b5f1b2424097", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "x3rJvW_TTGe_", - "outputId": "2c501972-20bf-4a83-ad4a-b5f1b2424097" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([[ 27., 30., 33.],\n", @@ -1885,9 +1870,8 @@ " [ 95., 106., 117.]])" ] }, - "execution_count": 41, "metadata": {}, - "output_type": "execute_result" + "execution_count": 41 } ], "source": [ @@ -1897,9 +1881,6 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "bXKozI4T0hFi" - }, "source": [ "Without the transpose, the rules of matrix mulitplication aren't fulfilled and we get an error like above.\n", "\n", @@ -1911,13 +1892,13 @@ "\n", "> **Note:** A matrix multiplication like this is also referred to as the [**dot product**](https://www.mathsisfun.com/algebra/vectors-dot-product.html) of two matrices.\n", "\n" - ] + ], + "metadata": { + "id": "bXKozI4T0hFi" + } }, { "cell_type": "markdown", - "metadata": { - "id": "hA64Z4DmkB31" - }, "source": [ "Neural networks are full of matrix multiplications and dot products.\n", "\n", @@ -1941,11 +1922,14 @@ "Try changing the values of `in_features` and `out_features` below and see what happens.\n", "\n", "Do you notice anything to do with the shapes?" - ] + ], + "metadata": { + "id": "hA64Z4DmkB31" + } }, { "cell_type": "code", - "execution_count": 42, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1955,8 +1939,8 @@ }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Input shape: torch.Size([3, 2])\n", "\n", @@ -1984,18 +1968,15 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "zIGrP5j1pN7j" - }, "source": [ "> **Question:** What happens if you change `in_features` from 2 to 3 above? Does it error? How could you change the shape of the input (`x`) to accomodate to the error? Hint: what did we have to do to `tensor_B` above?" - ] + ], + "metadata": { + "id": "zIGrP5j1pN7j" + } }, { "cell_type": "markdown", - "metadata": { - "id": "EPNF0nMWoGEj" - }, "source": [ "If you've never done it before, matrix multiplication can be a confusing topic at first.\n", "\n", @@ -2006,7 +1987,10 @@ "![matrix multiplication is all you need](https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/00_matrix_multiplication_is_all_you_need.jpeg)\n", "\n", "*When you start digging into neural network layers and building your own, you'll find matrix multiplications everywhere. **Source:** https://marksaroufim.substack.com/p/working-class-deep-learner*" - ] + ], + "metadata": { + "id": "EPNF0nMWoGEj" + } }, { "cell_type": "markdown", @@ -2026,7 +2010,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2036,14 +2020,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])" ] }, - "execution_count": 43, "metadata": {}, - "output_type": "execute_result" + "execution_count": 43 } ], "source": [ @@ -2054,16 +2038,16 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "-J-wfMdlsEco" - }, "source": [ "Now let's perform some aggregation." - ] + ], + "metadata": { + "id": "-J-wfMdlsEco" + } }, { "cell_type": "code", - "execution_count": 44, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2073,8 +2057,8 @@ }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Minimum: 0\n", "Maximum: 90\n", @@ -2093,18 +2077,18 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "JHoKpsg3sKQE" - }, "source": [ "> **Note:** You may find some methods such as `torch.mean()` require tensors to be in `torch.float32` (the most common) or another specific datatype, otherwise the operation will fail. \n", "\n", "You can also do the same as above with `torch` methods." - ] + ], + "metadata": { + "id": "JHoKpsg3sKQE" + } }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2114,14 +2098,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(tensor(90), tensor(0), tensor(45.), tensor(450))" ] }, - "execution_count": 45, "metadata": {}, - "output_type": "execute_result" + "execution_count": 45 } ], "source": [ @@ -2143,7 +2127,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2153,8 +2137,8 @@ }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Tensor: tensor([10, 20, 30, 40, 50, 60, 70, 80, 90])\n", "Index where max value occurs: 8\n", @@ -2193,7 +2177,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2203,14 +2187,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "torch.float32" ] }, - "execution_count": 47, "metadata": {}, - "output_type": "execute_result" + "execution_count": 47 } ], "source": [ @@ -2221,17 +2205,17 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "jR30FHEc92of" - }, "source": [ "Now we'll create another tensor the same as before but change its datatype to `torch.float16`.\n", "\n" - ] + ], + "metadata": { + "id": "jR30FHEc92of" + } }, { "cell_type": "code", - "execution_count": 48, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2241,14 +2225,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([10., 20., 30., 40., 50., 60., 70., 80., 90.], dtype=torch.float16)" ] }, - "execution_count": 48, "metadata": {}, - "output_type": "execute_result" + "execution_count": 48 } ], "source": [ @@ -2259,16 +2243,16 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "ndVlKJZ4-7_5" - }, "source": [ "And we can do something similar to make a `torch.int8` tensor." - ] + ], + "metadata": { + "id": "ndVlKJZ4-7_5" + } }, { "cell_type": "code", - "execution_count": 49, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2278,14 +2262,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([10, 20, 30, 40, 50, 60, 70, 80, 90], dtype=torch.int8)" ] }, - "execution_count": 49, "metadata": {}, - "output_type": "execute_result" + "execution_count": 49 } ], "source": [ @@ -2296,14 +2280,14 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "44GxVabar-xe" - }, "source": [ "> **Note:** Different datatypes can be confusing to begin with. But think of it like this, the lower the number (e.g. 32, 16, 8), the less precise a computer stores the value. And with a lower amount of storage, this generally results in faster computation and a smaller overall model. Mobile-based neural networks often operate with 8-bit integers, smaller and faster to run but less accurate than their float32 counterparts. For more on this, I'd read up about [precision in computing](https://en.wikipedia.org/wiki/Precision_(computer_science)).\n", "\n", "> **Exercise:** So far we've covered a fair few tensor methods but there's a bunch more in the [`torch.Tensor` documentation](https://pytorch.org/docs/stable/tensors.html), I'd recommend spending 10-minutes scrolling through and looking into any that catch your eye. Click on them and then write them out in code yourself to see what happens." - ] + ], + "metadata": { + "id": "44GxVabar-xe" + } }, { "cell_type": "markdown", @@ -2337,7 +2321,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2347,34 +2331,35 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(tensor([1., 2., 3., 4., 5., 6., 7.]), torch.Size([7]))" ] }, - "execution_count": 50, "metadata": {}, - "output_type": "execute_result" + "execution_count": 50 } ], "source": [ "# Create a tensor\n", + "import torch\n", "x = torch.arange(1., 8.)\n", "x, x.shape" ] }, { "cell_type": "markdown", - "metadata": { - "id": "3_VarMO9CoT8" - }, "source": [ "Now let's add an extra dimension with `torch.reshape()`. " - ] + ], + "metadata": { + "id": "3_VarMO9CoT8" + } }, { "cell_type": "code", - "execution_count": 51, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2384,14 +2369,14 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(tensor([[1., 2., 3., 4., 5., 6., 7.]]), torch.Size([1, 7]))" ] }, - "execution_count": 51, "metadata": {}, - "output_type": "execute_result" + "execution_count": 51 } ], "source": [ @@ -2402,33 +2387,33 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "tig5xm0jCxuU" - }, "source": [ "We can also change the view with `torch.view()`." - ] + ], + "metadata": { + "id": "tig5xm0jCxuU" + } }, { "cell_type": "code", - "execution_count": 52, + "execution_count": null, "metadata": { + "id": "WDN2BNe5TGfB", + "outputId": "3df1b0d6-2548-4ecc-ca25-0c4e28a6e536", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "WDN2BNe5TGfB", - "outputId": "3df1b0d6-2548-4ecc-ca25-0c4e28a6e536" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(tensor([[1., 2., 3., 4., 5., 6., 7.]]), torch.Size([1, 7]))" ] }, - "execution_count": 52, "metadata": {}, - "output_type": "execute_result" + "execution_count": 52 } ], "source": [ @@ -2440,35 +2425,35 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "m8joAaUEC2NX" - }, "source": [ "Remember though, changing the view of a tensor with `torch.view()` really only creates a new view of the *same* tensor.\n", "\n", "So changing the view changes the original tensor too. " - ] + ], + "metadata": { + "id": "m8joAaUEC2NX" + } }, { "cell_type": "code", - "execution_count": 53, + "execution_count": null, "metadata": { + "id": "2DxURVvXTGfC", + "outputId": "668d194d-dd0a-4db1-da00-9c3fd8849186", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "2DxURVvXTGfC", - "outputId": "668d194d-dd0a-4db1-da00-9c3fd8849186" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(tensor([[5., 2., 3., 4., 5., 6., 7.]]), tensor([5., 2., 3., 4., 5., 6., 7.]))" ] }, - "execution_count": 53, "metadata": {}, - "output_type": "execute_result" + "execution_count": 53 } ], "source": [ @@ -2479,16 +2464,16 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "YxnqDBlpDDJ_" - }, "source": [ "If we wanted to stack our new tensor on top of itself five times, we could do so with `torch.stack()`." - ] + ], + "metadata": { + "id": "YxnqDBlpDDJ_" + } }, { "cell_type": "code", - "execution_count": 54, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2498,6 +2483,7 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([[5., 2., 3., 4., 5., 6., 7.],\n", @@ -2506,9 +2492,8 @@ " [5., 2., 3., 4., 5., 6., 7.]])" ] }, - "execution_count": 54, "metadata": {}, - "output_type": "execute_result" + "execution_count": 54 } ], "source": [ @@ -2519,18 +2504,18 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "ET56QzNHDuOI" - }, "source": [ "How about removing all single dimensions from a tensor?\n", "\n", "To do so you can use `torch.squeeze()` (I remember this as *squeezing* the tensor to only have dimensions over 1)." - ] + ], + "metadata": { + "id": "ET56QzNHDuOI" + } }, { "cell_type": "code", - "execution_count": 55, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2540,8 +2525,8 @@ }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Previous tensor: tensor([[5., 2., 3., 4., 5., 6., 7.]])\n", "Previous shape: torch.Size([1, 7])\n", @@ -2563,16 +2548,16 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "acjDLk8WD8NC" - }, "source": [ "And to do the reverse of `torch.squeeze()` you can use `torch.unsqueeze()` to add a dimension value of 1 at a specific index." - ] + ], + "metadata": { + "id": "acjDLk8WD8NC" + } }, { "cell_type": "code", - "execution_count": 56, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2582,8 +2567,8 @@ }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Previous tensor: tensor([5., 2., 3., 4., 5., 6., 7.])\n", "Previous shape: torch.Size([7])\n", @@ -2605,27 +2590,27 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "R9DuJzXgFbM5" - }, "source": [ "You can also rearrange the order of axes values with `torch.permute(input, dims)`, where the `input` gets turned into a *view* with new `dims`." - ] + ], + "metadata": { + "id": "R9DuJzXgFbM5" + } }, { "cell_type": "code", - "execution_count": 57, + "execution_count": null, "metadata": { + "id": "fCRGCX8DTGfC", + "outputId": "6853328b-a1cf-4470-f366-106a231a189c", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "fCRGCX8DTGfC", - "outputId": "6853328b-a1cf-4470-f366-106a231a189c" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Previous shape: torch.Size([224, 224, 3])\n", "New shape: torch.Size([3, 224, 224])\n" @@ -2645,12 +2630,12 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "06LKaFemGBoE" - }, "source": [ "> **Note**: Because permuting returns a *view* (shares the same data as the original), the values in the permuted tensor will be the same as the original tensor and if you change the values in the view, it will change the values of the original." - ] + ], + "metadata": { + "id": "06LKaFemGBoE" + } }, { "cell_type": "markdown", @@ -2669,16 +2654,17 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": null, "metadata": { + "id": "oSXzdxCQTGfD", "colab": { "base_uri": "https://localhost:8080/" }, - "id": "oSXzdxCQTGfD", "outputId": "05a72c08-5f8c-433a-cd31-46065686f825" }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(tensor([[[1, 2, 3],\n", @@ -2686,40 +2672,40 @@ " [7, 8, 9]]]), torch.Size([1, 3, 3]))" ] }, - "execution_count": 58, "metadata": {}, - "output_type": "execute_result" + "execution_count": 58 } ], "source": [ "# Create a tensor \n", + "import torch\n", "x = torch.arange(1, 10).reshape(1, 3, 3)\n", "x, x.shape" ] }, { "cell_type": "markdown", - "metadata": { - "id": "xQG5krnKG43B" - }, "source": [ "Indexing values goes outer dimension -> inner dimension (check out the square brackets)." - ] + ], + "metadata": { + "id": "xQG5krnKG43B" + } }, { "cell_type": "code", - "execution_count": 59, + "execution_count": null, "metadata": { + "id": "zv_Z3IAzTGfD", + "outputId": "cf6c0936-7600-4af4-9b6f-f6b8ac9b4c05", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "zv_Z3IAzTGfD", - "outputId": "cf6c0936-7600-4af4-9b6f-f6b8ac9b4c05" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "First square bracket:\n", "tensor([[1, 2, 3],\n", @@ -2739,33 +2725,33 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "XaLjaIFxHe89" - }, "source": [ "You can also use `:` to specify \"all values in this dimension\" and then use a comma (`,`) to add another dimension." - ] + ], + "metadata": { + "id": "XaLjaIFxHe89" + } }, { "cell_type": "code", - "execution_count": 60, + "execution_count": null, "metadata": { + "id": "gCT09pqeTGfD", + "outputId": "a91f9b73-f8f0-476a-9c69-fcd03b042f6b", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "gCT09pqeTGfD", - "outputId": "a91f9b73-f8f0-476a-9c69-fcd03b042f6b" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([[1, 2, 3]])" ] }, - "execution_count": 60, "metadata": {}, - "output_type": "execute_result" + "execution_count": 60 } ], "source": [ @@ -2775,24 +2761,24 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": null, "metadata": { + "id": "dwDx_gMsTGfD", + "outputId": "8165cfd9-a88d-4212-8c45-1eb84ef5be83", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "dwDx_gMsTGfD", - "outputId": "8165cfd9-a88d-4212-8c45-1eb84ef5be83" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([[2, 5, 8]])" ] }, - "execution_count": 61, "metadata": {}, - "output_type": "execute_result" + "execution_count": 61 } ], "source": [ @@ -2802,24 +2788,24 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": null, "metadata": { + "id": "xiw3_1E3TGfD", + "outputId": "12fa4749-cf52-4e88-c2c0-44d26aeb633c", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "xiw3_1E3TGfD", - "outputId": "12fa4749-cf52-4e88-c2c0-44d26aeb633c" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([5])" ] }, - "execution_count": 62, "metadata": {}, - "output_type": "execute_result" + "execution_count": 62 } ], "source": [ @@ -2829,24 +2815,24 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": null, "metadata": { + "id": "XFVEgrKhTGfD", + "outputId": "69eadeb9-11b3-4b48-cb95-0b3305c1274c", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "XFVEgrKhTGfD", - "outputId": "69eadeb9-11b3-4b48-cb95-0b3305c1274c" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([1, 2, 3])" ] }, - "execution_count": 63, "metadata": {}, - "output_type": "execute_result" + "execution_count": 63 } ], "source": [ @@ -2856,12 +2842,12 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "6Ik0r11RIxtm" - }, "source": [ "Indexing can be quite confusing to begin with, especially with larger tensors (I still have to try indexing multiple times to get it right). But with a bit of practice and following the data explorer's motto (***visualize, visualize, visualize***), you'll start to get the hang of it." - ] + ], + "metadata": { + "id": "6Ik0r11RIxtm" + } }, { "cell_type": "markdown", @@ -2882,7 +2868,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2892,19 +2878,20 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(array([1., 2., 3., 4., 5., 6., 7.]),\n", " tensor([1., 2., 3., 4., 5., 6., 7.], dtype=torch.float64))" ] }, - "execution_count": 64, "metadata": {}, - "output_type": "execute_result" + "execution_count": 64 } ], "source": [ "# NumPy array to tensor\n", + "import torch\n", "import numpy as np\n", "array = np.arange(1.0, 8.0)\n", "tensor = torch.from_numpy(array)\n", @@ -2913,9 +2900,6 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "16JG6cONLPnO" - }, "source": [ "> **Note:** By default, NumPy arrays are created with the datatype `float64` and if you convert it to a PyTorch tensor, it'll keep the same datatype (as above). \n", ">\n", @@ -2924,11 +2908,14 @@ "> So if you want to convert your NumPy array (float64) -> PyTorch tensor (float64) -> PyTorch tensor (float32), you can use `tensor = torch.from_numpy(array).type(torch.float32)`.\n", "\n", "Because we reassigned `tensor` above, if you change the tensor, the array stays the same." - ] + ], + "metadata": { + "id": "16JG6cONLPnO" + } }, { "cell_type": "code", - "execution_count": 65, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2938,15 +2925,15 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(array([2., 3., 4., 5., 6., 7., 8.]),\n", " tensor([1., 2., 3., 4., 5., 6., 7.], dtype=torch.float64))" ] }, - "execution_count": 65, "metadata": {}, - "output_type": "execute_result" + "execution_count": 65 } ], "source": [ @@ -2957,16 +2944,16 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "geVvu1p0MTWc" - }, "source": [ "And if you want to go from PyTorch tensor to NumPy array, you can call `tensor.numpy()`." - ] + ], + "metadata": { + "id": "geVvu1p0MTWc" + } }, { "cell_type": "code", - "execution_count": 66, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2976,15 +2963,15 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(tensor([1., 1., 1., 1., 1., 1., 1.]),\n", " array([1., 1., 1., 1., 1., 1., 1.], dtype=float32))" ] }, - "execution_count": 66, "metadata": {}, - "output_type": "execute_result" + "execution_count": 66 } ], "source": [ @@ -2996,16 +2983,16 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "Dt8yEV1jMfi2" - }, "source": [ "And the same rule applies as above, if you change the original `tensor`, the new `numpy_tensor` stays the same." - ] + ], + "metadata": { + "id": "Dt8yEV1jMfi2" + } }, { "cell_type": "code", - "execution_count": 67, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -3015,15 +3002,15 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "(tensor([2., 2., 2., 2., 2., 2., 2.]),\n", " array([1., 1., 1., 1., 1., 1., 1.], dtype=float32))" ] }, - "execution_count": 67, "metadata": {}, - "output_type": "execute_result" + "execution_count": 67 } ], "source": [ @@ -3075,18 +3062,18 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": null, "metadata": { + "id": "eSwxnwEbTGfF", + "outputId": "73b34154-734f-496f-9b55-b6aaa137e854", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "eSwxnwEbTGfF", - "outputId": "73b34154-734f-496f-9b55-b6aaa137e854" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Tensor A:\n", "tensor([[0.8016, 0.3649, 0.6286, 0.9663],\n", @@ -3102,6 +3089,7 @@ ] }, { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([[False, False, False, False],\n", @@ -3109,12 +3097,13 @@ " [False, False, False, False]])" ] }, - "execution_count": 68, "metadata": {}, - "output_type": "execute_result" + "execution_count": 68 } ], "source": [ + "import torch\n", + "\n", "# Create two random tensors\n", "random_tensor_A = torch.rand(3, 4)\n", "random_tensor_B = torch.rand(3, 4)\n", @@ -3127,9 +3116,6 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "nPU6mDKJnr8M" - }, "source": [ "Just as you might've expected, the tensors come out with different values.\n", "\n", @@ -3140,22 +3126,25 @@ "That's where [`torch.manual_seed(seed)`](https://pytorch.org/docs/stable/generated/torch.manual_seed.html) comes in, where `seed` is an integer (like `42` but it could be anything) that flavours the randomness.\n", "\n", "Let's try it out by creating some more *flavoured* random tensors." - ] + ], + "metadata": { + "id": "nPU6mDKJnr8M" + } }, { "cell_type": "code", - "execution_count": 69, + "execution_count": null, "metadata": { + "id": "sB6d1GfYTGfF", + "outputId": "4d11d38e-4406-4aff-9a81-cf13aa89ee5f", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "sB6d1GfYTGfF", - "outputId": "4d11d38e-4406-4aff-9a81-cf13aa89ee5f" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Tensor C:\n", "tensor([[0.8823, 0.9150, 0.3829, 0.9593],\n", @@ -3171,6 +3160,7 @@ ] }, { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([[True, True, True, True],\n", @@ -3178,20 +3168,22 @@ " [True, True, True, True]])" ] }, - "execution_count": 69, "metadata": {}, - "output_type": "execute_result" + "execution_count": 69 } ], "source": [ - "## Set the random seed\n", + "import torch\n", + "import random\n", + "\n", + "# # Set the random seed\n", "RANDOM_SEED=42 # try changing this to different values and see what happens to the numbers below\n", "torch.manual_seed(seed=RANDOM_SEED) \n", "random_tensor_C = torch.rand(3, 4)\n", "\n", "# Have to reset the seed every time a new rand() is called \n", "# Without this, tensor_D would be different to tensor_C \n", - "torch.manual_seed(seed=RANDOM_SEED) # try commenting this line out and seeing what happens\n", + "torch.random.manual_seed(seed=RANDOM_SEED) # try commenting this line out and seeing what happens\n", "random_tensor_D = torch.rand(3, 4)\n", "\n", "print(f\"Tensor C:\\n{random_tensor_C}\\n\")\n", @@ -3202,9 +3194,6 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "uct53Xr5QRC_" - }, "source": [ "Nice!\n", "\n", @@ -3213,7 +3202,10 @@ "> **Resource:** What we've just covered only scratches the surface of reproducibility in PyTorch. For more, on reproducbility in general and random seeds, I'd checkout:\n", "> * [The PyTorch reproducibility documentation](https://pytorch.org/docs/stable/notes/randomness.html) (a good exericse would be to read through this for 10-minutes and even if you don't understand it now, being aware of it is important).\n", "> * [The Wikipedia random seed page](https://en.wikipedia.org/wiki/Random_seed) (this'll give a good overview of random seeds and pseudorandomness in general)." - ] + ], + "metadata": { + "id": "uct53Xr5QRC_" + } }, { "cell_type": "markdown", @@ -3241,9 +3233,6 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "0UiR6QpoYQH_" - }, "source": [ "\n", "### 1. Getting a GPU\n", @@ -3264,11 +3253,16 @@ "\n", "To check if you've got access to a Nvidia GPU, you can run `!nvidia-smi` where the `!` (also called bang) means \"run this on the command line\".\n", "\n" - ] + ], + "metadata": { + "id": "0UiR6QpoYQH_" + } }, { "cell_type": "code", - "execution_count": 70, + "source": [ + "!nvidia-smi" + ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -3276,10 +3270,11 @@ "id": "vEMcO-9zYc-w", "outputId": "77405db7-3494-4add-cfc7-8415e52a0412" }, + "execution_count": null, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Thu Feb 10 02:09:18 2022 \n", "+-----------------------------------------------------------------------------+\n", @@ -3303,16 +3298,10 @@ "+-----------------------------------------------------------------------------+\n" ] } - ], - "source": [ - "!nvidia-smi" ] }, { "cell_type": "markdown", - "metadata": { - "id": "HvkB9p5zYf8E" - }, "source": [ "If you don't have a Nvidia GPU accessible, the above will output something like:\n", "\n", @@ -3346,13 +3335,13 @@ "| No running processes found |\n", "+-----------------------------------------------------------------------------+\n", "```" - ] + ], + "metadata": { + "id": "HvkB9p5zYf8E" + } }, { "cell_type": "markdown", - "metadata": { - "id": "UvibZ6e0YcDk" - }, "source": [ "\n", "\n", @@ -3365,11 +3354,14 @@ "Rather than talk about it, let's try it out.\n", "\n", "You can test if PyTorch has access to a GPU using [`torch.cuda.is_available()`](https://pytorch.org/docs/stable/generated/torch.cuda.is_available.html#torch.cuda.is_available).\n" - ] + ], + "metadata": { + "id": "UvibZ6e0YcDk" + } }, { "cell_type": "code", - "execution_count": 71, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -3379,26 +3371,24 @@ }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, - "execution_count": 71, "metadata": {}, - "output_type": "execute_result" + "execution_count": 71 } ], "source": [ "# Check for GPU\n", + "import torch\n", "torch.cuda.is_available()" ] }, { "cell_type": "markdown", - "metadata": { - "id": "jedZcx2PZFpL" - }, "source": [ "If the above outputs `True`, PyTorch can see and use the GPU, if it outputs `False`, it can't see the GPU and in that case, you'll have to go back through the installation steps.\n", "\n", @@ -3407,21 +3397,25 @@ "That way, if you or someone decides to run your code, it'll work regardless of the computing device they're using. \n", "\n", "Let's create a `device` variable to store what kind of device is available." - ] + ], + "metadata": { + "id": "jedZcx2PZFpL" + } }, { "cell_type": "code", - "execution_count": 72, + "execution_count": null, "metadata": { + "id": "j92HBCKB7rYa", + "outputId": "8cca1643-645c-4b67-f1f5-37066f6b9549", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - }, - "id": "j92HBCKB7rYa", - "outputId": "8cca1643-645c-4b67-f1f5-37066f6b9549" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" @@ -3430,9 +3424,8 @@ "'cuda'" ] }, - "execution_count": 72, "metadata": {}, - "output_type": "execute_result" + "execution_count": 72 } ], "source": [ @@ -3443,9 +3436,6 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "FjFyPP2WaCch" - }, "source": [ "If the above output `\"cuda\"` it means we can set all of our PyTorch code to use the available CUDA device (a GPU) and if it output `\"cpu\"`, our PyTorch code will stick with the CPU.\n", "\n", @@ -3454,28 +3444,31 @@ "If you want to do faster computing you can use a GPU but if you want to do *much* faster computing, you can use multiple GPUs.\n", "\n", "You can count the number of GPUs PyTorch has access to using [`torch.cuda.device_count()`](https://pytorch.org/docs/stable/generated/torch.cuda.device_count.html#torch.cuda.device_count)." - ] + ], + "metadata": { + "id": "FjFyPP2WaCch" + } }, { "cell_type": "code", - "execution_count": 73, + "execution_count": null, "metadata": { + "id": "MArsn0DFTGfG", + "outputId": "de717df5-bb67-4900-805e-a6f00ad0b409", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "MArsn0DFTGfG", - "outputId": "de717df5-bb67-4900-805e-a6f00ad0b409" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "1" ] }, - "execution_count": 73, "metadata": {}, - "output_type": "execute_result" + "execution_count": 73 } ], "source": [ @@ -3485,12 +3478,12 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "xVNf1hiqa-gO" - }, "source": [ "Knowing the number of GPUs PyTorch has access to is helpful incase you wanted to run a specific process on one GPU and another process on another (PyTorch also has features to let you run a process across *all* GPUs)." - ] + ], + "metadata": { + "id": "xVNf1hiqa-gO" + } }, { "cell_type": "markdown", @@ -3515,31 +3508,31 @@ }, { "cell_type": "code", - "execution_count": 74, + "execution_count": null, "metadata": { + "id": "FhI3srFXEHfP", + "outputId": "2f4f6435-fdc4-4e99-e87c-9421c2100f36", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "FhI3srFXEHfP", - "outputId": "2f4f6435-fdc4-4e99-e87c-9421c2100f36" + } }, "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "tensor([1, 2, 3]) cpu\n" ] }, { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([1, 2, 3], device='cuda:0')" ] }, - "execution_count": 74, "metadata": {}, - "output_type": "execute_result" + "execution_count": 74 } ], "source": [ @@ -3573,9 +3566,6 @@ }, { "cell_type": "markdown", - "metadata": { - "id": "4puyUX4Bci5D" - }, "source": [ "### 4. Moving tensors back to the CPU\n", "\n", @@ -3584,24 +3574,27 @@ "For example, you'll want to do this if you want to interact with your tensors with NumPy (NumPy does not leverage the GPU).\n", "\n", "Let's try using the [`torch.Tensor.numpy()`](https://pytorch.org/docs/stable/generated/torch.Tensor.numpy.html) method on our `tensor_on_gpu`." - ] + ], + "metadata": { + "id": "4puyUX4Bci5D" + } }, { "cell_type": "code", - "execution_count": 75, + "execution_count": null, "metadata": { + "id": "3ChSLJgPTGfG", + "outputId": "32e92f62-db28-4dc7-ce93-c2ab33229252", "colab": { "base_uri": "https://localhost:8080/", "height": 186 - }, - "id": "3ChSLJgPTGfG", - "outputId": "32e92f62-db28-4dc7-ce93-c2ab33229252" + } }, "outputs": [ { + "output_type": "error", "ename": "TypeError", "evalue": "ignored", - "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", @@ -3628,24 +3621,24 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": null, "metadata": { + "id": "gN15s-NdTGfG", + "outputId": "9fffb6f2-c200-4f9c-d987-d9ab5d9cba49", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "gN15s-NdTGfG", - "outputId": "9fffb6f2-c200-4f9c-d987-d9ab5d9cba49" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "array([1, 2, 3])" ] }, - "execution_count": 76, "metadata": {}, - "output_type": "execute_result" + "execution_count": 76 } ], "source": [ @@ -3665,24 +3658,24 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": null, "metadata": { + "id": "S5u83PCRTGfH", + "outputId": "4cb931e2-7c8d-49b9-a7de-db3d3c6589b5", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "S5u83PCRTGfH", - "outputId": "4cb931e2-7c8d-49b9-a7de-db3d3c6589b5" + } }, "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "tensor([1, 2, 3], device='cuda:0')" ] }, - "execution_count": 77, "metadata": {}, - "output_type": "execute_result" + "execution_count": 77 } ], "source": [ @@ -3747,13 +3740,12 @@ } ], "metadata": { - "accelerator": "GPU", "colab": { "collapsed_sections": [], - "include_colab_link": true, "name": "00_pytorch_fundamentals.ipynb", "provenance": [], - "toc_visible": true + "toc_visible": true, + "include_colab_link": true }, "interpreter": { "hash": "3fbe1355223f7b2ffc113ba3ade6a2b520cadace5d5ec3e828c83ce02eb221bf" @@ -3773,8 +3765,9 @@ "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" - } + }, + "accelerator": "GPU" }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file