{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "pytorch beginners.ipynb", "version": "0.3.2", "provenance": [], "collapsed_sections": [], "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "metadata": { "id": "vz7PYT6L8XkK", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "import torch\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "4-n4KgVb8pWa", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "a = torch.tensor([1,2,3,4,5,6])" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "BBcM1DPw_wU1", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "fec99134-87e8-405c-f44e-3548faf79282" }, "cell_type": "code", "source": [ "a.dtype" ], "execution_count": 8, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "torch.int64" ] }, "metadata": { "tags": [] }, "execution_count": 8 } ] }, { "metadata": { "id": "PDsaKcVgAIQI", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "42aa81f4-dc14-4abb-839e-fef05c83a96f" }, "cell_type": "code", "source": [ "b = torch.FloatTensor([1,2,3,4,5,6])\n", "b.dtype" ], "execution_count": 9, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "torch.float32" ] }, "metadata": { "tags": [] }, "execution_count": 9 } ] }, { "metadata": { "id": "1bjuAAIUASfB", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "3a9cecda-daf2-4cae-8b13-7977735b273e" }, "cell_type": "code", "source": [ "b.size()" ], "execution_count": 10, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "torch.Size([6])" ] }, "metadata": { "tags": [] }, "execution_count": 10 } ] }, { "metadata": { "id": "W9_oH323ATsI", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "ccc5ac79-84f2-41b3-87da-d31dc1a0e40b" }, "cell_type": "code", "source": [ "c = torch.Tensor([1,2,3])\n", "c.dtype" ], "execution_count": 11, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "torch.float32" ] }, "metadata": { "tags": [] }, "execution_count": 11 } ] }, { "metadata": { "id": "5mD0lzrsA1LD", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 68 }, "outputId": "9fefc0ea-aa3a-4519-e494-24a61302c7df" }, "cell_type": "code", "source": [ "b.view(3,2)" ], "execution_count": 12, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "tensor([[1., 2.],\n", " [3., 4.],\n", " [5., 6.]])" ] }, "metadata": { "tags": [] }, "execution_count": 12 } ] }, { "metadata": { "id": "VMBYIutPA2pr", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "06b3c6d1-6e10-463c-b618-6455d9c6bd58" }, "cell_type": "code", "source": [ "a = torch.arange(9)\n", "a" ], "execution_count": 13, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "tensor([0, 1, 2, 3, 4, 5, 6, 7, 8])" ] }, "metadata": { "tags": [] }, "execution_count": 13 } ] }, { "metadata": { "id": "E4O1rGn-A67T", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 68 }, "outputId": "7ffc8ea3-10a0-4cb1-d4e8-e14a9d7a08fc" }, "cell_type": "code", "source": [ "a.view(3,3)" ], "execution_count": 14, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "tensor([[0, 1, 2],\n", " [3, 4, 5],\n", " [6, 7, 8]])" ] }, "metadata": { "tags": [] }, "execution_count": 14 } ] }, { "metadata": { "id": "E7TjZ9MZA80j", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "a = torch.arange(27)\n", "a = a.view(3,3,3)" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "mkyuKZSIBdBH", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 204 }, "outputId": "68b86d2b-953c-419a-d374-c64f13a1f2e4" }, "cell_type": "code", "source": [ "a" ], "execution_count": 16, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "tensor([[[ 0, 1, 2],\n", " [ 3, 4, 5],\n", " [ 6, 7, 8]],\n", "\n", " [[ 9, 10, 11],\n", " [12, 13, 14],\n", " [15, 16, 17]],\n", "\n", " [[18, 19, 20],\n", " [21, 22, 23],\n", " [24, 25, 26]]])" ] }, "metadata": { "tags": [] }, "execution_count": 16 } ] }, { "metadata": { "id": "kSC7VL_HBdQt", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "434aa72a-5ce9-4fe7-84ef-1ff0bccab758" }, "cell_type": "code", "source": [ "a[1,1,1]" ], "execution_count": 17, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "tensor(13)" ] }, "metadata": { "tags": [] }, "execution_count": 17 } ] }, { "metadata": { "id": "UjhD37fJBqo2", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "a = torch.tensor([1,2,3])\n", "b = torch.tensor([1,2,3]).view(3,1)" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "uDI4Ib2yB4sc", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "e3a2a419-0565-4f96-b3b9-3b5e2decedd0" }, "cell_type": "code", "source": [ "a" ], "execution_count": 19, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "tensor([1, 2, 3])" ] }, "metadata": { "tags": [] }, "execution_count": 19 } ] }, { "metadata": { "id": "_7gN68F2B5E8", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 68 }, "outputId": "a36cad54-ec3d-47b7-93ef-bebb92b2eb0e" }, "cell_type": "code", "source": [ "b" ], "execution_count": 20, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "tensor([[1],\n", " [2],\n", " [3]])" ] }, "metadata": { "tags": [] }, "execution_count": 20 } ] }, { "metadata": { "id": "fov-e_lgB5U7", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 68 }, "outputId": "4b7b396b-979d-4487-9ab9-01474bc0a892" }, "cell_type": "code", "source": [ "a*b" ], "execution_count": 21, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "tensor([[1, 2, 3],\n", " [2, 4, 6],\n", " [3, 6, 9]])" ] }, "metadata": { "tags": [] }, "execution_count": 21 } ] }, { "metadata": { "id": "G_yflvTeB6fM", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "acb11928-d8b2-49ea-b10d-61a90e88d2ce" }, "cell_type": "code", "source": [ "a@b" ], "execution_count": 23, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "tensor([14])" ] }, "metadata": { "tags": [] }, "execution_count": 23 } ] }, { "metadata": { "id": "13muWnuIB94R", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "0fb74e66-0fb9-4336-aad6-f92ca2ae20ef" }, "cell_type": "code", "source": [ "a = torch.tensor([1,2,3])\n", "b = torch.tensor([1,2,3])\n", "torch.dot(a,b)" ], "execution_count": 24, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "tensor(14)" ] }, "metadata": { "tags": [] }, "execution_count": 24 } ] }, { "metadata": { "id": "XpCGIBEXCZdQ", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "6a3710db-0a08-4f38-bfa4-300402433dd8" }, "cell_type": "code", "source": [ "x = torch.tensor(2.0, requires_grad=True)\n", "x" ], "execution_count": 26, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "tensor(2., requires_grad=True)" ] }, "metadata": { "tags": [] }, "execution_count": 26 } ] }, { "metadata": { "id": "zB9dvIuBCp5m", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "y = 5*x**2 + 20*x " ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "mtanR87PCyCV", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "cdd1f513-4b28-4567-ce73-d9de9707f205" }, "cell_type": "code", "source": [ "y.backward()\n", "x.grad" ], "execution_count": 28, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "tensor(40.)" ] }, "metadata": { "tags": [] }, "execution_count": 28 } ] }, { "metadata": { "id": "VxUVo1GgC0d-", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "" ], "execution_count": 0, "outputs": [] } ] }