{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "44f632cd", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import torch\n", "import os" ] }, { "cell_type": "code", "execution_count": 3, "id": "4a67324e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(torch.Size([3]), torch.Size([3]))" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = torch.arange(start=10, end=20, step=4)\n", "y = torch.arange(start=5, end=15, step=4)\n", "\n", "x.shape, y.shape" ] }, { "cell_type": "code", "execution_count": 5, "id": "cf2beedd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([10, 14, 18])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 6, "id": "0cd13130", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([ 5, 9, 13])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 4, "id": "6d7100d2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([15, 23, 31])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x + y" ] }, { "cell_type": "code", "execution_count": 7, "id": "8ab34570", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([5, 5, 5])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x - y" ] }, { "cell_type": "code", "execution_count": 8, "id": "e993b431", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([ 50, 126, 234])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x * y #<< Element wise multiplication" ] }, { "cell_type": "code", "execution_count": 24, "id": "010868e6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(410)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.dot(x, y)" ] }, { "cell_type": "code", "execution_count": 26, "id": "8c88ce34", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "410" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum([ 50, 126, 234])" ] }, { "cell_type": "code", "execution_count": 9, "id": "39bc530f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([2.0000, 1.5556, 1.3846])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x / y #<< Element wise division" ] }, { "cell_type": "code", "execution_count": 12, "id": "fc552085", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([1., 1., 1.])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = torch.ones(3)\n", "z" ] }, { "cell_type": "code", "execution_count": 13, "id": "d8a2b4b4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([11., 15., 19.])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = z + x\n", "z" ] }, { "cell_type": "code", "execution_count": 15, "id": "0f404afb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([31., 43., 55.])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## inplace addition\n", "z.add_(x)\n", "z" ] }, { "cell_type": "code", "execution_count": 16, "id": "f5694dc6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([100, 196, 324])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.pow(2)" ] }, { "cell_type": "code", "execution_count": 17, "id": "a21bed10", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([100, 196, 324])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x**2" ] }, { "cell_type": "code", "execution_count": 18, "id": "614b26ad", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([True, True, True])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x > 0" ] }, { "cell_type": "code", "execution_count": 19, "id": "b1b873f9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[1.0718, 1.8589, 2.2422, 0.8966],\n", " [0.6847, 1.3138, 1.1647, 0.5111],\n", " [1.0766, 1.7835, 1.9630, 0.9556]])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Matrix multpltplication\n", "\n", "A = torch.rand(size=(3,5))\n", "B = torch.rand(size=(5,4))\n", "\n", "A @ B " ] }, { "cell_type": "code", "execution_count": 20, "id": "395290ae", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[1.0718, 1.8589, 2.2422, 0.8966],\n", " [0.6847, 1.3138, 1.1647, 0.5111],\n", " [1.0766, 1.7835, 1.9630, 0.9556]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.matmul(B)" ] }, { "cell_type": "code", "execution_count": 22, "id": "226c626e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[2.7414, 2.0749, 1.5089],\n", " [2.2362, 3.0089, 1.1585],\n", " [2.8463, 2.8138, 2.9833]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = torch.rand((3,3))\n", "X.matrix_exp()" ] }, { "cell_type": "code", "execution_count": 23, "id": "b911c10a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[1.3818, 1.2499, 0.7981],\n", " [1.1508, 1.3000, 0.8949],\n", " [1.7752, 1.8222, 1.2387]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X.matrix_power(2)" ] }, { "cell_type": "code", "execution_count": 27, "id": "3b872579", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[0.5417, 0.4911, 0.7433, 0.4764, 0.7647],\n", " [0.3658, 0.6612, 0.0644, 0.5575, 0.2324],\n", " [0.0760, 0.9765, 0.6866, 0.5266, 0.8332]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Broadcasting\n", "\n", "A" ] }, { "cell_type": "code", "execution_count": 28, "id": "060261e3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[1.5417, 1.4911, 1.7433, 1.4764, 1.7647],\n", " [1.3658, 1.6612, 1.0644, 1.5575, 1.2324],\n", " [1.0760, 1.9765, 1.6866, 1.5266, 1.8332]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A + 1" ] }, { "cell_type": "code", "execution_count": 29, "id": "bed82cef", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[-0.4583, -0.5089, -0.2567, -0.5236, -0.2353],\n", " [-0.6342, -0.3388, -0.9356, -0.4425, -0.7676],\n", " [-0.9240, -0.0235, -0.3134, -0.4734, -0.1668]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A - 1" ] }, { "cell_type": "code", "execution_count": 30, "id": "d098a51c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[2.7087, 2.4553, 3.7167, 2.3822, 3.8236],\n", " [1.8292, 3.3058, 0.3218, 2.7874, 1.1621],\n", " [0.3798, 4.8824, 3.4330, 2.6328, 4.1660]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A * 5" ] }, { "cell_type": "code", "execution_count": 32, "id": "f30e607c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.Size([3, 5])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.shape" ] }, { "cell_type": "code", "execution_count": 35, "id": "e4121b64", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[1., 1., 1., 1., 1.]])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C = torch.ones((1,5))\n", "C" ] }, { "cell_type": "code", "execution_count": 34, "id": "0f4da8ca", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[-0.4583, -0.5089, -0.2567, -0.5236, -0.2353],\n", " [-0.6342, -0.3388, -0.9356, -0.4425, -0.7676],\n", " [-0.9240, -0.0235, -0.3134, -0.4734, -0.1668]])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A - C" ] }, { "cell_type": "code", "execution_count": 36, "id": "685f2da4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[1.5417, 1.4911, 1.7433, 1.4764, 1.7647],\n", " [1.3658, 1.6612, 1.0644, 1.5575, 1.2324],\n", " [1.0760, 1.9765, 1.6866, 1.5266, 1.8332]])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A + C" ] }, { "cell_type": "code", "execution_count": 38, "id": "5bc62f10", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[1.],\n", " [1.],\n", " [1.]])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D = torch.ones((3, 1))\n", "D" ] }, { "cell_type": "code", "execution_count": 39, "id": "78c15a70", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[-0.4583, -0.5089, -0.2567, -0.5236, -0.2353],\n", " [-0.6342, -0.3388, -0.9356, -0.4425, -0.7676],\n", " [-0.9240, -0.0235, -0.3134, -0.4734, -0.1668]])" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A - D" ] }, { "cell_type": "code", "execution_count": 40, "id": "11b079db", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[1.5417, 1.4911, 1.7433, 1.4764, 1.7647],\n", " [1.3658, 1.6612, 1.0644, 1.5575, 1.2324],\n", " [1.0760, 1.9765, 1.6866, 1.5266, 1.8332]])" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A + D" ] }, { "cell_type": "code", "execution_count": 42, "id": "ac6565e2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[1.],\n", " [1.],\n", " [1.]])" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D" ] }, { "cell_type": "code", "execution_count": 43, "id": "be2ab157", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(3.)" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.sum(D)" ] }, { "cell_type": "code", "execution_count": 44, "id": "0b34f120", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(7.9974)" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.sum(A)" ] }, { "cell_type": "code", "execution_count": 45, "id": "233f08d5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[0.5417, 0.4911, 0.7433, 0.4764, 0.7647],\n", " [0.3658, 0.6612, 0.0644, 0.5575, 0.2324],\n", " [0.0760, 0.9765, 0.6866, 0.5266, 0.8332]])" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 46, "id": "db7bd32f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(0.9765)" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.max(A)" ] }, { "cell_type": "code", "execution_count": 47, "id": "9afef318", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(0.0644)" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.min(A)" ] }, { "cell_type": "code", "execution_count": 48, "id": "f74fdd5b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.return_types.min(\n", "values=tensor([0.4764, 0.0644, 0.0760]),\n", "indices=tensor([3, 2, 0]))" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.min(A, dim=1)" ] }, { "cell_type": "code", "execution_count": 49, "id": "6a484028", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.return_types.max(\n", "values=tensor([0.7647, 0.6612, 0.9765]),\n", "indices=tensor([4, 1, 1]))" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.max(A, dim=1) # row wise max " ] }, { "cell_type": "code", "execution_count": 50, "id": "23b427e9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.return_types.max(\n", "values=tensor([0.5417, 0.9765, 0.7433, 0.5575, 0.8332]),\n", "indices=tensor([0, 2, 0, 1, 2]))" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.max(A, dim=0) # col wise max" ] }, { "cell_type": "code", "execution_count": 52, "id": "558b97bc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[0.5417, 0.4911, 0.7433, 0.4764, 0.7647],\n", " [0.3658, 0.6612, 0.0644, 0.5575, 0.2324],\n", " [0.0760, 0.9765, 0.6866, 0.5266, 0.8332]])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.abs(-A)" ] }, { "cell_type": "code", "execution_count": 54, "id": "54928d75", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[0.5417, 0.4911, 0.7433, 0.4764, 0.7647],\n", " [0.3658, 0.6612, 0.0644, 0.5575, 0.2324],\n", " [0.0760, 0.9765, 0.6866, 0.5266, 0.8332]])" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 53, "id": "0a729502", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(11)" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.argmax(A)" ] }, { "cell_type": "code", "execution_count": 58, "id": "c21eda57", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([4, 1, 1])" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.argmax(A, dim=1) # row wise argmax" ] }, { "cell_type": "code", "execution_count": 59, "id": "88cf1199", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([0, 2, 0, 1, 2])" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.argmax(A, dim=0) # col wise argmax" ] }, { "cell_type": "code", "execution_count": 55, "id": "c1de96b2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(7)" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.argmin(A)" ] }, { "cell_type": "code", "execution_count": 56, "id": "04dca97e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(0.5332)" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.mean(A)" ] }, { "cell_type": "code", "execution_count": 60, "id": "f4f33fdf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([0.6035, 0.3763, 0.6198])" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.mean(A, dim=1) # row wise mean" ] }, { "cell_type": "code", "execution_count": 61, "id": "5a0149d7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([0.3278, 0.7096, 0.4981, 0.5202, 0.6101])" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.mean(A, dim=0) # col wise mean" ] }, { "cell_type": "code", "execution_count": 62, "id": "d80598bf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[0.5417, 0.4911, 0.7433, 0.4764, 0.7647],\n", " [0.3658, 0.6612, 0.0644, 0.5575, 0.2324],\n", " [0.0760, 0.9765, 0.6866, 0.5266, 0.8332]])" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 67, "id": "a4ee06d2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1.]])" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D = torch.ones(A.shape)\n", "D" ] }, { "cell_type": "code", "execution_count": 68, "id": "a93ef5ae", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[False, False, False, False, False],\n", " [False, False, False, False, False],\n", " [False, False, False, False, False]])" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.eq(A, D)" ] }, { "cell_type": "code", "execution_count": 69, "id": "2e3539f4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[True, True, True, True, True],\n", " [True, True, True, True, True],\n", " [True, True, True, True, True]])" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.eq(D, D)" ] }, { "cell_type": "code", "execution_count": 70, "id": "647208c7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.return_types.sort(\n", "values=tensor([[0.4764, 0.4911, 0.5417, 0.7433, 0.7647],\n", " [0.0644, 0.2324, 0.3658, 0.5575, 0.6612],\n", " [0.0760, 0.5266, 0.6866, 0.8332, 0.9765]]),\n", "indices=tensor([[3, 1, 0, 2, 4],\n", " [2, 4, 0, 3, 1],\n", " [0, 3, 2, 4, 1]]))" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.sort(A)" ] }, { "cell_type": "code", "execution_count": 75, "id": "c9746bd6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[0.5417, 0.5000, 0.7433, 0.5000, 0.7647],\n", " [0.5000, 0.6612, 0.5000, 0.5575, 0.5000],\n", " [0.5000, 0.9765, 0.6866, 0.5266, 0.8332]])" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.clamp(A, min=0.5)" ] }, { "cell_type": "code", "execution_count": null, "id": "714ae7e3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "0cec2923", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "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.7.11" } }, "nbformat": 4, "nbformat_minor": 5 }