From 89b23dc5fc50d43c6dd2d86a7758d824508badc9 Mon Sep 17 00:00:00 2001 From: Daniel Bourke Date: Fri, 12 Aug 2022 14:50:46 +1000 Subject: [PATCH] fix exercise ordering --- 00_pytorch_fundamentals.ipynb | 268 ++++++++++++++++++---------------- 1 file changed, 143 insertions(+), 125 deletions(-) diff --git a/00_pytorch_fundamentals.ipynb b/00_pytorch_fundamentals.ipynb index b25c922..c562b98 100644 --- a/00_pytorch_fundamentals.ipynb +++ b/00_pytorch_fundamentals.ipynb @@ -96,7 +96,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/", @@ -108,11 +108,8 @@ "outputs": [ { "data": { - "application/vnd.google.colaboratory.intrinsic+json": { - "type": "string" - }, "text/plain": [ - "'1.10.0+cu111'" + "'1.12.0+cu102'" ] }, "execution_count": 1, @@ -184,7 +181,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -225,7 +222,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -264,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -306,7 +303,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -345,7 +342,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -391,7 +388,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -429,7 +426,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -469,7 +466,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -507,7 +504,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -544,7 +541,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -593,7 +590,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -629,7 +626,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -715,7 +712,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -727,9 +724,10 @@ { "data": { "text/plain": [ - "(tensor([[0.4090, 0.2527, 0.8699, 0.2002],\n", - " [0.8421, 0.1428, 0.1431, 0.0111],\n", - " [0.2281, 0.0345, 0.6734, 0.3866]]), torch.float32)" + "(tensor([[0.0862, 0.7581, 0.2308, 0.8367],\n", + " [0.3364, 0.5120, 0.8584, 0.8268],\n", + " [0.6265, 0.9721, 0.2375, 0.9852]]),\n", + " torch.float32)" ] }, "execution_count": 14, @@ -756,7 +754,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -801,7 +799,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -815,7 +813,8 @@ "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": 16, @@ -840,7 +839,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -854,7 +853,8 @@ "text/plain": [ "(tensor([[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", - " [1., 1., 1., 1.]]), torch.float32)" + " [1., 1., 1., 1.]]),\n", + " torch.float32)" ] }, "execution_count": 17, @@ -890,7 +890,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -903,8 +903,8 @@ "name": "stderr", "output_type": "stream", "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" + "/tmp/ipykernel_1193675/193451495.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) # Note: this may return an error in the future\n" ] }, { @@ -942,7 +942,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1015,7 +1015,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1064,7 +1064,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1111,7 +1111,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1124,9 +1124,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "tensor([[0.7799, 0.8140, 0.0893, 0.2062],\n", - " [0.7525, 0.3845, 0.8207, 0.4587],\n", - " [0.9277, 0.8166, 0.9052, 0.0953]])\n", + "tensor([[0.6844, 0.5630, 0.7787, 0.6471],\n", + " [0.0562, 0.4598, 0.3106, 0.7787],\n", + " [0.2167, 0.8431, 0.2011, 0.2784]])\n", "Shape of tensor: torch.Size([3, 4])\n", "Datatype of tensor: torch.float32\n", "Device tensor is stored on: cpu\n" @@ -1193,7 +1193,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1221,7 +1221,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1257,7 +1257,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1293,7 +1293,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1321,7 +1321,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1358,7 +1358,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1385,7 +1385,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1421,7 +1421,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1476,7 +1476,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1520,7 +1520,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1547,7 +1547,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1574,7 +1574,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1612,7 +1612,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1625,9 +1625,19 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 146 µs, sys: 38 µs, total: 184 µs\n", - "Wall time: 227 µs\n" + "CPU times: user 157 µs, sys: 14 µs, total: 171 µs\n", + "Wall time: 110 µs\n" ] + }, + { + "data": { + "text/plain": [ + "tensor(14)" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -1642,7 +1652,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1655,8 +1665,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 27 µs, sys: 7 µs, total: 34 µs\n", - "Wall time: 36.7 µs\n" + "CPU times: user 41 µs, sys: 4 µs, total: 45 µs\n", + "Wall time: 34.3 µs\n" ] }, { @@ -1688,7 +1698,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": { "colab": { "base_uri": "https://localhost:8080/", @@ -1700,12 +1710,12 @@ "outputs": [ { "ename": "RuntimeError", - "evalue": "ignored", + "evalue": "mat1 and mat2 shapes cannot be multiplied (3x2 and 3x2)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\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;31m# (this will error)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "Input \u001b[0;32mIn [37]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m tensor_A \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39mtensor([[\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m],\n\u001b[1;32m 3\u001b[0m [\u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m4\u001b[39m],\n\u001b[1;32m 4\u001b[0m [\u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m6\u001b[39m]], dtype\u001b[38;5;241m=\u001b[39mtorch\u001b[38;5;241m.\u001b[39mfloat32)\n\u001b[1;32m 6\u001b[0m tensor_B \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39mtensor([[\u001b[38;5;241m7\u001b[39m, \u001b[38;5;241m10\u001b[39m],\n\u001b[1;32m 7\u001b[0m [\u001b[38;5;241m8\u001b[39m, \u001b[38;5;241m11\u001b[39m], \n\u001b[1;32m 8\u001b[0m [\u001b[38;5;241m9\u001b[39m, \u001b[38;5;241m12\u001b[39m]], dtype\u001b[38;5;241m=\u001b[39mtorch\u001b[38;5;241m.\u001b[39mfloat32)\n\u001b[0;32m---> 10\u001b[0m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmatmul\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtensor_A\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtensor_B\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mRuntimeError\u001b[0m: mat1 and mat2 shapes cannot be multiplied (3x2 and 3x2)" ] } @@ -1742,7 +1752,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1772,7 +1782,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1801,7 +1811,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1852,7 +1862,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1929,7 +1939,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2010,7 +2020,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2047,7 +2057,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2088,7 +2098,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2127,7 +2137,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2177,7 +2187,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2215,7 +2225,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2252,7 +2262,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2321,7 +2331,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2359,7 +2369,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2396,7 +2406,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2436,7 +2446,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2473,7 +2483,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2515,7 +2525,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2557,7 +2567,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2599,7 +2609,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2654,7 +2664,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2668,7 +2678,8 @@ "text/plain": [ "(tensor([[[1, 2, 3],\n", " [4, 5, 6],\n", - " [7, 8, 9]]]), torch.Size([1, 3, 3]))" + " [7, 8, 9]]]),\n", + " torch.Size([1, 3, 3]))" ] }, "execution_count": 58, @@ -2694,7 +2705,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2734,7 +2745,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2761,7 +2772,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 61, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2788,7 +2799,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2815,7 +2826,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 63, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2868,7 +2879,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2915,7 +2926,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2953,7 +2964,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -2992,7 +3003,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -3062,7 +3073,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 68, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -3133,7 +3144,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 69, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -3260,7 +3271,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 70, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -3273,16 +3284,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "Thu Feb 10 02:09:18 2022 \n", + "Fri Aug 12 14:50:11 2022 \n", "+-----------------------------------------------------------------------------+\n", - "| NVIDIA-SMI 460.32.03 Driver Version: 460.32.03 CUDA Version: 11.2 |\n", + "| NVIDIA-SMI 515.48.07 Driver Version: 515.48.07 CUDA Version: 11.7 |\n", "|-------------------------------+----------------------+----------------------+\n", "| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\n", "| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\n", "| | | MIG M. |\n", "|===============================+======================+======================|\n", - "| 0 Tesla P100-PCIE... Off | 00000000:00:04.0 Off | 0 |\n", - "| N/A 36C P0 28W / 250W | 0MiB / 16280MiB | 0% Default |\n", + "| 0 NVIDIA TITAN RTX On | 00000000:01:00.0 Off | N/A |\n", + "| 40% 24C P8 7W / 280W | 1939MiB / 24576MiB | 0% Default |\n", "| | | N/A |\n", "+-------------------------------+----------------------+----------------------+\n", " \n", @@ -3291,7 +3302,9 @@ "| GPU GI CI PID Type Process name GPU Memory |\n", "| ID ID Usage |\n", "|=============================================================================|\n", - "| No running processes found |\n", + "| 0 N/A N/A 1068 G /usr/lib/xorg/Xorg 86MiB |\n", + "| 0 N/A N/A 1220 G /usr/bin/gnome-shell 9MiB |\n", + "| 0 N/A N/A 1186254 C ...de/pytorch/env/bin/python 1839MiB |\n", "+-----------------------------------------------------------------------------+\n" ] } @@ -3361,7 +3374,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -3404,7 +3417,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": { "colab": { "base_uri": "https://localhost:8080/", @@ -3416,9 +3429,6 @@ "outputs": [ { "data": { - "application/vnd.google.colaboratory.intrinsic+json": { - "type": "string" - }, "text/plain": [ "'cuda'" ] @@ -3451,7 +3461,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -3508,7 +3518,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 74, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -3581,7 +3591,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "metadata": { "colab": { "base_uri": "https://localhost:8080/", @@ -3593,12 +3603,12 @@ "outputs": [ { "ename": "TypeError", - "evalue": "ignored", + "evalue": "can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\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[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", + "Input \u001b[0;32mIn [75]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# If tensor is on GPU, can't transform it to NumPy (this will error)\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mtensor_on_gpu\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnumpy\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", "\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." ] } @@ -3621,7 +3631,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 76, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -3658,7 +3668,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 77, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -3690,14 +3700,14 @@ "source": [ "## Exercises\n", "\n", - "1. Documentation reading - A big part of deep learning (and learning to code in general) is getting familiar with the documentation of a certain framework you're using. We'll be using the PyTorch documentation a lot throughout the rest of this course. So I'd recommend spending 10-minutes reading the following (it's okay if you don't get some things for now, the focus is not yet full understanding, it's awareness):\n", - " * The documentation on [`torch.Tensor`](https://pytorch.org/docs/stable/tensors.html#torch-tensor).\n", - " * The documentation on [`torch.cuda`](https://pytorch.org/docs/master/notes/cuda.html#cuda-semantics).\n", - "2. Create a random tensor with shape `(7, 7)`.\n", - "3. Perform a matrix multiplication on the tensor from 2 with another random tensor with shape `(1, 7)` (hint: you may have to transpose the second tensor).\n", - "4. Set the random seed to `0` and do 2 & 3 over again. The output should be:\n", + "* 1. Documentation reading - A big part of deep learning (and learning to code in general) is getting familiar with the documentation of a certain framework you're using. We'll be using the PyTorch documentation a lot throughout the rest of this course. So I'd recommend spending 10-minutes reading the following (it's okay if you don't get some things for now, the focus is not yet full understanding, it's awareness):\n", + " * The documentation on [`torch.Tensor`](https://pytorch.org/docs/stable/tensors.html#torch-tensor).\n", + " * The documentation on [`torch.cuda`](https://pytorch.org/docs/master/notes/cuda.html#cuda-semantics).\n", + "* 2. Create a random tensor with shape `(7, 7)`.\n", + "* 3. Perform a matrix multiplication on the tensor from 2 with another random tensor with shape `(1, 7)` (hint: you may have to transpose the second tensor).\n", + "* 4. Set the random seed to `0` and do exercises 2 & 3 over again. The output should be:\n", "\n", - "```\n", + "```python\n", "(tensor([[1.8542],\n", " [1.9611],\n", " [2.2884],\n", @@ -3707,31 +3717,39 @@ " [1.7989]]), torch.Size([7, 1]))\n", "```\n", "\n", - "5. Speaking of random seeds, we saw how to set it with `torch.manual_seed()` but is there a GPU equivalent? (hint: you'll need to look into the documentation for `torch.cuda` for this one)\n", + "* 5. Speaking of random seeds, we saw how to set it with `torch.manual_seed()` but is there a GPU equivalent? (hint: you'll need to look into the documentation for `torch.cuda` for this one)\n", " * If there is, set the GPU random seed to `1234`.\n", - "6. Create two random tensors of shape `(2, 3)` and send them both to the GPU (you'll need access to a GPU for this). Set `torch.manual_seed(1234)` when creating the tensors (this doesn't have to be the GPU random seed). The output should be something like:\n", + "* 6. Create two random tensors of shape `(2, 3)` and send them both to the GPU (you'll need access to a GPU for this). Set `torch.manual_seed(1234)` when creating the tensors (this doesn't have to be the GPU random seed). The output should be something like:\n", "\n", - "```\n", + "```python\n", "Device: cuda\n", "(tensor([[0.0290, 0.4019, 0.2598],\n", " [0.3666, 0.0583, 0.7006]], device='cuda:0'),\n", " tensor([[0.0518, 0.4681, 0.6738],\n", " [0.3315, 0.7837, 0.5631]], device='cuda:0'))\n", "```\n", - "7. Perform a matrix multiplication on the tensors you created in 6 (again, you may have to adjust the shapes of one of the tensors).\n", - "8. Find the maximum and minimum values of the output of 7.\n", - "9. Find the maximum and minimum index values of the output of 7.\n", - "10. Make a random tensor with shape `(1, 1, 1, 10)` and then create a new tensor with all the `1` dimensions removed to be left with a tensor of shape `(10)`. Set the seed to `7` when you create it and print out the first tensor and it's shape as well as the second tensor and it's shape. The output should look something like:\n", "\n", - "```\n", + "* 7. Perform a matrix multiplication on the tensors you created in 6 (again, you may have to adjust the shapes of one of the tensors).\n", + "* 8. Find the maximum and minimum values of the output of 7.\n", + "* 9. Find the maximum and minimum index values of the output of 7.\n", + "* 10. Make a random tensor with shape `(1, 1, 1, 10)` and then create a new tensor with all the `1` dimensions removed to be left with a tensor of shape `(10)`. Set the seed to `7` when you create it and print out the first tensor and it's shape as well as the second tensor and it's shape. The output should look something like:\n", + "\n", + "```python\n", "tensor([[[[0.5349, 0.1988, 0.6592, 0.6569, 0.2328, 0.4251, 0.2071, 0.6297,\n", " 0.3653, 0.8513]]]]) torch.Size([1, 1, 1, 10])\n", "tensor([0.5349, 0.1988, 0.6592, 0.6569, 0.2328, 0.4251, 0.2071, 0.6297, 0.3653,\n", " 0.8513]) torch.Size([10])\n", "```\n", "\n", - "> **Resource:** To complete these exercises, see the [exercises notebooks templates](https://github.com/mrdbourke/pytorch-deep-learning/tree/main/extras/exercises) and potential [solutions](https://github.com/mrdbourke/pytorch-deep-learning/tree/main/extras/solutions) on the course GitHub.\n", - "\n", + "> **Resource:** To complete these exercises, see the [exercises notebooks templates](https://github.com/mrdbourke/pytorch-deep-learning/tree/main/extras/exercises) and potential [solutions](https://github.com/mrdbourke/pytorch-deep-learning/tree/main/extras/solutions) on the course GitHub." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xlmBpnuPTGfH" + }, + "source": [ "## Extra-curriculum\n", "\n", "* Spend 1-hour going through the [PyTorch basics tutorial](https://pytorch.org/tutorials/beginner/basics/intro.html) (I'd recommend the [Quickstart](https://pytorch.org/tutorials/beginner/basics/quickstart_tutorial.html) and [Tensors](https://pytorch.org/tutorials/beginner/basics/tensorqs_tutorial.html) sections).\n",