diff --git a/00_pytorch_fundamentals.ipynb b/00_pytorch_fundamentals.ipynb new file mode 100644 index 0000000..76dc2e0 --- /dev/null +++ b/00_pytorch_fundamentals.ipynb @@ -0,0 +1,1839 @@ +{ + "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" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jSNK7duj5SeU" + }, + "source": [ + "# TK - intro to PyTorch\n", + "\n", + "## TK - What is PyTorch?\n", + "\n", + "## TK - What can PyTorch be used for?\n", + "\n", + "## TK - Who uses PyTorch?\n", + "\n", + "## TK - Why use PyTorch?\n", + "\n", + "Researchers love using PyTorch -- https://paperswithcode.com/trends \n", + "\n", + "## TK - What we're going to cover in this module...\n", + "\n", + "* Introduction to Tensors\n", + "* Creating tensors\n", + "* Getting information from tensors\n", + "* Manipulating tensors\n", + "* Tensor operations (neural networks involve manipulating tensors...)\n", + "* Tensors and NumPy\n", + "* Running tensors on GPU\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "1VxEOik46Y4i", + "outputId": "8b131929-d8f9-4d1e-ea1b-f2cb0d5447a4" + }, + "source": [ + "import torch\n", + "print(torch.__version__)" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "1.9.0+cu111\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "i-33BKR16iWc" + }, + "source": [ + "## TK - Introduction to tensors \n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gFF0N2TU7S7Q" + }, + "source": [ + "### TK - Creating tensors \n", + "\n", + "All you need to know - https://pytorch.org/docs/stable/generated/torch.tensor.html#torch.tensor\n", + "\n", + "Many different ops for PyTorch tensors - https://pytorch.org/docs/stable/torch.html" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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)" + ] + }, + "metadata": {}, + "execution_count": 8 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "lV98Yz868bav", + "outputId": "a571d544-d615-48ef-db49-80021dbe8efb" + }, + "source": [ + "scalar.ndim" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": {}, + "execution_count": 9 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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" + ] + }, + "metadata": {}, + "execution_count": 97 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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])" + ] + }, + "metadata": {}, + "execution_count": 10 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "03hm3VVv8kr4", + "outputId": "c0b58040-8c82-49b3-a46f-02a80859f4dc" + }, + "source": [ + "vector.ndim" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "1" + ] + }, + "metadata": {}, + "execution_count": 11 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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]])" + ] + }, + "metadata": {}, + "execution_count": 12 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "8LREUbeb8r8j", + "outputId": "575a9f1c-d552-469f-9678-71c17f35794e" + }, + "source": [ + "matrix.ndim" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "2" + ] + }, + "metadata": {}, + "execution_count": 13 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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", + " [3, 6, 9],\n", + " [3, 6, 9]]])" + ] + }, + "metadata": {}, + "execution_count": 16 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "8dhuEsjS8QcT", + "outputId": "2a08eb90-3625-4b03-b004-bc8f3556669b" + }, + "source": [ + "tensor.ndim" + ], + "execution_count": 17, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": {}, + "execution_count": 17 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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", + "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]))" + ] + }, + "metadata": {}, + "execution_count": 19 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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]))" + ] + }, + "metadata": {}, + "execution_count": 20 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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]))" + ] + }, + "metadata": {}, + "execution_count": 21 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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", + " [0.6924, 0.6208, 0.8857, 0.1900],\n", + " [0.7239, 0.4225, 0.0904, 0.8145]]), torch.float32)" + ] + }, + "metadata": {}, + "execution_count": 27 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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", + " [0., 0., 0., 0.],\n", + " [0., 0., 0., 0.]]), torch.float32)" + ] + }, + "metadata": {}, + "execution_count": 29 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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])" + ] + }, + "metadata": {}, + "execution_count": 30 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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])" + ] + }, + "metadata": {}, + "execution_count": 32 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "q3MoGnpw9XaF", + "outputId": "82963d1f-92ee-484b-cc1c-e4b7a4dda135" + }, + "source": [ + "# Default datatype for tensors is float32\n", + "float_32_tensor = torch.tensor([3.0, 6.0, 9.0],\n", + " dtype=None, # defaults to None, which is torch.float32 or whatever datatype is passed\n", + " device=None, # defaults to None, which uses the default tensor type\n", + " 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", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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" + ] + }, + "metadata": {}, + "execution_count": 24 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gUjkB2AX7Upz" + }, + "source": [ + "## TK - Getting information from tensors" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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", + "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", + "Shape of tensor: torch.Size([3, 4])\n", + "Datatype of tensor: torch.float32\n", + "Device tensor is stored on: cpu\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BdiWvoAi7UjL" + }, + "source": [ + "## TK - Manipulating tensors (tensor operations)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Sk_6Dd7L7Uce" + }, + "source": [ + "### Basic operations" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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])" + ] + }, + "metadata": {}, + "execution_count": 94 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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])" + ] + }, + "metadata": {}, + "execution_count": 44 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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])" + ] + }, + "metadata": {}, + "execution_count": 45 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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])" + ] + }, + "metadata": {}, + "execution_count": 46 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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])" + ] + }, + "metadata": {}, + "execution_count": 47 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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])" + ] + }, + "metadata": {}, + "execution_count": 48 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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])" + ] + }, + "metadata": {}, + "execution_count": 49 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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])" + ] + }, + "metadata": {}, + "execution_count": 55 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TT5fVuyu7q5z" + }, + "source": [ + "### Matrix multiplcation\n", + "\n", + "Neural networks are mostly matrix multiplcation - https://marksaroufim.substack.com/p/working-class-deep-learner\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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])" + ] + }, + "metadata": {}, + "execution_count": 50 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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)" + ] + }, + "metadata": {}, + "execution_count": 51 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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)" + ] + }, + "metadata": {}, + "execution_count": 54 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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)" + ] + }, + "metadata": {}, + "execution_count": 58 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "6qMSaLOoJscL", + "outputId": "debdac67-169b-4217-f654-f9b0fdfc2c87" + }, + "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", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 196 + }, + "id": "rN5RcoD4Jo6y", + "outputId": "459e3ab8-1233-43ac-d097-aa7ad10d8f58" + }, + "source": [ + "# Shapes need to be in the right way (this will error) \n", + "tensor_A = torch.tensor([[1, 2],\n", + " [3, 4],\n", + " [5, 6]], dtype=torch.float32)\n", + "\n", + "tensor_B = torch.tensor([[7, 8],\n", + " [9, 10], \n", + " [11, 12]], dtype=torch.float32)\n", + "\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", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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", + " [ 53., 67., 81.],\n", + " [ 83., 105., 127.]])" + ] + }, + "metadata": {}, + "execution_count": 67 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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]))" + ] + }, + "metadata": {}, + "execution_count": 79 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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])" + ] + }, + "metadata": {}, + "execution_count": 80 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pjMmrJOOPv5e" + }, + "source": [ + "### TK - Finding the min, max, mean, sum, etc (aggregation)\n", + "\n", + "See here for more operations - https://pytorch.org/docs/stable/tensors.html\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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])" + ] + }, + "metadata": {}, + "execution_count": 103 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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", + "text": [ + "Minimum: 0\n", + "Maximum: 90\n", + "Mean: 45.0\n", + "Sum: 450\n" + ] + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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))" + ] + }, + "metadata": {}, + "execution_count": 106 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "i7ApCaZjDkvp" + }, + "source": [ + "### TK - Positional min/max" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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))" + ] + }, + "metadata": {}, + "execution_count": 83 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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))" + ] + }, + "metadata": {}, + "execution_count": 86 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QBu33WihOXBk" + }, + "source": [ + "### TK - Change tensor datatype\n", + "\n", + "See torch datatypes here - https://pytorch.org/docs/stable/tensors.html " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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" + ] + }, + "metadata": {}, + "execution_count": 87 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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)" + ] + }, + "metadata": {}, + "execution_count": 92 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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)" + ] + }, + "metadata": {}, + "execution_count": 93 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7CkCtAYmGsHY" + }, + "source": [ + "### TK - Reshaping, stacking, squeezing and unsqueezing\n", + "\n", + "Why do any of these?\n", + "\n", + "Avoid for loops in your tensor code.\n", + "\n", + "For loops slow things down." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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.])" + ] + }, + "metadata": {}, + "execution_count": 107 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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]))" + ] + }, + "metadata": {}, + "execution_count": 115 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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", + " [1., 2., 3., 4., 5., 6., 7.],\n", + " [1., 2., 3., 4., 5., 6., 7.],\n", + " [1., 2., 3., 4., 5., 6., 7.]])" + ] + }, + "metadata": {}, + "execution_count": 123 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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]))" + ] + }, + "metadata": {}, + "execution_count": 116 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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]))" + ] + }, + "metadata": {}, + "execution_count": 120 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "h8ZaW0Bq7rCm" + }, + "source": [ + "## TK - PyTorch tensors & NumPy" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "yDrDCnvY7rKS", + "outputId": "0344f8be-632b-40d9-9d48-afdeebf1b804" + }, + "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", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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]))" + ] + }, + "metadata": {}, + "execution_count": 131 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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))" + ] + }, + "metadata": {}, + "execution_count": 134 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "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))" + ] + }, + "metadata": {}, + "execution_count": 135 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7gU3ubCrUkI-" + }, + "source": [ + "## TK - Reproducibility\n", + "\n", + "* Create tensors with same values...\n", + "* Show PyTorch random seed so we can all use similar values\n", + "\n", + "See: https://pytorch.org/docs/stable/notes/randomness.html" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hxIIM7t27rQ-" + }, + "source": [ + "## TK - Running tensors on GPUs " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "OweDLgwjEvZ2", + "outputId": "959a6350-9eee-4a02-d66c-1a870f9f1d9b" + }, + "source": [ + "# Check for GPU\n", + "import torch\n", + "torch.cuda.is_available()" + ], + "execution_count": 137, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": {}, + "execution_count": 137 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_QTHdTruUUy4" + }, + "source": [ + "UPTOHERE:\n", + "* get tensors on GPU\n", + "* show how to go back and forth between GPU and non-GPU\n", + "* go back through headings and make sure code is good to go...\n" + ] + }, + { + "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": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "ulZG668QFVgX" + }, + "source": [ + "\n" + ], + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file