From 2c17629505a9b2905f0cd78075b63355ef33f541 Mon Sep 17 00:00:00 2001 From: Daniel Bourke Date: Tue, 19 Oct 2021 13:14:01 +1000 Subject: [PATCH] update fundamentals notebook --- 00_pytorch_fundamentals.ipynb | 1243 +++++++++++++++++++-------------- 1 file changed, 730 insertions(+), 513 deletions(-) diff --git a/00_pytorch_fundamentals.ipynb b/00_pytorch_fundamentals.ipynb index 76dc2e0..9f3aaa1 100644 --- a/00_pytorch_fundamentals.ipynb +++ b/00_pytorch_fundamentals.ipynb @@ -1,28 +1,10 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "00_pytorch_fundamentals.ipynb", - "provenance": [], - "collapsed_sections": [], - "authorship_tag": "ABX9TyPOZajF83061txEjr/v7czv", - "include_colab_link": true - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, "cells": [ { "cell_type": "markdown", "metadata": { - "id": "view-in-github", - "colab_type": "text" + "colab_type": "text", + "id": "view-in-github" }, "source": [ "\"Open" @@ -62,6 +44,7 @@ }, { "cell_type": "code", + "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -69,19 +52,18 @@ "id": "1VxEOik46Y4i", "outputId": "8b131929-d8f9-4d1e-ea1b-f2cb0d5447a4" }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.9.1\n" + ] + } + ], "source": [ "import torch\n", "print(torch.__version__)" - ], - "execution_count": 2, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "1.9.0+cu111\n" - ] - } ] }, { @@ -108,6 +90,7 @@ }, { "cell_type": "code", + "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -115,27 +98,27 @@ "id": "YUDgG2zk7Us5", "outputId": "b53cc251-6458-47a2-ae77-3006c045a2da" }, - "source": [ - "# Scalar\n", - "scalar = torch.tensor(7)\n", - "scalar" - ], - "execution_count": 8, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor(7)" ] }, + "execution_count": 2, "metadata": {}, - "execution_count": 8 + "output_type": "execute_result" } + ], + "source": [ + "# Scalar\n", + "scalar = torch.tensor(7)\n", + "scalar" ] }, { "cell_type": "code", + "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -143,25 +126,25 @@ "id": "lV98Yz868bav", "outputId": "a571d544-d615-48ef-db49-80021dbe8efb" }, - "source": [ - "scalar.ndim" - ], - "execution_count": 9, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "0" ] }, + "execution_count": 3, "metadata": {}, - "execution_count": 9 + "output_type": "execute_result" } + ], + "source": [ + "scalar.ndim" ] }, { "cell_type": "code", + "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -169,26 +152,26 @@ "id": "-k4cyKumPfbE", "outputId": "0756a55c-9b9b-4f52-a832-9abbafe689e0" }, - "source": [ - "# Get the Python number within a tensor (only works with one-element tensors)\n", - "scalar.item()" - ], - "execution_count": 97, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "7" ] }, + "execution_count": 4, "metadata": {}, - "execution_count": 97 + "output_type": "execute_result" } + ], + "source": [ + "# Get the Python number within a tensor (only works with one-element tensors)\n", + "scalar.item()" ] }, { "cell_type": "code", + "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -196,27 +179,27 @@ "id": "-IZF6ASs8QH9", "outputId": "43f9e4b0-e836-4b13-9ebd-94c689a12e86" }, - "source": [ - "# Vector\n", - "vector = torch.tensor([7, 7])\n", - "vector" - ], - "execution_count": 10, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([7, 7])" ] }, + "execution_count": 5, "metadata": {}, - "execution_count": 10 + "output_type": "execute_result" } + ], + "source": [ + "# Vector\n", + "vector = torch.tensor([7, 7])\n", + "vector" ] }, { "cell_type": "code", + "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -224,25 +207,25 @@ "id": "03hm3VVv8kr4", "outputId": "c0b58040-8c82-49b3-a46f-02a80859f4dc" }, - "source": [ - "vector.ndim" - ], - "execution_count": 11, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "1" ] }, + "execution_count": 6, "metadata": {}, - "execution_count": 11 + "output_type": "execute_result" } + ], + "source": [ + "vector.ndim" ] }, { "cell_type": "code", + "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -250,29 +233,29 @@ "id": "D5iNwCYL8QO9", "outputId": "24e6d331-943c-42dc-e258-6a4780ee97f9" }, - "source": [ - "# Matrix\n", - "matrix = torch.tensor([[7, 8], \n", - " [9, 10]])\n", - "matrix" - ], - "execution_count": 12, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([[ 7, 8],\n", " [ 9, 10]])" ] }, + "execution_count": 7, "metadata": {}, - "execution_count": 12 + "output_type": "execute_result" } + ], + "source": [ + "# Matrix\n", + "matrix = torch.tensor([[7, 8], \n", + " [9, 10]])\n", + "matrix" ] }, { "cell_type": "code", + "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -280,25 +263,25 @@ "id": "8LREUbeb8r8j", "outputId": "575a9f1c-d552-469f-9678-71c17f35794e" }, - "source": [ - "matrix.ndim" - ], - "execution_count": 13, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "2" ] }, + "execution_count": 8, "metadata": {}, - "execution_count": 13 + "output_type": "execute_result" } + ], + "source": [ + "matrix.ndim" ] }, { "cell_type": "code", + "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -306,17 +289,8 @@ "id": "wEMDQr188QWW", "outputId": "0394c6bf-4dde-41e0-80ed-3b0d4ca7c85d" }, - "source": [ - "# Tensor\n", - "tensor = torch.tensor([[[3, 6, 9],\n", - " [3, 6, 9],\n", - " [3, 6, 9]]])\n", - "tensor" - ], - "execution_count": 16, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([[[3, 6, 9],\n", @@ -324,13 +298,22 @@ " [3, 6, 9]]])" ] }, + "execution_count": 9, "metadata": {}, - "execution_count": 16 + "output_type": "execute_result" } + ], + "source": [ + "# Tensor\n", + "tensor = torch.tensor([[[3, 6, 9],\n", + " [3, 6, 9],\n", + " [3, 6, 9]]])\n", + "tensor" ] }, { "cell_type": "code", + "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -338,25 +321,25 @@ "id": "8dhuEsjS8QcT", "outputId": "2a08eb90-3625-4b03-b004-bc8f3556669b" }, - "source": [ - "tensor.ndim" - ], - "execution_count": 17, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "3" ] }, + "execution_count": 10, "metadata": {}, - "execution_count": 17 + "output_type": "execute_result" } + ], + "source": [ + "tensor.ndim" ] }, { "cell_type": "code", + "execution_count": 19, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -364,37 +347,37 @@ "id": "xRnzgN2F9K0B", "outputId": "40a5889e-a82e-4037-d7fb-b7ceb0b1710e" }, - "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" - ], - "execution_count": 19, "outputs": [ { - "output_type": "stream", "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" ] }, { - "output_type": "execute_result", "data": { "text/plain": [ "(tensor([1, 2, 3]), tensor([1, 2, 3]))" ] }, + "execution_count": 19, "metadata": {}, - "execution_count": 19 + "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/" @@ -402,28 +385,28 @@ "id": "bK4B-xpT9op8", "outputId": "8d43c184-de2d-497a-8b93-8192053b31fd" }, - "source": [ - "# Change tensor_B, doesn't change tensor_A\n", - "tensor_B[0] = 4\n", - "\n", - "tensor_A, tensor_B" - ], - "execution_count": 20, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "(tensor([1, 2, 3]), tensor([4, 2, 3]))" ] }, + "execution_count": 20, "metadata": {}, - "execution_count": 20 + "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/" @@ -431,28 +414,28 @@ "id": "GCbBkdKa96aO", "outputId": "5ba5a1eb-5a4e-438e-8271-6ca886738901" }, - "source": [ - "# Change tensor_A, doesn't change tensor_B\n", - "tensor_A[1] = 4\n", - "\n", - "tensor_A, tensor_B" - ], - "execution_count": 21, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "(tensor([1, 4, 3]), tensor([4, 2, 3]))" ] }, + "execution_count": 21, "metadata": {}, - "execution_count": 21 + "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, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -460,15 +443,8 @@ "id": "EOJEtDx--GnK", "outputId": "a717476d-4199-4ba9-859c-0fd788c456c5" }, - "source": [ - "# Random\n", - "random_tensor = torch.rand(size=(3, 4))\n", - "random_tensor, random_tensor.dtype" - ], - "execution_count": 27, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "(tensor([[0.6109, 0.1709, 0.9683, 0.5726],\n", @@ -476,13 +452,20 @@ " [0.7239, 0.4225, 0.0904, 0.8145]]), torch.float32)" ] }, + "execution_count": 27, "metadata": {}, - "execution_count": 27 + "output_type": "execute_result" } + ], + "source": [ + "# Random\n", + "random_tensor = torch.rand(size=(3, 4))\n", + "random_tensor, random_tensor.dtype" ] }, { "cell_type": "code", + "execution_count": 29, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -490,15 +473,8 @@ "id": "oCzhd0hl9Vp6", "outputId": "ffcbcc86-ef8d-4284-d6ca-c1c6df7c5bff" }, - "source": [ - "# Zeros\n", - "zeros = torch.zeros(size=(3, 4))\n", - "zeros, zeros.dtype" - ], - "execution_count": 29, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "(tensor([[0., 0., 0., 0.],\n", @@ -506,13 +482,20 @@ " [0., 0., 0., 0.]]), torch.float32)" ] }, + "execution_count": 29, "metadata": {}, - "execution_count": 29 + "output_type": "execute_result" } + ], + "source": [ + "# Zeros\n", + "zeros = torch.zeros(size=(3, 4))\n", + "zeros, zeros.dtype" ] }, { "cell_type": "code", + "execution_count": 30, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -520,27 +503,27 @@ "id": "1IqUs81d9W4W", "outputId": "20c17956-9d58-4757-c786-cb089d2a9551" }, - "source": [ - "# torch.arange(), torch.range() is deprecated \n", - "zero_to_ten = torch.arange(0, 10)\n", - "zero_to_ten" - ], - "execution_count": 30, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, + "execution_count": 30, "metadata": {}, - "execution_count": 30 + "output_type": "execute_result" } + ], + "source": [ + "# torch.arange(), torch.range() is deprecated \n", + "zero_to_ten = torch.arange(0, 10)\n", + "zero_to_ten" ] }, { "cell_type": "code", + "execution_count": 32, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -548,27 +531,27 @@ "id": "ZvXwUut5BhHq", "outputId": "516baed2-790a-440a-92c0-c9201d5675e2" }, - "source": [ - "# Can also create a tensor of zeros similar to another tensor\n", - "ten_zeros = torch.zeros_like(zero_to_ten) # will have same shape\n", - "ten_zeros" - ], - "execution_count": 32, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])" ] }, + "execution_count": 32, "metadata": {}, - "execution_count": 32 + "output_type": "execute_result" } + ], + "source": [ + "# Can also create a tensor of zeros similar to another tensor\n", + "ten_zeros = torch.zeros_like(zero_to_ten) # will have same shape\n", + "ten_zeros" ] }, { "cell_type": "code", + "execution_count": 23, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -576,6 +559,18 @@ "id": "q3MoGnpw9XaF", "outputId": "82963d1f-92ee-484b-cc1c-e4b7a4dda135" }, + "outputs": [ + { + "data": { + "text/plain": [ + "(torch.Size([3]), torch.float32, device(type='cpu'))" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Default datatype for tensors is float32\n", "float_32_tensor = torch.tensor([3.0, 6.0, 9.0],\n", @@ -584,23 +579,11 @@ " requires_grad=False) # if True, operations perfromed on the tensor are recorded \n", "\n", "float_32_tensor.shape, float_32_tensor.dtype, float_32_tensor.device" - ], - "execution_count": 23, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "(torch.Size([3]), torch.float32, device(type='cpu'))" - ] - }, - "metadata": {}, - "execution_count": 23 - } ] }, { "cell_type": "code", + "execution_count": 24, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -608,24 +591,23 @@ "id": "PKSuajld_09s", "outputId": "00a90334-bd19-4759-eea9-6f92ec278e92" }, - "source": [ - "float_16_tensor = torch.tensor([3.0, 6.0, 9.0],\n", - " dtype=torch.float16)\n", - "\n", - "float_16_tensor.dtype" - ], - "execution_count": 24, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "torch.float16" ] }, + "execution_count": 24, "metadata": {}, - "execution_count": 24 + "output_type": "execute_result" } + ], + "source": [ + "float_16_tensor = torch.tensor([3.0, 6.0, 9.0],\n", + " dtype=torch.float16)\n", + "\n", + "float_16_tensor.dtype" ] }, { @@ -639,6 +621,7 @@ }, { "cell_type": "code", + "execution_count": 34, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -646,19 +629,10 @@ "id": "hd_X4D0j7Umq", "outputId": "368cde26-906b-4682-a401-e59d06e6952a" }, - "source": [ - "some_tensor = torch.rand(3, 4)\n", - "\n", - "print(some_tensor)\n", - "print(f\"Shape of tensor: {some_tensor.shape}\")\n", - "print(f\"Datatype of tensor: {some_tensor.dtype}\")\n", - "print(f\"Device tensor is stored on: {some_tensor.device}\") # will default to CPU" - ], - "execution_count": 34, "outputs": [ { - "output_type": "stream", "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", @@ -668,6 +642,14 @@ "Device tensor is stored on: cpu\n" ] } + ], + "source": [ + "some_tensor = torch.rand(3, 4)\n", + "\n", + "print(some_tensor)\n", + "print(f\"Shape of tensor: {some_tensor.shape}\")\n", + "print(f\"Datatype of tensor: {some_tensor.dtype}\")\n", + "print(f\"Device tensor is stored on: {some_tensor.device}\") # will default to CPU" ] }, { @@ -690,6 +672,7 @@ }, { "cell_type": "code", + "execution_count": 94, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -697,26 +680,26 @@ "id": "X71WpQoPD7a4", "outputId": "f7eab2bf-ba9b-4612-9cfa-e8d80093cc20" }, - "source": [ - "tensor = torch.tensor([1, 2, 3])\n", - "tensor + 10" - ], - "execution_count": 94, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([11, 12, 13])" ] }, + "execution_count": 94, "metadata": {}, - "execution_count": 94 + "output_type": "execute_result" } + ], + "source": [ + "tensor = torch.tensor([1, 2, 3])\n", + "tensor + 10" ] }, { "cell_type": "code", + "execution_count": 44, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -724,25 +707,25 @@ "id": "Sp4TlTWWEFeO", "outputId": "db58e2e0-fe7e-4670-f61a-a6ff00f33250" }, - "source": [ - "tensor * 10" - ], - "execution_count": 44, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([10, 20, 30])" ] }, + "execution_count": 44, "metadata": {}, - "execution_count": 44 + "output_type": "execute_result" } + ], + "source": [ + "tensor * 10" ] }, { "cell_type": "code", + "execution_count": 45, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -750,26 +733,26 @@ "id": "XuB1UjCIEJIA", "outputId": "063b6c17-8d2f-4627-8e7d-932af58b0cd5" }, - "source": [ - "# Tensors don't change unless reassigned\n", - "tensor" - ], - "execution_count": 45, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([1, 2, 3])" ] }, + "execution_count": 45, "metadata": {}, - "execution_count": 45 + "output_type": "execute_result" } + ], + "source": [ + "# Tensors don't change unless reassigned\n", + "tensor" ] }, { "cell_type": "code", + "execution_count": 46, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -777,26 +760,26 @@ "id": "U4iWKoLsENry", "outputId": "c8751a66-fa34-40bd-db94-2a3c81ebc99a" }, - "source": [ - "tensor = tensor - 10\n", - "tensor" - ], - "execution_count": 46, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([-9, -8, -7])" ] }, + "execution_count": 46, "metadata": {}, - "execution_count": 46 + "output_type": "execute_result" } + ], + "source": [ + "tensor = tensor - 10\n", + "tensor" ] }, { "cell_type": "code", + "execution_count": 47, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -804,26 +787,26 @@ "id": "tFgZY-PaFNXa", "outputId": "160484d4-768b-4a30-f3a9-c5ae4ded8735" }, - "source": [ - "tensor = tensor + 10\n", - "tensor" - ], - "execution_count": 47, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([1, 2, 3])" ] }, + "execution_count": 47, "metadata": {}, - "execution_count": 47 + "output_type": "execute_result" } + ], + "source": [ + "tensor = tensor + 10\n", + "tensor" ] }, { "cell_type": "code", + "execution_count": 48, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -831,26 +814,26 @@ "id": "uVysdk3kFWbY", "outputId": "0c5f56c5-869b-4a05-819c-4467772ee62f" }, - "source": [ - "# Can also use torch functions\n", - "torch.multiply(tensor, 10)" - ], - "execution_count": 48, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([10, 20, 30])" ] }, + "execution_count": 48, "metadata": {}, - "execution_count": 48 + "output_type": "execute_result" } + ], + "source": [ + "# Can also use torch functions\n", + "torch.multiply(tensor, 10)" ] }, { "cell_type": "code", + "execution_count": 49, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -858,26 +841,26 @@ "id": "IxuPJIpNFbqO", "outputId": "8c6df0c4-dc15-4540-8dde-1c54a867b986" }, - "source": [ - "# Original tensor is still unchanged \n", - "tensor" - ], - "execution_count": 49, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([1, 2, 3])" ] }, + "execution_count": 49, "metadata": {}, - "execution_count": 49 + "output_type": "execute_result" } + ], + "source": [ + "# Original tensor is still unchanged \n", + "tensor" ] }, { "cell_type": "code", + "execution_count": 55, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -885,22 +868,21 @@ "id": "S5v3RkR0F2Jq", "outputId": "9d7a6757-29c3-43e5-8389-f21907bcd89e" }, - "source": [ - "# Element-wise multiplication\n", - "tensor * tensor" - ], - "execution_count": 55, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([1, 4, 9])" ] }, + "execution_count": 55, "metadata": {}, - "execution_count": 55 + "output_type": "execute_result" } + ], + "source": [ + "# Element-wise multiplication\n", + "tensor * tensor" ] }, { @@ -917,6 +899,7 @@ }, { "cell_type": "code", + "execution_count": 50, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -924,26 +907,26 @@ "id": "ZE7loucmDlEM", "outputId": "49dd001e-f472-43c9-cead-8fdb3d380689" }, - "source": [ - "tensor = torch.tensor([1, 2, 3])\n", - "tensor" - ], - "execution_count": 50, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([1, 2, 3])" ] }, + "execution_count": 50, "metadata": {}, - "execution_count": 50 + "output_type": "execute_result" } + ], + "source": [ + "tensor = torch.tensor([1, 2, 3])\n", + "tensor" ] }, { "cell_type": "code", + "execution_count": 51, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -951,25 +934,25 @@ "id": "PvCBiiTTDk8y", "outputId": "6f4633ca-41af-48f7-a664-fd033a3633c1" }, - "source": [ - "torch.matmul(tensor, tensor)" - ], - "execution_count": 51, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor(14)" ] }, + "execution_count": 51, "metadata": {}, - "execution_count": 51 + "output_type": "execute_result" } + ], + "source": [ + "torch.matmul(tensor, tensor)" ] }, { "cell_type": "code", + "execution_count": 54, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -977,25 +960,25 @@ "id": "m4E_pROBDk2r", "outputId": "80bdc547-e6c6-4047-d9f2-e88dc0a26f18" }, - "source": [ - "tensor @ tensor" - ], - "execution_count": 54, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor(14)" ] }, + "execution_count": 54, "metadata": {}, - "execution_count": 54 + "output_type": "execute_result" } + ], + "source": [ + "tensor @ tensor" ] }, { "cell_type": "code", + "execution_count": 58, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1003,25 +986,25 @@ "id": "2u4IZnFSFxKQ", "outputId": "e11ee2aa-f22e-4c0d-bb82-7ec83dfd1878" }, - "source": [ - "sum(tensor * tensor.T)" - ], - "execution_count": 58, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor(14)" ] }, + "execution_count": 58, "metadata": {}, - "execution_count": 58 + "output_type": "execute_result" } + ], + "source": [ + "sum(tensor * tensor.T)" ] }, { "cell_type": "code", + "execution_count": 60, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1029,29 +1012,29 @@ "id": "6qMSaLOoJscL", "outputId": "debdac67-169b-4217-f654-f9b0fdfc2c87" }, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor(14)" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# By hand (avoid doing operations with for loops at all cost, they are computationally expensive)\n", "value = 0\n", "for i in range(len(tensor)):\n", " value += tensor[i] * tensor[i]\n", "value" - ], - "execution_count": 60, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "tensor(14)" - ] - }, - "metadata": {}, - "execution_count": 60 - } ] }, { "cell_type": "code", + "execution_count": 66, "metadata": { "colab": { "base_uri": "https://localhost:8080/", @@ -1060,6 +1043,19 @@ "id": "rN5RcoD4Jo6y", "outputId": "459e3ab8-1233-43ac-d097-aa7ad10d8f58" }, + "outputs": [ + { + "ename": "RuntimeError", + "evalue": "ignored", + "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 [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)" + ] + } + ], "source": [ "# Shapes need to be in the right way (this will error) \n", "tensor_A = torch.tensor([[1, 2],\n", @@ -1071,24 +1067,11 @@ " [11, 12]], dtype=torch.float32)\n", "\n", "torch.matmul(tensor_A, tensor_B)" - ], - "execution_count": 66, - "outputs": [ - { - "output_type": "error", - "ename": "RuntimeError", - "evalue": "ignored", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\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)" - ] - } ] }, { "cell_type": "code", + "execution_count": 67, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1096,13 +1079,8 @@ "id": "35rEIu-NKtVE", "outputId": "b1672c80-9b01-4f97-db34-74403b8a9f40" }, - "source": [ - "torch.matmul(tensor_A, tensor_B.T)" - ], - "execution_count": 67, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([[ 23., 29., 35.],\n", @@ -1110,13 +1088,18 @@ " [ 83., 105., 127.]])" ] }, + "execution_count": 67, "metadata": {}, - "execution_count": 67 + "output_type": "execute_result" } + ], + "source": [ + "torch.matmul(tensor_A, tensor_B.T)" ] }, { "cell_type": "code", + "execution_count": 79, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1124,30 +1107,30 @@ "id": "mC_MjKW1LX7T", "outputId": "78a3c4f6-ddb3-4bf9-f25f-241f6b436192" }, - "source": [ - "# This uses matrix mutliplcation...\n", - "linear = torch.nn.Linear(in_features=3, out_features=2)\n", - "input = tensor_A\n", - "output = linear(input.T)\n", - "output, output.shape" - ], - "execution_count": 79, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "(tensor([[ 1.4391, -1.4383],\n", " [ 1.5400, -1.5229]], grad_fn=), torch.Size([2, 2]))" ] }, + "execution_count": 79, "metadata": {}, - "execution_count": 79 + "output_type": "execute_result" } + ], + "source": [ + "# This uses matrix mutliplcation...\n", + "linear = torch.nn.Linear(in_features=3, out_features=2)\n", + "input = tensor_A\n", + "output = linear(input.T)\n", + "output, output.shape" ] }, { "cell_type": "code", + "execution_count": 80, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1155,21 +1138,20 @@ "id": "qATzlPYsNgMD", "outputId": "ea5c7d99-213e-4308-a7e0-50d086ec3a28" }, - "source": [ - "tensor_A.T.shape" - ], - "execution_count": 80, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "torch.Size([2, 3])" ] }, + "execution_count": 80, "metadata": {}, - "execution_count": 80 + "output_type": "execute_result" } + ], + "source": [ + "tensor_A.T.shape" ] }, { @@ -1186,6 +1168,7 @@ }, { "cell_type": "code", + "execution_count": 103, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1193,26 +1176,26 @@ "id": "jrFQbe5fP1Rk", "outputId": "166a15a9-7f55-42f4-f51f-06f3b0f35c78" }, - "source": [ - "x = torch.arange(0, 100, 10)\n", - "x" - ], - "execution_count": 103, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])" ] }, + "execution_count": 103, "metadata": {}, - "execution_count": 103 + "output_type": "execute_result" } + ], + "source": [ + "x = torch.arange(0, 100, 10)\n", + "x" ] }, { "cell_type": "code", + "execution_count": 104, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1220,17 +1203,10 @@ "id": "e5wSP9YKP3Lb", "outputId": "68b7b5da-2fd4-49a9-9298-1e685a4b0257" }, - "source": [ - "print(f\"Minimum: {x.min()}\")\n", - "print(f\"Maximum: {x.max()}\")\n", - "print(f\"Mean: {x.type(torch.float32).mean()}\") # won't work without float datatype\n", - "print(f\"Sum: {x.sum()}\")" - ], - "execution_count": 104, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Minimum: 0\n", "Maximum: 90\n", @@ -1238,10 +1214,17 @@ "Sum: 450\n" ] } + ], + "source": [ + "print(f\"Minimum: {x.min()}\")\n", + "print(f\"Maximum: {x.max()}\")\n", + "print(f\"Mean: {x.type(torch.float32).mean()}\") # won't work without float datatype\n", + "print(f\"Sum: {x.sum()}\")" ] }, { "cell_type": "code", + "execution_count": 106, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1249,21 +1232,20 @@ "id": "0Cr23Y9uP3HO", "outputId": "92dd559f-c8bf-4a15-c8b0-503a2303ad7c" }, - "source": [ - "torch.max(x), torch.min(x), torch.mean(x.type(torch.float32)), torch.sum(x)" - ], - "execution_count": 106, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "(tensor(90), tensor(0), tensor(45.), tensor(450))" ] }, + "execution_count": 106, "metadata": {}, - "execution_count": 106 + "output_type": "execute_result" } + ], + "source": [ + "torch.max(x), torch.min(x), torch.mean(x.type(torch.float32)), torch.sum(x)" ] }, { @@ -1277,6 +1259,7 @@ }, { "cell_type": "code", + "execution_count": 83, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1284,27 +1267,27 @@ "id": "FzNBl9JSGlHi", "outputId": "42b84dc7-15f2-4b19-8c4d-cb161343e52e" }, - "source": [ - "# Returns index of max value\n", - "tensor = torch.arange(10, 100, 10)\n", - "tensor, tensor.argmax()" - ], - "execution_count": 83, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "(tensor([10, 20, 30, 40, 50, 60, 70, 80, 90]), tensor(8))" ] }, + "execution_count": 83, "metadata": {}, - "execution_count": 83 + "output_type": "execute_result" } + ], + "source": [ + "# Returns index of max value\n", + "tensor = torch.arange(10, 100, 10)\n", + "tensor, tensor.argmax()" ] }, { "cell_type": "code", + "execution_count": 86, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1312,23 +1295,22 @@ "id": "QsnWtzWuOIPe", "outputId": "24d76bd3-1671-43b8-d6ad-768b830bdfaa" }, - "source": [ - "# Returns index of min value\n", - "tensor = torch.arange(10, 100, 10)\n", - "tensor, tensor.argmin()" - ], - "execution_count": 86, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "(tensor([10, 20, 30, 40, 50, 60, 70, 80, 90]), tensor(0))" ] }, + "execution_count": 86, "metadata": {}, - "execution_count": 86 + "output_type": "execute_result" } + ], + "source": [ + "# Returns index of min value\n", + "tensor = torch.arange(10, 100, 10)\n", + "tensor, tensor.argmin()" ] }, { @@ -1344,6 +1326,7 @@ }, { "cell_type": "code", + "execution_count": 87, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1351,26 +1334,26 @@ "id": "rY2FEsCAOaLu", "outputId": "fa6b90bf-60a4-4769-b6a9-b29c080a6df6" }, - "source": [ - "tensor = torch.arange(10., 100., 10.)\n", - "tensor.dtype" - ], - "execution_count": 87, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "torch.float32" ] }, + "execution_count": 87, "metadata": {}, - "execution_count": 87 + "output_type": "execute_result" } + ], + "source": [ + "tensor = torch.arange(10., 100., 10.)\n", + "tensor.dtype" ] }, { "cell_type": "code", + "execution_count": 92, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1378,26 +1361,26 @@ "id": "Cac8gRYjOeab", "outputId": "e160bb02-9961-4c34-9ef3-d0acfcd4ca4d" }, - "source": [ - "tensor_float16 = tensor.type(torch.float16)\n", - "tensor_float16" - ], - "execution_count": 92, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([10., 20., 30., 40., 50., 60., 70., 80., 90.], dtype=torch.float16)" ] }, + "execution_count": 92, "metadata": {}, - "execution_count": 92 + "output_type": "execute_result" } + ], + "source": [ + "tensor_float16 = tensor.type(torch.float16)\n", + "tensor_float16" ] }, { "cell_type": "code", + "execution_count": 93, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1405,22 +1388,21 @@ "id": "8Yqovld2Oj6s", "outputId": "f501e583-feed-447c-f8ee-9fe56bf6c2e5" }, - "source": [ - "tensor_int32 = tensor.type(torch.int32)\n", - "tensor_int32" - ], - "execution_count": 93, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([10, 20, 30, 40, 50, 60, 70, 80, 90], dtype=torch.int32)" ] }, + "execution_count": 93, "metadata": {}, - "execution_count": 93 + "output_type": "execute_result" } + ], + "source": [ + "tensor_int32 = tensor.type(torch.int32)\n", + "tensor_int32" ] }, { @@ -1440,6 +1422,7 @@ }, { "cell_type": "code", + "execution_count": 107, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1447,26 +1430,26 @@ "id": "EYjRTLOzG4Ev", "outputId": "207416f6-9862-4a20-c216-290f04c84408" }, - "source": [ - "x = torch.arange(1., 8.)\n", - "x" - ], - "execution_count": 107, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([1., 2., 3., 4., 5., 6., 7.])" ] }, + "execution_count": 107, "metadata": {}, - "execution_count": 107 + "output_type": "execute_result" } + ], + "source": [ + "x = torch.arange(1., 8.)\n", + "x" ] }, { "cell_type": "code", + "execution_count": 115, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1474,27 +1457,27 @@ "id": "US4WjpQ3SG-8", "outputId": "44e3fb5c-2cbf-439e-e293-ddc7475a3660" }, - "source": [ - "# Add an extra dimension\n", - "x_reshaped = x.reshape(1, 7)\n", - "x_reshaped, x_reshaped.shape" - ], - "execution_count": 115, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "(tensor([[1., 2., 3., 4., 5., 6., 7.]]), torch.Size([1, 7]))" ] }, + "execution_count": 115, "metadata": {}, - "execution_count": 115 + "output_type": "execute_result" } + ], + "source": [ + "# Add an extra dimension\n", + "x_reshaped = x.reshape(1, 7)\n", + "x_reshaped, x_reshaped.shape" ] }, { "cell_type": "code", + "execution_count": 123, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1502,15 +1485,8 @@ "id": "pX5Adf3ORiTK", "outputId": "60b3ae0d-b819-4765-9987-70a71ec4bc78" }, - "source": [ - "# Stack tensors on top of each other\n", - "x_stacked = torch.stack([x, x, x, x], dim=0)\n", - "x_stacked" - ], - "execution_count": 123, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "tensor([[1., 2., 3., 4., 5., 6., 7.],\n", @@ -1519,13 +1495,20 @@ " [1., 2., 3., 4., 5., 6., 7.]])" ] }, + "execution_count": 123, "metadata": {}, - "execution_count": 123 + "output_type": "execute_result" } + ], + "source": [ + "# Stack tensors on top of each other\n", + "x_stacked = torch.stack([x, x, x, x], dim=0)\n", + "x_stacked" ] }, { "cell_type": "code", + "execution_count": 116, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1533,27 +1516,27 @@ "id": "w2Y2HEoDRxJZ", "outputId": "7fdf9ae2-250f-463c-9b93-38246e992fbf" }, - "source": [ - "# Remove extra dimension from x_reshaped\n", - "x_squeezed = x_reshaped.squeeze()\n", - "x_squeezed, x_squeezed.shape" - ], - "execution_count": 116, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "(tensor([1., 2., 3., 4., 5., 6., 7.]), torch.Size([7]))" ] }, + "execution_count": 116, "metadata": {}, - "execution_count": 116 + "output_type": "execute_result" } + ], + "source": [ + "# Remove extra dimension from x_reshaped\n", + "x_squeezed = x_reshaped.squeeze()\n", + "x_squeezed, x_squeezed.shape" ] }, { "cell_type": "code", + "execution_count": 120, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1561,23 +1544,22 @@ "id": "CUC-DEEwSYv7", "outputId": "adc36fde-58ce-472f-af8f-2e6362f0a360" }, - "source": [ - "# Add an extra dimension with unsqueeze\n", - "x_unsqueezed = x_squeezed.unsqueeze(dim=0)\n", - "x_unsqueezed, x_unsqueezed.shape" - ], - "execution_count": 120, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "(tensor([[1., 2., 3., 4., 5., 6., 7.]]), torch.Size([1, 7]))" ] }, + "execution_count": 120, "metadata": {}, - "execution_count": 120 + "output_type": "execute_result" } + ], + "source": [ + "# Add an extra dimension with unsqueeze\n", + "x_unsqueezed = x_squeezed.unsqueeze(dim=0)\n", + "x_unsqueezed, x_unsqueezed.shape" ] }, { @@ -1591,6 +1573,7 @@ }, { "cell_type": "code", + "execution_count": 130, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1598,29 +1581,29 @@ "id": "yDrDCnvY7rKS", "outputId": "0344f8be-632b-40d9-9d48-afdeebf1b804" }, + "outputs": [ + { + "data": { + "text/plain": [ + "(array([1, 2, 3, 4, 5, 6, 7]), tensor([1, 2, 3, 4, 5, 6, 7]))" + ] + }, + "execution_count": 130, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# NumPy array to tensor\n", "import numpy as np\n", "array = np.arange(1, 8)\n", "tensor = torch.from_numpy(array)\n", "array, tensor" - ], - "execution_count": 130, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "(array([1, 2, 3, 4, 5, 6, 7]), tensor([1, 2, 3, 4, 5, 6, 7]))" - ] - }, - "metadata": {}, - "execution_count": 130 - } ] }, { "cell_type": "code", + "execution_count": 131, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1628,27 +1611,27 @@ "id": "ovwl7VCREv8L", "outputId": "5c91968e-a9f9-4434-d950-a22a78d5a12e" }, - "source": [ - "# Change the array, keep the tensor\n", - "array = array + 1\n", - "array, tensor" - ], - "execution_count": 131, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "(array([2, 3, 4, 5, 6, 7, 8]), tensor([1, 2, 3, 4, 5, 6, 7]))" ] }, + "execution_count": 131, "metadata": {}, - "execution_count": 131 + "output_type": "execute_result" } + ], + "source": [ + "# Change the array, keep the tensor\n", + "array = array + 1\n", + "array, tensor" ] }, { "cell_type": "code", + "execution_count": 134, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1656,29 +1639,29 @@ "id": "xw_7ZyVaTKxQ", "outputId": "340fc9a4-2a46-48af-fd9e-1bc1d6d6c7e5" }, - "source": [ - "# Tensor to NumPy array\n", - "tensor = torch.ones(7)\n", - "numpy_tensor = tensor.numpy()\n", - "tensor, numpy_tensor" - ], - "execution_count": 134, "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": 134, "metadata": {}, - "execution_count": 134 + "output_type": "execute_result" } + ], + "source": [ + "# Tensor to NumPy array\n", + "tensor = torch.ones(7)\n", + "numpy_tensor = tensor.numpy()\n", + "tensor, numpy_tensor" ] }, { "cell_type": "code", + "execution_count": 135, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1686,24 +1669,23 @@ "id": "mMp6ZSkET4_Y", "outputId": "a9d5e467-b35e-4aca-b1d7-3fc7034ebd20" }, - "source": [ - "# Change the tensor, keep the array the same\n", - "tensor = tensor + 1\n", - "tensor, numpy_tensor" - ], - "execution_count": 135, "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": 135, "metadata": {}, - "execution_count": 135 + "output_type": "execute_result" } + ], + "source": [ + "# Change the tensor, keep the array the same\n", + "tensor = tensor + 1\n", + "tensor, numpy_tensor" ] }, { @@ -1720,17 +1702,84 @@ "See: https://pytorch.org/docs/stable/notes/randomness.html" ] }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([[False, False, False, False],\n", + " [False, False, False, False],\n", + " [False, False, False, False]])" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# No seed\n", + "import torch\n", + "\n", + "random_tensor_A = torch.rand(3, 4)\n", + "random_tensor_B = torch.rand(3, 4)\n", + "\n", + "random_tensor_A == random_tensor_B" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([[True, True, True, True],\n", + " [True, True, True, True],\n", + " [True, True, True, True]])" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# With seed\n", + "import torch\n", + "\n", + "# Set the random seed\n", + "torch.random.manual_seed(42)\n", + "random_tensor_C = torch.rand(3, 4)\n", + "\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", + "\n", + "random_tensor_C == random_tesnor_D\n" + ] + }, { "cell_type": "markdown", "metadata": { "id": "hxIIM7t27rQ-" }, "source": [ - "## TK - Running tensors on GPUs " + "## TK - Running tensors on GPUs \n", + "\n", + "The cuda module is what you'll want:\n", + "\n", + "* https://pytorch.org/docs/stable/cuda.html\n", + "* Best practices for running device-agnostic PyTorch - https://pytorch.org/docs/master/notes/cuda.html#device-agnostic-code" ] }, { "cell_type": "code", + "execution_count": 42, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1738,23 +1787,206 @@ "id": "OweDLgwjEvZ2", "outputId": "959a6350-9eee-4a02-d66c-1a870f9f1d9b" }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Check for GPU\n", "import torch\n", "torch.cuda.is_available()" - ], - "execution_count": 137, + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "id": "j92HBCKB7rYa" + }, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ - "False" + "'cuda'" ] }, + "execution_count": 14, "metadata": {}, - "execution_count": 137 + "output_type": "execute_result" } + ], + "source": [ + "# Set device type\n", + "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", + "device" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Count number of devices\n", + "torch.cuda.device_count()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XqQLcuj68OA-" + }, + "source": [ + "You can put tensors on the GPU using [`to()`](https://pytorch.org/docs/stable/generated/torch.Tensor.to.html).\n", + "\n", + "GPUs offer far faster numerical computing than CPUs do." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "id": "FhI3srFXEHfP" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([1, 2, 3]) cpu\n" + ] + }, + { + "data": { + "text/plain": [ + "tensor([1, 2, 3], device='cuda:0')" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create tensor (default on CPU)\n", + "tensor = torch.tensor([1, 2, 3])\n", + "\n", + "# Tensor not on GPU\n", + "print(tensor, tensor.device)\n", + "\n", + "# Move tensor to GPU\n", + "tensor_on_gpu = tensor.to(device)\n", + "tensor_on_gpu" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What if we wanted to move the tensor back to CPU?\n", + "\n", + "For example, you'll want to do this if you want to interact with your tensors with NumPy.\n", + "\n", + "Let's try using the `numpy()` method on our `tensor_on_gpu`." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "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/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;31mTypeError\u001b[0m: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first." + ] + } + ], + "source": [ + "# If tensor is on GPU, can't transform it to NumPy (this will error)\n", + "tensor_on_gpu.numpy()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Instead, to get a tensor back to CPU and usable with NumPy we can use [`Tensor.cpu()`](https://pytorch.org/docs/stable/generated/torch.Tensor.cpu.html).\n", + "\n", + "This copies the tensor to CPU memory so it's usable with CPUs." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2, 3])" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Instead, copy the tensor back to cpu\n", + "tensor_back_on_cpu = tensor_on_gpu.cpu().numpy()\n", + "tensor_back_on_cpu" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The above returns a copy of the GPU tensor in CPU memory so the original tensor is still on GPU." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([1, 2, 3], device='cuda:0')" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tensor_on_gpu" ] }, { @@ -1771,69 +2003,54 @@ }, { "cell_type": "code", - "metadata": { - "id": "j92HBCKB7rYa" - }, - "source": [ - "device = \"gpu\" if torch.cuda.is_available() else \"cpu\"" - ], "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "XqQLcuj68OA-" - }, - "source": [ - "" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "FhI3srFXEHfP" - }, - "source": [ - "" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "Jkk72A84EId1" - }, - "source": [ - "" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", "metadata": { "id": "5Ji-FyLNFMgh" }, - "source": [ - "" - ], - "execution_count": null, - "outputs": [] + "outputs": [], + "source": [] }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "ulZG668QFVgX" }, + "outputs": [], "source": [ - "\n" - ], - "execution_count": null, - "outputs": [] + "import torch\n", + "torch.arange(1, 10, 1)" + ] } - ] -} \ No newline at end of file + ], + "metadata": { + "colab": { + "authorship_tag": "ABX9TyPOZajF83061txEjr/v7czv", + "collapsed_sections": [], + "include_colab_link": true, + "name": "00_pytorch_fundamentals.ipynb", + "provenance": [] + }, + "interpreter": { + "hash": "3fbe1355223f7b2ffc113ba3ade6a2b520cadace5d5ec3e828c83ce02eb221bf" + }, + "kernelspec": { + "display_name": "Python 3.9.7 64-bit (conda)", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}