From 45cd65862fdd078a9dcdb80af54496bd1978f1ed Mon Sep 17 00:00:00 2001 From: Karthik Sivakoti Date: Wed, 22 May 2024 22:10:37 -0500 Subject: [PATCH] Created using Colab --- PyTorch_Fundamentals.ipynb | 2827 ++++++++++++++++++++++++++++++++++++ 1 file changed, 2827 insertions(+) create mode 100644 PyTorch_Fundamentals.ipynb diff --git a/PyTorch_Fundamentals.ipynb b/PyTorch_Fundamentals.ipynb new file mode 100644 index 00000000..c7e314a4 --- /dev/null +++ b/PyTorch_Fundamentals.ipynb @@ -0,0 +1,2827 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "gpuType": "T4", + "authorship_tag": "ABX9TyOgv0AozH1FKQBD+RkgT2bV", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + }, + "accelerator": "GPU" + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "EHh5JllMh1rG", + "outputId": "f55755ad-c369-414c-85ec-6e9d4f061a02", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'2.2.1+cu121'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 1 + } + ], + "source": [ + "import torch\n", + "torch.__version__" + ] + }, + { + "cell_type": "code", + "source": [ + "print(\"I am excited to run this\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "UPlb-duwXAfz", + "outputId": "cfd687e4-1238-49f4-ab6b-ee1305b740d2" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "I am excited to run this\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "import torch\n", + "import pandas as pd\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "print(torch.__version__)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "byWVlJ9wXDSk", + "outputId": "fd74a5c4-4d4a-41b2-ef3c-562ea3e4811f" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "2.2.1+cu121\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "# **Introduction to Tensors**" + ], + "metadata": { + "id": "Osm80zoEYklS" + } + }, + { + "cell_type": "code", + "source": [ + "# scalar\n", + "scalar = torch.tensor(7)\n", + "scalar" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "-o8wvJ-VXZmI", + "outputId": "558816f5-1205-4de1-fe1f-2f96e9bd79e6" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(7)" + ] + }, + "metadata": {}, + "execution_count": 4 + } + ] + }, + { + "cell_type": "code", + "source": [ + "scalar.ndim" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "mCZ2tXC4Y_Sg", + "outputId": "2d86dbdc-56e1-45c6-d3dd-14515f2a457a" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": {}, + "execution_count": 5 + } + ] + }, + { + "cell_type": "code", + "source": [ + "scalar.item()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ssN00By0ZQgS", + "outputId": "490f40d1-5135-4969-a6d3-c8c902cdc473" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "7" + ] + }, + "metadata": {}, + "execution_count": 6 + } + ] + }, + { + "cell_type": "code", + "source": [ + "# vector\n", + "vector = torch.tensor([7, 7])\n", + "vector\n", + "#vector.ndim\n", + "#vector.item()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Bws__5wlZnmF", + "outputId": "944e38f9-5ba1-4ddc-a9c6-cfb6a19bb488" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([7, 7])" + ] + }, + "metadata": {}, + "execution_count": 7 + } + ] + }, + { + "cell_type": "code", + "source": [ + "vector.shape" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "9pjCvnsZZzNG", + "outputId": "e030a4da-8f81-4858-fbce-86da2aaafe52" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "torch.Size([2])" + ] + }, + "metadata": {}, + "execution_count": 8 + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Matrix\n", + "MATRIX = torch.tensor([[7, 8],[9, 10]])\n", + "MATRIX" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "a747hI9SaBGW", + "outputId": "af835ddb-81ff-4981-badb-441567194d15" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[ 7, 8],\n", + " [ 9, 10]])" + ] + }, + "metadata": {}, + "execution_count": 9 + } + ] + }, + { + "cell_type": "code", + "source": [ + "MATRIX.ndim" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "XdTfFa7vaRUj", + "outputId": "0fbbab9c-8263-4cad-a380-0d2a16ca499e" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "2" + ] + }, + "metadata": {}, + "execution_count": 10 + } + ] + }, + { + "cell_type": "code", + "source": [ + "MATRIX[0]\n", + "MATRIX[1]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "TFeD3jSDafm7", + "outputId": "69b44ab3-5ba7-451a-c6b2-f019a03d0c96" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([ 9, 10])" + ] + }, + "metadata": {}, + "execution_count": 11 + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Tensor\n", + "TENSOR = torch.tensor([[[1, 2, 3],[3,6,9], [2,4,5]]])\n", + "TENSOR" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ic3cE47tah42", + "outputId": "f250e295-91de-43ec-9d80-588a6fe0abde" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[[1, 2, 3],\n", + " [3, 6, 9],\n", + " [2, 4, 5]]])" + ] + }, + "metadata": {}, + "execution_count": 12 + } + ] + }, + { + "cell_type": "code", + "source": [ + "TENSOR.shape" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Wvjf5fczbAM1", + "outputId": "9c72b5b8-bafe-4ae7-9883-b051e209eada" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "torch.Size([1, 3, 3])" + ] + }, + "metadata": {}, + "execution_count": 13 + } + ] + }, + { + "cell_type": "code", + "source": [ + "TENSOR.ndim" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "mwtXZwiMbN3m", + "outputId": "331a5e36-b1b0-4a5f-a9b8-e7049cbaa8f9" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": {}, + "execution_count": 14 + } + ] + }, + { + "cell_type": "code", + "source": [ + "TENSOR[0]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "vzdZu_IfbP3J", + "outputId": "e24e7e71-e365-412d-ff50-fc094b56d2f3" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[1, 2, 3],\n", + " [3, 6, 9],\n", + " [2, 4, 5]])" + ] + }, + "metadata": {}, + "execution_count": 15 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "# RANDOM TENSOR" + ], + "metadata": { + "id": "A8OL9eWfcRrJ" + } + }, + { + "cell_type": "code", + "source": [ + "random_tensor = torch.rand(3,4)\n", + "random_tensor" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "hAqSDE1EcVS_", + "outputId": "946171c3-d054-400c-f893-79110356888c" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[0.4414, 0.7681, 0.8385, 0.3166],\n", + " [0.0468, 0.5812, 0.0670, 0.9173],\n", + " [0.2959, 0.3276, 0.7411, 0.4643]])" + ] + }, + "metadata": {}, + "execution_count": 16 + } + ] + }, + { + "cell_type": "code", + "source": [ + "random_tensor.ndim" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "g4fvPE5GcwzP", + "outputId": "8737f36b-6864-4059-eaed-6f9156c22306" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "2" + ] + }, + "metadata": {}, + "execution_count": 17 + } + ] + }, + { + "cell_type": "code", + "source": [ + "random_tensor.shape" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "XsAg99QmdAU6", + "outputId": "35467c11-257c-4f16-99aa-eca930bcbc36" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "torch.Size([3, 4])" + ] + }, + "metadata": {}, + "execution_count": 18 + } + ] + }, + { + "cell_type": "code", + "source": [ + "random_tensor.size()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "cii1pNdVdB68", + "outputId": "fc8d2de6-9215-43de-99f7-7b0d7f7d20fa" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "torch.Size([3, 4])" + ] + }, + "metadata": {}, + "execution_count": 19 + } + ] + }, + { + "cell_type": "code", + "source": [ + "random_image_tensor = torch.rand(size=(3, 224, 224)) #color channels, height, width\n", + "random_image_tensor.ndim, random_image_tensor.shape" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "aTKq2j0cdDjb", + "outputId": "6be42057-20b9-4faf-d79d-8b65c42cc27e" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(3, torch.Size([3, 224, 224]))" + ] + }, + "metadata": {}, + "execution_count": 20 + } + ] + }, + { + "cell_type": "code", + "source": [ + "random_tensor_ofownsize = torch.rand(size=(5,10,10))\n", + "random_tensor_ofownsize.ndim, random_tensor_ofownsize.shape\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "IyhDdj-Pd6nC", + "outputId": "43e5e334-6d4d-4b67-f87d-7d364c6d8c67" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(3, torch.Size([5, 10, 10]))" + ] + }, + "metadata": {}, + "execution_count": 21 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Zeroes and Ones tensor" + ], + "metadata": { + "id": "UOJW08uOert_" + } + }, + { + "cell_type": "code", + "source": [ + "zero = torch.zeros(size=(3, 4))\n", + "zero" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "uGvXtaXyefie", + "outputId": "d40d3e28-8667-4d2f-8b62-f0829c6162ad" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[0., 0., 0., 0.],\n", + " [0., 0., 0., 0.],\n", + " [0., 0., 0., 0.]])" + ] + }, + "metadata": {}, + "execution_count": 22 + } + ] + }, + { + "cell_type": "code", + "source": [ + "zero*random_tensor" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "OyUkUPkDe0uH", + "outputId": "26c2e4be-36ba-4c6c-9a90-2704ec135828" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[0., 0., 0., 0.],\n", + " [0., 0., 0., 0.],\n", + " [0., 0., 0., 0.]])" + ] + }, + "metadata": {}, + "execution_count": 23 + } + ] + }, + { + "cell_type": "code", + "source": [ + "ones = torch.ones(size=(3, 4))\n", + "ones\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "y_Ac62Aqe82G", + "outputId": "291de5d9-b9df-49de-c9d1-d098e3e9f4d8" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[1., 1., 1., 1.],\n", + " [1., 1., 1., 1.],\n", + " [1., 1., 1., 1.]])" + ] + }, + "metadata": {}, + "execution_count": 24 + } + ] + }, + { + "cell_type": "code", + "source": [ + "ones.dtype" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "TvGOA9odfIEO", + "outputId": "45949ef4-6649-4b6c-d6af-2d4bfb8de832" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "torch.float32" + ] + }, + "metadata": {}, + "execution_count": 25 + } + ] + }, + { + "cell_type": "code", + "source": [ + "ones*zero" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "--pTyge-fI-8", + "outputId": "c4d9bb7e-829b-43db-e2db-b1a2d64e61f0" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[0., 0., 0., 0.],\n", + " [0., 0., 0., 0.],\n", + " [0., 0., 0., 0.]])" + ] + }, + "metadata": {}, + "execution_count": 26 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Range of Tensors, Tensor - like" + ], + "metadata": { + "id": "qDcc7Z36fSJF" + } + }, + { + "cell_type": "code", + "source": [ + "one_to_ten = torch.arange(start = 1, end = 11, step = 1)\n", + "one_to_ten" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "w3CZB4zUfR1s", + "outputId": "197fcba1-da0a-4b4a-ed11-3974bd6c01aa" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" + ] + }, + "metadata": {}, + "execution_count": 27 + } + ] + }, + { + "cell_type": "code", + "source": [ + "ten_zeros = torch.zeros_like(one_to_ten)\n", + "ten_zeros" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "WZh99BwVfRy8", + "outputId": "51ef8bfb-6fa0-4099-ff66-b97d65b2ddea" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])" + ] + }, + "metadata": {}, + "execution_count": 28 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Tensor Datatypes" + ], + "metadata": { + "id": "pGGhgsbUgqbW" + } + }, + { + "cell_type": "code", + "source": [ + "float_32_tensor = torch.tensor([3.0, 6.0,9.0], dtype = None, device = None, requires_grad = False)\n", + "float_32_tensor" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "JORJl4XkfRsx", + "outputId": "71114171-0f49-481f-b6fc-6cb48e2fb895" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([3., 6., 9.])" + ] + }, + "metadata": {}, + "execution_count": 29 + } + ] + }, + { + "cell_type": "code", + "source": [ + "float_32_tensor.dtype" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "6wOPPwGyfRLn", + "outputId": "f23776a1-b682-404a-9f67-d5bcb0402666" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "torch.float32" + ] + }, + "metadata": {}, + "execution_count": 30 + } + ] + }, + { + "cell_type": "code", + "source": [ + "float_16_tensor = float_32_tensor.type(torch.float16)\n", + "float_16_tensor.dtype" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "tFsHCvmZfOYe", + "outputId": "d3aa305a-7591-47f5-97fd-61bff60b44bd" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "torch.float16" + ] + }, + "metadata": {}, + "execution_count": 31 + } + ] + }, + { + "cell_type": "code", + "source": [ + "float_16_tensor*float_32_tensor" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "TQiCGTPuwq0q", + "outputId": "98750fce-1ca3-4889-e269-8b753efdea96" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([ 9., 36., 81.])" + ] + }, + "metadata": {}, + "execution_count": 32 + } + ] + }, + { + "cell_type": "code", + "source": [ + "int_32_tensor = torch.tensor([3, 6, 9], dtype = torch.int32)\n", + "int_32_tensor" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "5hlrLvGUw5D_", + "outputId": "41d890a0-9aee-446c-d906-631ce2ab0995" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([3, 6, 9], dtype=torch.int32)" + ] + }, + "metadata": {}, + "execution_count": 33 + } + ] + }, + { + "cell_type": "code", + "source": [ + "int_32_tensor*float_32_tensor" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ihApD9u3xTNW", + "outputId": "d295eed0-6996-4e0f-8502-ff4b55cd1373" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([ 9., 36., 81.])" + ] + }, + "metadata": {}, + "execution_count": 34 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x = torch.arange(0,100,10)" + ], + "metadata": { + "id": "utKhlb_KxWDQ" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "x" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "p78D74E9Rj7Y", + "outputId": "781a1614-a900-41f5-9e5d-358f0b2390aa" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])" + ] + }, + "metadata": {}, + "execution_count": 36 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x.min()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "4BcSs5NeRkcj", + "outputId": "3f24a8dc-58e9-4a5f-9834-e85856a34f9d" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(0)" + ] + }, + "metadata": {}, + "execution_count": 37 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x.max()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "hinqvXVLRm4q", + "outputId": "5c7d8a53-3913-4ac1-bba3-5ba8ff68250a" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(90)" + ] + }, + "metadata": {}, + "execution_count": 38 + } + ] + }, + { + "cell_type": "code", + "source": [ + "torch.mean(x.type(torch.float32))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "k7okc0_vRpnB", + "outputId": "91e5494f-dc57-417c-ea4d-25dbc547c893" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(45.)" + ] + }, + "metadata": {}, + "execution_count": 39 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x.type(torch.float32).mean()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "29QcDTjHRq10", + "outputId": "62937c6c-78e0-49f2-dde3-1543ee8f7907" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(45.)" + ] + }, + "metadata": {}, + "execution_count": 40 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x.sum()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "wlpY_G_sbdKF", + "outputId": "475d8258-af65-4011-a258-b93d4d8142d4" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(450)" + ] + }, + "metadata": {}, + "execution_count": 41 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x.argmax()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "GT6HJzwhbk4n", + "outputId": "2e455c20-c322-4bcf-d07c-1259d3ccefc6" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(9)" + ] + }, + "metadata": {}, + "execution_count": 42 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x.argmin()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "egL3oi2Mb19P", + "outputId": "f71fb32f-6338-44a3-b377-75bea0a3ab54" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(0)" + ] + }, + "metadata": {}, + "execution_count": 43 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x[0]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "p2U8DZKib3DP", + "outputId": "b9f613b9-74e9-45f4-ed01-05babb6a6793" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(0)" + ] + }, + "metadata": {}, + "execution_count": 44 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x[9]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "24qBFlGYcABe", + "outputId": "5813cfcb-7f63-4bd7-ee46-f95ccbfda939" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(90)" + ] + }, + "metadata": {}, + "execution_count": 45 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x = torch.arange(1, 10)\n", + "x.shape" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "0GPOxEzkcBHO", + "outputId": "aefbd903-4f4c-4d2c-c90f-eccd682fe018" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "torch.Size([9])" + ] + }, + "metadata": {}, + "execution_count": 46 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x_reshaped = x.reshape(1,9)\n", + "x_reshaped, x_reshaped.shape" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "spmRgQjwddgp", + "outputId": "85a7c55c-2909-4ea2-fc68-386dddc65742" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(tensor([[1, 2, 3, 4, 5, 6, 7, 8, 9]]), torch.Size([1, 9]))" + ] + }, + "metadata": {}, + "execution_count": 47 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x_reshaped.view(1,9)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "tH2ahWGydqqP", + "outputId": "65d92263-4fc4-434a-c06d-c5e08436f7fe" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[1, 2, 3, 4, 5, 6, 7, 8, 9]])" + ] + }, + "metadata": {}, + "execution_count": 48 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x_stacked = torch.stack([x, x, x, x], dim = 1)\n", + "x_stacked" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "jgCeJcaud_-1", + "outputId": "7f293a37-6ef1-43b6-aee5-9d6d91c94f9e" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[1, 1, 1, 1],\n", + " [2, 2, 2, 2],\n", + " [3, 3, 3, 3],\n", + " [4, 4, 4, 4],\n", + " [5, 5, 5, 5],\n", + " [6, 6, 6, 6],\n", + " [7, 7, 7, 7],\n", + " [8, 8, 8, 8],\n", + " [9, 9, 9, 9]])" + ] + }, + "metadata": {}, + "execution_count": 49 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x_stacked.squeeze()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "XhJHIK6cfPse", + "outputId": "06c47b89-3a9e-453e-bcc3-00cbcb0b8b49" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[1, 1, 1, 1],\n", + " [2, 2, 2, 2],\n", + " [3, 3, 3, 3],\n", + " [4, 4, 4, 4],\n", + " [5, 5, 5, 5],\n", + " [6, 6, 6, 6],\n", + " [7, 7, 7, 7],\n", + " [8, 8, 8, 8],\n", + " [9, 9, 9, 9]])" + ] + }, + "metadata": {}, + "execution_count": 50 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x_stacked.unsqueeze(dim=1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ej2c3Xxzf0tq", + "outputId": "94024061-eb37-446d-c4a8-e4d16cb6de81" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[[1, 1, 1, 1]],\n", + "\n", + " [[2, 2, 2, 2]],\n", + "\n", + " [[3, 3, 3, 3]],\n", + "\n", + " [[4, 4, 4, 4]],\n", + "\n", + " [[5, 5, 5, 5]],\n", + "\n", + " [[6, 6, 6, 6]],\n", + "\n", + " [[7, 7, 7, 7]],\n", + "\n", + " [[8, 8, 8, 8]],\n", + "\n", + " [[9, 9, 9, 9]]])" + ] + }, + "metadata": {}, + "execution_count": 52 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x_stacked.squeeze()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "4DJYo1a0f5M0", + "outputId": "efca2b47-1b14-44de-9a9a-2c83629d153f" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[1, 1, 1, 1],\n", + " [2, 2, 2, 2],\n", + " [3, 3, 3, 3],\n", + " [4, 4, 4, 4],\n", + " [5, 5, 5, 5],\n", + " [6, 6, 6, 6],\n", + " [7, 7, 7, 7],\n", + " [8, 8, 8, 8],\n", + " [9, 9, 9, 9]])" + ] + }, + "metadata": {}, + "execution_count": 53 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x_stacked.unsqueeze(dim=-2)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "J4iEjn2ah2HL", + "outputId": "22395593-7c16-4162-beae-dd2bbe7bda35" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[[1, 1, 1, 1]],\n", + "\n", + " [[2, 2, 2, 2]],\n", + "\n", + " [[3, 3, 3, 3]],\n", + "\n", + " [[4, 4, 4, 4]],\n", + "\n", + " [[5, 5, 5, 5]],\n", + "\n", + " [[6, 6, 6, 6]],\n", + "\n", + " [[7, 7, 7, 7]],\n", + "\n", + " [[8, 8, 8, 8]],\n", + "\n", + " [[9, 9, 9, 9]]])" + ] + }, + "metadata": {}, + "execution_count": 55 + } + ] + }, + { + "cell_type": "code", + "source": [ + "import torch\n", + "tensor = torch.tensor([1, 2, 3])\n", + "tensor = tensor - 10\n", + "tensor" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "cFfiD7Nth7Z_", + "outputId": "1139e1f8-fc1a-46ca-d636-f2bc4fd2eef6" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([-9, -8, -7])" + ] + }, + "metadata": {}, + "execution_count": 7 + } + ] + }, + { + "cell_type": "code", + "source": [ + "torch.mul(tensor, 10)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "dyA7BM_GHhqE", + "outputId": "0e3b9671-d9e8-4a32-87bb-59bc05986142" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([-90, -80, -70])" + ] + }, + "metadata": {}, + "execution_count": 9 + } + ] + }, + { + "cell_type": "code", + "source": [ + "torch.sub(tensor, 100)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "owtUsZ1KNegI", + "outputId": "189b7b23-0041-4e09-b991-cd209a48506a" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([-109, -108, -107])" + ] + }, + "metadata": {}, + "execution_count": 10 + } + ] + }, + { + "cell_type": "code", + "source": [ + "torch.add(tensor, 100)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "K5STXlQONsyc", + "outputId": "00cbb79a-0a1d-4e21-86ec-5c91c37a2d01" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([91, 92, 93])" + ] + }, + "metadata": {}, + "execution_count": 11 + } + ] + }, + { + "cell_type": "code", + "source": [ + "torch.divide(tensor, 2)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "xqMGnzIUNvp0", + "outputId": "c894cf3e-f148-45f8-cfc8-d78740735306" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([-4.5000, -4.0000, -3.5000])" + ] + }, + "metadata": {}, + "execution_count": 13 + } + ] + }, + { + "cell_type": "code", + "source": [ + "torch.matmul(tensor, tensor)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ruGzKpV8NyBc", + "outputId": "fddb63bf-006f-48b6-ae28-287fbcda8bc5" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(194)" + ] + }, + "metadata": {}, + "execution_count": 15 + } + ] + }, + { + "cell_type": "code", + "source": [ + "tensor@tensor" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "8GS3r9yTeGfD", + "outputId": "c80b12ac-30b5-4f3d-c38c-9e41ba511b0e" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(194)" + ] + }, + "metadata": {}, + "execution_count": 16 + } + ] + }, + { + "cell_type": "code", + "source": [ + "%%time\n", + "tensor@tensor" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "QmuYHqXTemC0", + "outputId": "402fe3ba-70b5-4bb2-c83b-254db84ff810" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "CPU times: user 622 µs, sys: 0 ns, total: 622 µs\n", + "Wall time: 516 µs\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(194)" + ] + }, + "metadata": {}, + "execution_count": 17 + } + ] + }, + { + "cell_type": "code", + "source": [ + "%%time\n", + "torch.matmul(tensor,tensor)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "dGr1fzdNepd8", + "outputId": "97bd6c91-bc25-4b38-cdf5-f22dcdef243e" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "CPU times: user 424 µs, sys: 998 µs, total: 1.42 ms\n", + "Wall time: 1.43 ms\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(194)" + ] + }, + "metadata": {}, + "execution_count": 18 + } + ] + }, + { + "cell_type": "code", + "source": [ + "torch.rand(3,2)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "pGYDoK2gevfo", + "outputId": "2c8783d5-0453-47c5-c7ed-af10d25d6989" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[0.5999, 0.0073],\n", + " [0.9321, 0.3026],\n", + " [0.3463, 0.3872]])" + ] + }, + "metadata": {}, + "execution_count": 20 + } + ] + }, + { + "cell_type": "code", + "source": [ + "torch.matmul(torch.rand(3,2), torch.rand(2,3))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "KGBGQoB8e2DP", + "outputId": "4c2ef361-a2d0-41ee-c328-3992cbbc138d" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[0.3528, 0.1893, 0.0714],\n", + " [1.2791, 0.7110, 0.2563],\n", + " [0.8812, 0.4553, 0.1803]])" + ] + }, + "metadata": {}, + "execution_count": 23 + } + ] + }, + { + "cell_type": "code", + "source": [ + "import torch" + ], + "metadata": { + "id": "ib8DMtkBe_LJ" + }, + "execution_count": 1, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "x = torch.rand(2,9)" + ], + "metadata": { + "id": "nJo8ZBdrQY1b" + }, + "execution_count": 2, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "x" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "wi6oRv4MQfgf", + "outputId": "55c99f55-31f6-4cf5-ba4e-19a47c3a0167" + }, + "execution_count": 3, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[0.5894, 0.4391, 0.2018, 0.5417, 0.3844, 0.3592, 0.9209, 0.9269, 0.0681],\n", + " [0.0746, 0.1740, 0.6821, 0.6890, 0.0999, 0.7444, 0.2391, 0.4625, 0.8302]])" + ] + }, + "metadata": {}, + "execution_count": 3 + } + ] + }, + { + "cell_type": "code", + "source": [ + "y=torch.randn(2,3,5)\n", + "y" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Zpx8myAUQgoc", + "outputId": "07756d70-56bd-437c-c74e-9aecc1a77311" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[[ 1.5552, -0.4877, 0.5175, -1.7958, -0.6187],\n", + " [-0.3359, -1.9710, 0.0112, -1.7578, -1.5295],\n", + " [ 0.0932, 1.4079, 0.9108, 0.3328, -0.6978]],\n", + "\n", + " [[-0.9406, -1.0809, -0.2595, 0.1282, 1.6605],\n", + " [ 1.1624, 1.0902, 1.7092, -0.2842, -1.3780],\n", + " [-0.1534, -1.2795, -0.5495, 0.9902, 0.1822]]])" + ] + }, + "metadata": {}, + "execution_count": 5 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x_original = torch.rand(size=(224,224,3))\n", + "x_original" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "s4U-X9bJQnWe", + "outputId": "657a7a76-962c-4b41-a76b-902d0482266c" + }, + "execution_count": 6, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[[0.4549, 0.6809, 0.2118],\n", + " [0.4824, 0.9008, 0.8741],\n", + " [0.1715, 0.1757, 0.1845],\n", + " ...,\n", + " [0.8741, 0.6594, 0.2610],\n", + " [0.0092, 0.1984, 0.1955],\n", + " [0.4236, 0.4182, 0.0251]],\n", + "\n", + " [[0.9174, 0.1661, 0.5852],\n", + " [0.1837, 0.2351, 0.3810],\n", + " [0.3726, 0.4808, 0.8732],\n", + " ...,\n", + " [0.6794, 0.0554, 0.9202],\n", + " [0.0864, 0.8750, 0.3558],\n", + " [0.8445, 0.9759, 0.4934]],\n", + "\n", + " [[0.1600, 0.2635, 0.7194],\n", + " [0.9488, 0.3405, 0.3647],\n", + " [0.6683, 0.5168, 0.9592],\n", + " ...,\n", + " [0.0521, 0.0140, 0.2445],\n", + " [0.3596, 0.3999, 0.2730],\n", + " [0.5926, 0.9877, 0.7784]],\n", + "\n", + " ...,\n", + "\n", + " [[0.4794, 0.5635, 0.3764],\n", + " [0.9124, 0.6094, 0.5059],\n", + " [0.4528, 0.4447, 0.5021],\n", + " ...,\n", + " [0.0089, 0.4816, 0.8727],\n", + " [0.2173, 0.6296, 0.2347],\n", + " [0.2028, 0.9931, 0.7201]],\n", + "\n", + " [[0.3116, 0.6459, 0.4703],\n", + " [0.0148, 0.2345, 0.7149],\n", + " [0.8393, 0.5804, 0.6691],\n", + " ...,\n", + " [0.2105, 0.9460, 0.2696],\n", + " [0.5918, 0.9295, 0.2616],\n", + " [0.2537, 0.7819, 0.4700]],\n", + "\n", + " [[0.6654, 0.1200, 0.5841],\n", + " [0.9147, 0.5522, 0.6529],\n", + " [0.1799, 0.5276, 0.5415],\n", + " ...,\n", + " [0.7536, 0.4346, 0.8793],\n", + " [0.3793, 0.1750, 0.7792],\n", + " [0.9266, 0.8325, 0.9974]]])" + ] + }, + "metadata": {}, + "execution_count": 6 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x_permuted=x_original.permute(2, 0, 1)\n", + "print(x_original.shape)\n", + "print(x_permuted.shape)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "DD19_zvbQzHo", + "outputId": "1d64ce1b-eb48-47e3-90b6-7f1340e7f2b2" + }, + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "torch.Size([224, 224, 3])\n", + "torch.Size([3, 224, 224])\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "x_original[0,0,0]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "NnPmMk4ZRF7w", + "outputId": "2cd5da7f-4a23-4a76-8c4a-bb982113f2a4" + }, + "execution_count": 10, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(0.4549)" + ] + }, + "metadata": {}, + "execution_count": 10 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x_permuted[0,0,0]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Z0ylNoAARgTo", + "outputId": "ddca0298-cddf-4048-9b71-a791655e5bed" + }, + "execution_count": 11, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(0.4549)" + ] + }, + "metadata": {}, + "execution_count": 11 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x_original[0,0,0]=0.989" + ], + "metadata": { + "id": "RXw0xXsDRi4L" + }, + "execution_count": 13, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "x_original[0,0,0]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "1sFdV6wzRo3f", + "outputId": "1cf87d2c-6d88-453a-d136-0f625a2800f1" + }, + "execution_count": 14, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(0.9890)" + ] + }, + "metadata": {}, + "execution_count": 14 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x_permuted[0,0,0]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "xTX-hx2SR1wp", + "outputId": "0d4908c4-c3bc-44e3-8ec6-1487104cc209" + }, + "execution_count": 15, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(0.9890)" + ] + }, + "metadata": {}, + "execution_count": 15 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x=torch.arange(1,10).reshape(1,3,3)\n", + "x, x.shape" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "mZomOe7gR4Q8", + "outputId": "0b3c922f-ec11-46de-b8a5-9f9533d866ad" + }, + "execution_count": 18, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(tensor([[[1, 2, 3],\n", + " [4, 5, 6],\n", + " [7, 8, 9]]]),\n", + " torch.Size([1, 3, 3]))" + ] + }, + "metadata": {}, + "execution_count": 18 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x[0]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "3y7v4SQvSBs1", + "outputId": "8c53307d-e628-404d-db66-56c6bdffab7c" + }, + "execution_count": 19, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([[1, 2, 3],\n", + " [4, 5, 6],\n", + " [7, 8, 9]])" + ] + }, + "metadata": {}, + "execution_count": 19 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x[0][0]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "hf9uG4xLSNya", + "outputId": "3075bc42-9ffa-426b-8a86-95628ffcd824" + }, + "execution_count": 21, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([1, 2, 3])" + ] + }, + "metadata": {}, + "execution_count": 21 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x[0][0][0]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "zA4G2Se4SRB3", + "outputId": "324312d2-ed0a-49eb-f81f-e904e53992fe" + }, + "execution_count": 22, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(1)" + ] + }, + "metadata": {}, + "execution_count": 22 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x[0][2][2]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Mwy3zmKKSdbk", + "outputId": "d35172c3-b099-40a6-ddf1-a453c2adfa44" + }, + "execution_count": 23, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor(9)" + ] + }, + "metadata": {}, + "execution_count": 23 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x[:,1,1]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "fE3nCM1KS7XT", + "outputId": "01f5d755-9737-4235-9f73-dce89ff6ba16" + }, + "execution_count": 24, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([5])" + ] + }, + "metadata": {}, + "execution_count": 24 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x[0,0,:]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "luNDINKNTTxp", + "outputId": "091195ef-2f71-4602-e95f-529a69193150" + }, + "execution_count": 25, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([1, 2, 3])" + ] + }, + "metadata": {}, + "execution_count": 25 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x[0,:,2]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "KG8A4xbfThCL", + "outputId": "5866bc41-9241-4619-be7b-e9206b3f80ab" + }, + "execution_count": 26, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([3, 6, 9])" + ] + }, + "metadata": {}, + "execution_count": 26 + } + ] + }, + { + "cell_type": "code", + "source": [ + "import numpy as np" + ], + "metadata": { + "id": "CZ3PX0qlTwHJ" + }, + "execution_count": 27, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array = np.arange(1.0, 8.0)" + ], + "metadata": { + "id": "UOBeTumiT3Lf" + }, + "execution_count": 28, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "RzcO32E9UCQl", + "outputId": "430def24-c42c-461f-e5e7-398544c695d3" + }, + "execution_count": 29, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1., 2., 3., 4., 5., 6., 7.])" + ] + }, + "metadata": {}, + "execution_count": 29 + } + ] + }, + { + "cell_type": "code", + "source": [ + "tensor = torch.from_numpy(array)\n", + "tensor" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "JJIL0q1DUC6O", + "outputId": "8a3b1d7c-4482-4d32-f34f-9212d9d3a177" + }, + "execution_count": 32, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([1., 2., 3., 4., 5., 6., 7.], dtype=torch.float64)" + ] + }, + "metadata": {}, + "execution_count": 32 + } + ] + }, + { + "cell_type": "code", + "source": [ + "array[3]=11.0" + ], + "metadata": { + "id": "j3Ce6q3DUIEK" + }, + "execution_count": 33, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "dc_BCVdjUsCc", + "outputId": "65537325-8b11-4f36-fc73-e56f30d6a036" + }, + "execution_count": 34, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 1., 2., 3., 11., 5., 6., 7.])" + ] + }, + "metadata": {}, + "execution_count": 34 + } + ] + }, + { + "cell_type": "code", + "source": [ + "tensor" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "VG1e_eITUta2", + "outputId": "a26c5198-23b6-4a6d-d73a-ba20cd9782b8" + }, + "execution_count": 35, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([ 1., 2., 3., 11., 5., 6., 7.], dtype=torch.float64)" + ] + }, + "metadata": {}, + "execution_count": 35 + } + ] + }, + { + "cell_type": "code", + "source": [ + "tensor = torch.ones(7)\n", + "tensor, tensor.dtype\n", + "numpy_tensor = tensor.numpy()\n", + "numpy_tensor, numpy_tensor.dtype" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Swt8JF8vUuev", + "outputId": "c9e5bf6a-6d2c-41d6-8327-366867ffdd2d" + }, + "execution_count": 37, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(array([1., 1., 1., 1., 1., 1., 1.], dtype=float32), dtype('float32'))" + ] + }, + "metadata": {}, + "execution_count": 37 + } + ] + }, + { + "cell_type": "code", + "source": [ + "import torch\n", + "random_tensor_A = torch.rand(3,4)\n", + "random_tensor_B = torch.rand(3,4)\n", + "print(random_tensor_A)\n", + "print(random_tensor_B)\n", + "print(random_tensor_A == random_tensor_B)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "uGcagTteVFTD", + "outputId": "49405790-08e7-4210-b7f1-f00b904c7eb9" + }, + "execution_count": 38, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "tensor([[0.9870, 0.6636, 0.6873, 0.8863],\n", + " [0.8386, 0.4169, 0.3587, 0.0265],\n", + " [0.2981, 0.6025, 0.5652, 0.5840]])\n", + "tensor([[0.9821, 0.3481, 0.0913, 0.4940],\n", + " [0.7495, 0.4387, 0.9582, 0.8659],\n", + " [0.5064, 0.6919, 0.0809, 0.9771]])\n", + "tensor([[False, False, False, False],\n", + " [False, False, False, False],\n", + " [False, False, False, False]])\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "RANDOM_SEED = 42\n", + "torch.manual_seed(RANDOM_SEED)\n", + "random_tensor_C = torch.rand(3,4)\n", + "torch.manual_seed(RANDOM_SEED)\n", + "random_tensor_D = torch.rand(3,4)\n", + "print(random_tensor_C)\n", + "print(random_tensor_D)\n", + "print(random_tensor_C == random_tensor_D)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "HznyXyEaWjLM", + "outputId": "25956434-01b6-4059-9054-c9978884ddc1" + }, + "execution_count": 46, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "tensor([[0.8823, 0.9150, 0.3829, 0.9593],\n", + " [0.3904, 0.6009, 0.2566, 0.7936],\n", + " [0.9408, 0.1332, 0.9346, 0.5936]])\n", + "tensor([[0.8823, 0.9150, 0.3829, 0.9593],\n", + " [0.3904, 0.6009, 0.2566, 0.7936],\n", + " [0.9408, 0.1332, 0.9346, 0.5936]])\n", + "tensor([[True, True, True, True],\n", + " [True, True, True, True],\n", + " [True, True, True, True]])\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "!nvidia-smi" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "vltPTh0YXJSt", + "outputId": "807af6dc-a9ca-4301-ec32-b688dbde8be8" + }, + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Thu May 23 02:57:59 2024 \n", + "+---------------------------------------------------------------------------------------+\n", + "| NVIDIA-SMI 535.104.05 Driver Version: 535.104.05 CUDA Version: 12.2 |\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 T4 Off | 00000000:00:04.0 Off | 0 |\n", + "| N/A 60C P8 11W / 70W | 0MiB / 15360MiB | 0% Default |\n", + "| | | N/A |\n", + "+-----------------------------------------+----------------------+----------------------+\n", + " \n", + "+---------------------------------------------------------------------------------------+\n", + "| Processes: |\n", + "| GPU GI CI PID Type Process name GPU Memory |\n", + "| ID ID Usage |\n", + "|=======================================================================================|\n", + "| No running processes found |\n", + "+---------------------------------------------------------------------------------------+\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "import torch\n", + "torch.cuda.is_available()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "L6mMyPDyYh1j", + "outputId": "279c5dd8-c2a8-4fbd-f321-2f5d7c6e90e6" + }, + "execution_count": 3, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": {}, + "execution_count": 3 + } + ] + }, + { + "cell_type": "code", + "source": [ + "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", + "device" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "oOdiYa7ZYytx", + "outputId": "d73b04fc-8963-4826-9722-08d118d5ab91" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'cuda'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 5 + } + ] + }, + { + "cell_type": "code", + "source": [ + "torch.cuda.device_count()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "vOdsazLqZFM5", + "outputId": "8189cd6a-9017-4663-a652-3e15c517d9c3" + }, + "execution_count": 6, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "1" + ] + }, + "metadata": {}, + "execution_count": 6 + } + ] + }, + { + "cell_type": "code", + "source": [ + "tensor = torch.tensor([1,2,3], device = \"cpu\")\n", + "print(tensor, tensor.device)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "cdik9Vw3ZMv0", + "outputId": "044a68fd-83a1-409d-8e3b-655142ca0270" + }, + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "tensor([1, 2, 3]) cpu\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "tensor_on_gpu = tensor.to(device)\n", + "tensor_on_gpu" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Zmp835rrZp-z", + "outputId": "37fa3413-18a3-47bf-ae51-5b36ff85a3ef" + }, + "execution_count": 8, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tensor([1, 2, 3], device='cuda:0')" + ] + }, + "metadata": {}, + "execution_count": 8 + } + ] + }, + { + "cell_type": "code", + "source": [ + "tensor_on_gpu.numpy()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 159 + }, + "id": "jhriaa8uZ1yM", + "outputId": "bc5a3226-1a12-4fea-8769-a44f21cdc323" + }, + "execution_count": 10, + "outputs": [ + { + "output_type": "error", + "ename": "TypeError", + "evalue": "can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.", + "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[0;32m----> 1\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." + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "tensor_on_cpu = tensor_on_gpu.cpu().numpy()" + ], + "metadata": { + "id": "LHGXK3GgaOzL" + }, + "execution_count": 12, + "outputs": [] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "j-El4LlCajfq" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file