{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.4.1\n" ] } ], "source": [ "import torch\n", "print(torch.__version__)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Tensor x torch.FloatTensor torch.Size([2, 3])\n", "tensor([[0.0091, 0.7210, 0.2294],\n", " [0.4992, 0.3683, 0.0428]])\n", "-----------\n", "Tensor y torch.FloatTensor torch.Size([2, 3])\n", "tensor([[0., 0., 0.],\n", " [0., 0., 0.]])\n" ] } ], "source": [ "x = torch.rand(2,3)\n", "y = torch.zeros(2,3)\n", "print('Tensor x ',x.type(), x.size())\n", "print(x)\n", "print('-----------')\n", "print('Tensor y ',y.type(), y.size())\n", "print(y)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "torch.LongTensor torch.Size([2, 3])\n", "tensor([[1, 2, 3],\n", " [4, 5, 6]])\n" ] } ], "source": [ "import numpy as np\n", "x1 = np.ndarray(shape=(2,3), dtype=int,buffer=np.array([1,2,3,4,5,6]))\n", "x2 = torch.from_numpy(x1)\n", "print(x2.type(),x2.size())\n", "print(x2)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x1 = np.array([[1,2],[3,4]])\n", "x2 = torch.from_numpy(x1)\n", "x2\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "int64 (2, 2)\n", "[[1 2]\n", " [3 4]]\n" ] } ], "source": [ "x3 = x2.numpy() # Convert tensor to numpy\n", "print(x3.dtype,x3.shape)\n", "print(x3)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x= tensor([[0.6394, 0.7000, 0.9000],\n", " [0.4809, 0.7579, 0.9847]])\n", "out1= tensor([[0.6394, 0.9000],\n", " [0.4809, 0.9847]])\n", "out2= tensor([[0.6394, 0.7000],\n", " [0.4809, 0.7579]])\n" ] } ], "source": [ "#Indexing and Slicing\n", "# torch.index_select(input, dim, index)\n", "\n", "x = torch.rand(2,3)\n", "out1 = torch.index_select(x,1,torch.LongTensor([0,2]))\n", "out2 = x[:,0:2]\n", "print('x=',x)\n", "print('out1=',out1)\n", "print('out2=',out2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Math" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "add op tensor([[ 2., 4.],\n", " [ 8., 10.]])\n", "direct add tensor([[ 2., 4.],\n", " [ 8., 10.]])\n", "eigen values (tensor([[ 0.0000e+00, 0.0000e+00],\n", " [-6.5430e+16, 0.0000e+00],\n", " [-2.7031e+15, 0.0000e+00],\n", " [ 0.0000e+00, 0.0000e+00]]), tensor([]))\n" ] } ], "source": [ "x1 = torch.FloatTensor([[1,2],[4,5]])\n", "x2 = torch.FloatTensor([[1,2],[4,5]])\n", "add = torch.add(x1,x2)\n", "print('add op',add)\n", "print('direct add',x1+x2)\n", "\n", "x3 = torch.FloatTensor(4,4) # initializes a 4x4 tensor, does not hold meaningful values\n", "eigen_value = torch.eig(x3) # torch.eig(a,eigenvectors=False) -> eigen_value, eigen_vector\n", "print(\"eigen values\",eigen_value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# AutoGrad\n", "#### Modified by Dr. Srijith Rajamohan for PyTorch 0.4" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor(48.)\n", "tensor(32.)\n", "tensor(304.)\n" ] } ], "source": [ "import torch\n", "x = torch.tensor(4.0,requires_grad=True)\n", "z = x**3\n", "z.backward()\n", "print(x.grad) # gradient is dz/dx\n", "a = x**2\n", "y = a**2\n", "a.register_hook(print) # This gradient will be optimized away because this is an intermediate variable\n", "y.backward()\n", "print(x.grad) # gradient now is dy/dx + dz/dx" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([[21.5000],\n", " [38.0000]], grad_fn=)\n", "None\n", "tensor([ 7.0000, 9.0000, 11.5000])\n" ] } ], "source": [ "a = torch.tensor([[2.5,3.5,4.0],[4.5,5.5,7.5]],requires_grad=False)\n", "w = torch.tensor([1.0,2.0,3.0],requires_grad=True)\n", "loss = torch.mm(a,w.view(-1,1)) # a * w\n", "print(loss)\n", "print(w.grad) # Gradient only computed after backward() is called\n", "loss.backward(torch.ones_like(loss),retain_graph=True) #If your loss or objective function is not a scalar, you have\n", "# to pass an array of ones (can also be corresponding weights) corresponding to the size of this loss vector. \n", "print(w.grad)\n", "w.grad = torch.zeros(w.grad.size()) # Manually set the grad to zero" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.0" } }, "nbformat": 4, "nbformat_minor": 2 }