From d8d5a13dc29782446c35f6c79e915cd84ac0513e Mon Sep 17 00:00:00 2001 From: Daniel Bourke Date: Wed, 8 Dec 2021 11:17:13 +1000 Subject: [PATCH] clean up fundamentals --- 00_pytorch_fundamentals.ipynb | 439 +++++++++++++++++----------------- 1 file changed, 223 insertions(+), 216 deletions(-) diff --git a/00_pytorch_fundamentals.ipynb b/00_pytorch_fundamentals.ipynb index 1fc96c0..3acc5a6 100644 --- a/00_pytorch_fundamentals.ipynb +++ b/00_pytorch_fundamentals.ipynb @@ -59,7 +59,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "1.9.1\n" + "1.10.0\n" ] } ], @@ -341,103 +341,7 @@ }, { "cell_type": "code", - "execution_count": 19, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "xRnzgN2F9K0B", - "outputId": "40a5889e-a82e-4037-d7fb-b7ceb0b1710e" - }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:3: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n", - " This is separate from the ipykernel package so we can avoid doing imports until\n" - ] - }, - { - "data": { - "text/plain": [ - "(tensor([1, 2, 3]), tensor([1, 2, 3]))" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Note: torch.tensor() always makes a copy\n", - "tensor_A = torch.tensor([1, 2, 3])\n", - "tensor_B = torch.tensor(tensor_A) # Creating a tensor like this produces a warning\n", - "\n", - "tensor_A, tensor_B" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "bK4B-xpT9op8", - "outputId": "8d43c184-de2d-497a-8b93-8192053b31fd" - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(tensor([1, 2, 3]), tensor([4, 2, 3]))" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Change tensor_B, doesn't change tensor_A\n", - "tensor_B[0] = 4\n", - "\n", - "tensor_A, tensor_B" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "GCbBkdKa96aO", - "outputId": "5ba5a1eb-5a4e-438e-8271-6ca886738901" - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(tensor([1, 4, 3]), tensor([4, 2, 3]))" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Change tensor_A, doesn't change tensor_B\n", - "tensor_A[1] = 4\n", - "\n", - "tensor_A, tensor_B" - ] - }, - { - "cell_type": "code", - "execution_count": 27, + "execution_count": 15, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -449,12 +353,13 @@ { "data": { "text/plain": [ - "(tensor([[0.6109, 0.1709, 0.9683, 0.5726],\n", - " [0.6924, 0.6208, 0.8857, 0.1900],\n", - " [0.7239, 0.4225, 0.0904, 0.8145]]), torch.float32)" + "(tensor([[0.3608, 0.7695, 0.7676, 0.4997],\n", + " [0.9372, 0.8822, 0.1910, 0.7166],\n", + " [0.2663, 0.1389, 0.1353, 0.4211]]),\n", + " torch.float32)" ] }, - "execution_count": 27, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -467,7 +372,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 16, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -481,10 +386,11 @@ "text/plain": [ "(tensor([[0., 0., 0., 0.],\n", " [0., 0., 0., 0.],\n", - " [0., 0., 0., 0.]]), torch.float32)" + " [0., 0., 0., 0.]]),\n", + " torch.float32)" ] }, - "execution_count": 29, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -497,7 +403,32 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor([[1., 1., 1., 1.],\n", + " [1., 1., 1., 1.],\n", + " [1., 1., 1., 1.]]),\n", + " torch.float32)" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Ones\n", + "ones = torch.ones(size=(3, 4))\n", + "ones, ones.dtype" + ] + }, + { + "cell_type": "code", + "execution_count": 20, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -506,26 +437,35 @@ "outputId": "20c17956-9d58-4757-c786-cb089d2a9551" }, "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "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" + ] + }, { "data": { "text/plain": [ "tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, - "execution_count": 30, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# torch.arange(), torch.range() is deprecated \n", + "# 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", "zero_to_ten" ] }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 19, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -540,7 +480,7 @@ "tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])" ] }, - "execution_count": 32, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -553,7 +493,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 21, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -568,7 +508,7 @@ "(torch.Size([3]), torch.float32, device(type='cpu'))" ] }, - "execution_count": 23, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -585,7 +525,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 22, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -600,7 +540,7 @@ "torch.float16" ] }, - "execution_count": 24, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -623,7 +563,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 23, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -636,9 +576,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "tensor([[0.7211, 0.2743, 0.2621, 0.0821],\n", - " [0.0890, 0.7967, 0.8323, 0.2623],\n", - " [0.8343, 0.2572, 0.4677, 0.1218]])\n", + "tensor([[0.0026, 0.9400, 0.5669, 0.7157],\n", + " [0.7173, 0.2704, 0.3400, 0.6617],\n", + " [0.4410, 0.5464, 0.1685, 0.7501]])\n", "Shape of tensor: torch.Size([3, 4])\n", "Datatype of tensor: torch.float32\n", "Device tensor is stored on: cpu\n" @@ -674,7 +614,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 24, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -689,7 +629,7 @@ "tensor([11, 12, 13])" ] }, - "execution_count": 94, + "execution_count": 24, "metadata": {}, "output_type": "execute_result" } @@ -701,7 +641,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 25, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -716,7 +656,7 @@ "tensor([10, 20, 30])" ] }, - "execution_count": 44, + "execution_count": 25, "metadata": {}, "output_type": "execute_result" } @@ -727,7 +667,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 26, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -742,7 +682,7 @@ "tensor([1, 2, 3])" ] }, - "execution_count": 45, + "execution_count": 26, "metadata": {}, "output_type": "execute_result" } @@ -754,7 +694,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 27, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -769,7 +709,7 @@ "tensor([-9, -8, -7])" ] }, - "execution_count": 46, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -781,7 +721,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 28, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -796,7 +736,7 @@ "tensor([1, 2, 3])" ] }, - "execution_count": 47, + "execution_count": 28, "metadata": {}, "output_type": "execute_result" } @@ -808,7 +748,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 29, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -823,7 +763,7 @@ "tensor([10, 20, 30])" ] }, - "execution_count": 48, + "execution_count": 29, "metadata": {}, "output_type": "execute_result" } @@ -835,7 +775,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 30, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -850,7 +790,7 @@ "tensor([1, 2, 3])" ] }, - "execution_count": 49, + "execution_count": 30, "metadata": {}, "output_type": "execute_result" } @@ -862,7 +802,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 31, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -877,7 +817,7 @@ "tensor([1, 4, 9])" ] }, - "execution_count": 55, + "execution_count": 31, "metadata": {}, "output_type": "execute_result" } @@ -901,7 +841,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 32, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -916,7 +856,7 @@ "tensor([1, 2, 3])" ] }, - "execution_count": 2, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } @@ -929,7 +869,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 33, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -944,7 +884,7 @@ "tensor(14)" ] }, - "execution_count": 3, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -955,7 +895,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 34, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -970,7 +910,7 @@ "tensor(14)" ] }, - "execution_count": 4, + "execution_count": 34, "metadata": {}, "output_type": "execute_result" } @@ -981,7 +921,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 35, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -996,7 +936,7 @@ "tensor(14)" ] }, - "execution_count": 5, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } @@ -1007,7 +947,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 36, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1022,7 +962,7 @@ "tensor(14)" ] }, - "execution_count": 6, + "execution_count": 36, "metadata": {}, "output_type": "execute_result" } @@ -1035,9 +975,18 @@ "value" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## One of the most common errors in deep learning\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": 7, + "execution_count": 37, "metadata": { "colab": { "base_uri": "https://localhost:8080/", @@ -1054,7 +1003,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/tmp/ipykernel_34033/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/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;31mRuntimeError\u001b[0m: mat1 and mat2 shapes cannot be multiplied (3x2 and 3x2)" ] } @@ -1074,7 +1023,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 39, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1091,18 +1040,19 @@ " [ 83., 105., 127.]])" ] }, - "execution_count": 8, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "# The operation works when tensor_B is transposed\n", "torch.matmul(tensor_A, tensor_B.T)" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 40, "metadata": {}, "outputs": [ { @@ -1113,7 +1063,7 @@ " [ 83., 105., 127.]])" ] }, - "execution_count": 9, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } @@ -1125,7 +1075,7 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": 41, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1137,11 +1087,12 @@ { "data": { "text/plain": [ - "(tensor([[ 1.4391, -1.4383],\n", - " [ 1.5400, -1.5229]], grad_fn=), torch.Size([2, 2]))" + "(tensor([[0.3035, 2.4169],\n", + " [0.8981, 2.5015]], grad_fn=),\n", + " torch.Size([2, 2]))" ] }, - "execution_count": 79, + "execution_count": 41, "metadata": {}, "output_type": "execute_result" } @@ -1156,7 +1107,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 42, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1171,7 +1122,7 @@ "torch.Size([2, 3])" ] }, - "execution_count": 80, + "execution_count": 42, "metadata": {}, "output_type": "execute_result" } @@ -1194,7 +1145,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 43, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1209,7 +1160,7 @@ "tensor([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])" ] }, - "execution_count": 103, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" } @@ -1221,7 +1172,7 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 44, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1250,7 +1201,7 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": 45, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1265,7 +1216,7 @@ "(tensor(90), tensor(0), tensor(45.), tensor(450))" ] }, - "execution_count": 106, + "execution_count": 45, "metadata": {}, "output_type": "execute_result" } @@ -1285,7 +1236,7 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 46, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1300,7 +1251,7 @@ "(tensor([10, 20, 30, 40, 50, 60, 70, 80, 90]), tensor(8))" ] }, - "execution_count": 83, + "execution_count": 46, "metadata": {}, "output_type": "execute_result" } @@ -1313,7 +1264,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 47, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1328,7 +1279,7 @@ "(tensor([10, 20, 30, 40, 50, 60, 70, 80, 90]), tensor(0))" ] }, - "execution_count": 86, + "execution_count": 47, "metadata": {}, "output_type": "execute_result" } @@ -1352,7 +1303,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": 48, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1367,7 +1318,7 @@ "torch.float32" ] }, - "execution_count": 87, + "execution_count": 48, "metadata": {}, "output_type": "execute_result" } @@ -1379,7 +1330,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 49, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1394,7 +1345,7 @@ "tensor([10., 20., 30., 40., 50., 60., 70., 80., 90.], dtype=torch.float16)" ] }, - "execution_count": 92, + "execution_count": 49, "metadata": {}, "output_type": "execute_result" } @@ -1406,7 +1357,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 50, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1421,7 +1372,7 @@ "tensor([10, 20, 30, 40, 50, 60, 70, 80, 90], dtype=torch.int32)" ] }, - "execution_count": 93, + "execution_count": 50, "metadata": {}, "output_type": "execute_result" } @@ -1448,7 +1399,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 52, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1463,7 +1414,7 @@ "tensor([1., 2., 3., 4., 5., 6., 7.])" ] }, - "execution_count": 16, + "execution_count": 52, "metadata": {}, "output_type": "execute_result" } @@ -1476,7 +1427,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 53, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1491,7 +1442,7 @@ "(tensor([[1., 2., 3., 4., 5., 6., 7.]]), torch.Size([1, 7]))" ] }, - "execution_count": 17, + "execution_count": 53, "metadata": {}, "output_type": "execute_result" } @@ -1504,7 +1455,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 54, "metadata": {}, "outputs": [ { @@ -1513,7 +1464,7 @@ "(tensor([[1., 2., 3., 4., 5., 6., 7.]]), torch.Size([1, 7]))" ] }, - "execution_count": 18, + "execution_count": 54, "metadata": {}, "output_type": "execute_result" } @@ -1527,7 +1478,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 55, "metadata": {}, "outputs": [ { @@ -1536,7 +1487,7 @@ "(tensor([[5., 2., 3., 4., 5., 6., 7.]]), tensor([5., 2., 3., 4., 5., 6., 7.]))" ] }, - "execution_count": 19, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } @@ -1549,7 +1500,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 56, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1567,7 +1518,7 @@ " [5., 2., 3., 4., 5., 6., 7.]])" ] }, - "execution_count": 20, + "execution_count": 56, "metadata": {}, "output_type": "execute_result" } @@ -1580,7 +1531,7 @@ }, { "cell_type": "code", - "execution_count": 116, + "execution_count": 57, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1592,10 +1543,10 @@ { "data": { "text/plain": [ - "(tensor([1., 2., 3., 4., 5., 6., 7.]), torch.Size([7]))" + "(tensor([5., 2., 3., 4., 5., 6., 7.]), torch.Size([7]))" ] }, - "execution_count": 116, + "execution_count": 57, "metadata": {}, "output_type": "execute_result" } @@ -1608,7 +1559,7 @@ }, { "cell_type": "code", - "execution_count": 120, + "execution_count": 58, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1620,10 +1571,10 @@ { "data": { "text/plain": [ - "(tensor([[1., 2., 3., 4., 5., 6., 7.]]), torch.Size([1, 7]))" + "(tensor([[5., 2., 3., 4., 5., 6., 7.]]), torch.Size([1, 7]))" ] }, - "execution_count": 120, + "execution_count": 58, "metadata": {}, "output_type": "execute_result" } @@ -1634,6 +1585,29 @@ "x_unsqueezed, x_unsqueezed.shape" ] }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(torch.Size([224, 224, 3]), torch.Size([3, 224, 224]))" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Change different axes\n", + "x_original = torch.rand(size=(224, 224, 3))\n", + "x_permuted = x_original.permute(2, 0, 1) # shifts axis 0->1, 1->2, 2->0\n", + "x_original.shape, x_permuted.shape" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1643,7 +1617,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 64, "metadata": {}, "outputs": [ { @@ -1654,7 +1628,7 @@ " [6, 7, 8]]])" ] }, - "execution_count": 23, + "execution_count": 64, "metadata": {}, "output_type": "execute_result" } @@ -1667,7 +1641,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 65, "metadata": {}, "outputs": [ { @@ -1678,7 +1652,7 @@ " [6, 7, 8]])" ] }, - "execution_count": 24, + "execution_count": 65, "metadata": {}, "output_type": "execute_result" } @@ -1689,7 +1663,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 66, "metadata": {}, "outputs": [ { @@ -1698,7 +1672,7 @@ "tensor([[0, 1, 2]])" ] }, - "execution_count": 25, + "execution_count": 66, "metadata": {}, "output_type": "execute_result" } @@ -1709,7 +1683,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 67, "metadata": {}, "outputs": [ { @@ -1718,7 +1692,7 @@ "tensor([[1, 4, 7]])" ] }, - "execution_count": 26, + "execution_count": 67, "metadata": {}, "output_type": "execute_result" } @@ -1729,7 +1703,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 68, "metadata": {}, "outputs": [ { @@ -1738,7 +1712,7 @@ "tensor([4])" ] }, - "execution_count": 28, + "execution_count": 68, "metadata": {}, "output_type": "execute_result" } @@ -1749,7 +1723,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 69, "metadata": {}, "outputs": [ { @@ -1758,7 +1732,7 @@ "tensor([[0, 3, 6]])" ] }, - "execution_count": 31, + "execution_count": 69, "metadata": {}, "output_type": "execute_result" } @@ -1784,7 +1758,7 @@ "metadata": {}, "outputs": [], "source": [ - "# TODO - add in example of scatter" + "# TODO - add in example of scatter\n" ] }, { @@ -1983,9 +1957,9 @@ "\n", "# Have to reset the seed every time a new rand() is called \n", "torch.random.manual_seed(42) # Without this, tensor_D would be different to tensor_C\n", - "random_tesnor_D = torch.rand(3, 4)\n", + "random_tensor_D = torch.rand(3, 4)\n", "\n", - "random_tensor_C == random_tesnor_D\n" + "random_tensor_C == random_tensor_D\n" ] }, { @@ -2004,7 +1978,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 70, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2019,7 +1993,7 @@ "True" ] }, - "execution_count": 42, + "execution_count": 70, "metadata": {}, "output_type": "execute_result" } @@ -2032,7 +2006,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 71, "metadata": { "id": "j92HBCKB7rYa" }, @@ -2043,7 +2017,7 @@ "'cuda'" ] }, - "execution_count": 14, + "execution_count": 71, "metadata": {}, "output_type": "execute_result" } @@ -2056,7 +2030,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 72, "metadata": {}, "outputs": [ { @@ -2065,7 +2039,7 @@ "1" ] }, - "execution_count": 41, + "execution_count": 72, "metadata": {}, "output_type": "execute_result" } @@ -2092,7 +2066,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 73, "metadata": { "id": "FhI3srFXEHfP" }, @@ -2110,7 +2084,7 @@ "tensor([1, 2, 3], device='cuda:0')" ] }, - "execution_count": 21, + "execution_count": 73, "metadata": {}, "output_type": "execute_result" } @@ -2140,7 +2114,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 74, "metadata": {}, "outputs": [ { @@ -2150,7 +2124,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/tmp/ipykernel_7994/1481451780.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# If tensor is on GPU, can't transform it to NumPy\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mtensor_on_gpu\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnumpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/tmp/ipykernel_327633/704146591.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# If tensor is on GPU, can't transform it to NumPy (this will error)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mtensor_on_gpu\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnumpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first." ] } @@ -2171,7 +2145,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 75, "metadata": {}, "outputs": [ { @@ -2180,7 +2154,7 @@ "array([1, 2, 3])" ] }, - "execution_count": 29, + "execution_count": 75, "metadata": {}, "output_type": "execute_result" } @@ -2200,7 +2174,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 76, "metadata": {}, "outputs": [ { @@ -2209,7 +2183,7 @@ "tensor([1, 2, 3], device='cuda:0')" ] }, - "execution_count": 30, + "execution_count": 76, "metadata": {}, "output_type": "execute_result" } @@ -2218,6 +2192,38 @@ "tensor_on_gpu" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can also use `detach()` to cut the connection between the tensor on GPU and CPU.\n", + "\n", + "See here for more: \n", + "* https://discuss.pytorch.org/t/detach-vs-cpu/99991\n", + "* https://discuss.pytorch.org/t/is-any-case-to-prefer-detach-cpu-to-cpu-detach-or-vice-versa/93712" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2, 3])" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tensor_detached = tensor_on_gpu.detach().cpu().numpy()\n", + "tensor_detached" + ] + }, { "cell_type": "markdown", "metadata": { @@ -2225,6 +2231,7 @@ }, "source": [ "UPTOHERE:\n", + "* Fix up section for .cpu() and .detach() to make it clearer\n", "* add in scatter (for changing values)\n", "* go back through headings and make sure cells are complete \n" ]