From ae23cdd1245de41c8010d3c9e9539f8ad3773d1e Mon Sep 17 00:00:00 2001 From: EddieCueto Date: Thu, 16 Apr 2020 13:29:41 -0600 Subject: [PATCH] [MOD] changes from secondary platform --- Numpy_Bridge.ipynb | 246 ++++++++++++++++++++++++++++++++++ Pytorch_1.ipynb | 44 ------- Pytorch_2.ipynb | 319 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 565 insertions(+), 44 deletions(-) create mode 100644 Numpy_Bridge.ipynb create mode 100644 Pytorch_2.ipynb diff --git a/Numpy_Bridge.ipynb b/Numpy_Bridge.ipynb new file mode 100644 index 0000000..3c2ed60 --- /dev/null +++ b/Numpy_Bridge.ipynb @@ -0,0 +1,246 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import torch\n", + "import torchvision\n", + "torch.cuda.is_available()" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([1., 1., 1., 1., 1.])\n", + "[1. 1. 1. 1. 1.]\n", + "tensor([2., 2., 2., 2., 2.])\n", + "[2. 2. 2. 2. 2.]\n" + ] + } + ], + "source": [ + "# the numpy array is just a pointer to the torch tensor\n", + "a = torch.ones(5)\n", + "print(a)\n", + "b = a.numpy()\n", + "print(b)\n", + "a.add_(1)\n", + "print(a)\n", + "print(b)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2. 2. 2. 2. 2.]\n", + "tensor([2., 2., 2., 2., 2.], dtype=torch.float64)\n" + ] + } + ], + "source": [ + "a = np.ones(5)\n", + "b = torch.from_numpy(a)\n", + "np.add(a,1,out=a)\n", + "print(a)\n", + "print(b)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "r2 = torch.randn(4,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "r = torch.rand(4,4)\n", + "\n", + "add_result = torch.add(r,r2)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([[-0.5124, -0.0635, -1.0141, 0.2371],\n", + " [ 0.7809, -1.0184, 1.1734, -0.4778],\n", + " [ 1.6340, 0.0201, 0.4821, -0.1986],\n", + " [ 1.7512, -0.1389, 1.4546, -0.2230]], device='cuda:0')\n" + ] + } + ], + "source": [ + "# Move tensor to GPU\n", + "r2 = r2.cuda()\n", + "print(r2)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "tensor([[ 0.0573, 0.7549, -0.1884, 0.6424],\n", + " [ 0.9001, -0.9460, 2.1332, 0.0502],\n", + " [ 1.7473, 0.6463, 0.5804, 0.2859],\n", + " [ 2.0273, 0.6695, 2.4453, 0.4559]], device='cuda:0')\n" + ] + } + ], + "source": [ + "CUDA = torch.cuda.is_available()\n", + "print(CUDA)\n", + "\n", + "if CUDA:\n", + " add_result = add_result.cuda()\n", + " print(add_result)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([[-0.4551, 0.6915, -1.2024, 0.8795],\n", + " [ 1.6810, -1.9643, 3.3066, -0.4276],\n", + " [ 3.3813, 0.6664, 1.0625, 0.0872],\n", + " [ 3.7784, 0.5306, 3.8999, 0.2329]], device='cuda:0')" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add_result.add(r2)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 3, 4, 1]\n", + "tensor([2, 3, 4, 1]) torch.int64\n" + ] + } + ], + "source": [ + "a = [2,3,4,1]\n", + "print(a)\n", + "to_list = torch.tensor(a)\n", + "print(to_list, to_list.dtype)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([[1, 2],\n", + " [3, 4],\n", + " [5, 6],\n", + " [7, 8]]) torch.int64\n" + ] + } + ], + "source": [ + "data = [[1,2],[3,4],[5,6],[7,8]]\n", + "T = torch.tensor(data)\n", + "print(T,T.dtype)" + ] + }, + { + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/Pytorch_1.ipynb b/Pytorch_1.ipynb index 094e22e..3e1d4bb 100644 --- a/Pytorch_1.ipynb +++ b/Pytorch_1.ipynb @@ -324,50 +324,6 @@ "print(three_dim.view(2,-1))" ] }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "tensor([[0.8131, 0.3529, 0.4527, 0.8643],\n", - " [0.5623, 0.7266, 0.7129, 0.4911],\n", - " [0.3165, 0.8044, 0.8082, 0.4713],\n", - " [0.7482, 0.7304, 0.9080, 0.0568]])\n" - ] - } - ], - "source": [ - "r = torch.rand(4,4)\n", - "print(r)" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "tensor([[ 0.0370, -0.3710, 0.8986, -1.1703],\n", - " [-0.8784, -0.7126, 0.4817, -0.9874],\n", - " [ 0.2186, -1.7692, 1.3162, -1.2127],\n", - " [ 0.2576, -0.4097, -1.5554, 0.2979]])\n", - "torch.float32\n" - ] - } - ], - "source": [ - "r2 = torch.randn(4,4)\n", - "print(r2)\n", - "print(r2.dtype)" - ] - }, { "cell_type": "code", "execution_count": null, diff --git a/Pytorch_2.ipynb b/Pytorch_2.ipynb new file mode 100644 index 0000000..fddbccc --- /dev/null +++ b/Pytorch_2.ipynb @@ -0,0 +1,319 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import torch" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import torchvision" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "torch.cuda.is_available()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([[0.2504, 0.6128, 0.9066, 0.9701],\n", + " [0.9145, 0.5638, 0.2492, 0.8657],\n", + " [0.9521, 0.5454, 0.6647, 0.9666],\n", + " [0.9705, 0.8375, 0.9598, 0.4804]])\n" + ] + } + ], + "source": [ + "r = torch.rand(4,4)\n", + "print(r)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([[-0.6323, -0.4558, -0.9853, 1.5795],\n", + " [-1.5415, 0.3864, -0.0094, 0.4048],\n", + " [ 1.2190, 0.7174, 0.0796, 0.0580],\n", + " [-0.8419, 1.5195, 0.9428, 0.5261]])\n", + "torch.float32\n" + ] + } + ], + "source": [ + "r2 = torch.randn(4,4)\n", + "print(r2)\n", + "print(r2.dtype)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([7, 9, 9, 8, 8])\n", + "torch.int64\n" + ] + } + ], + "source": [ + "in_array = torch.randint(6,10,(5,))\n", + "print(in_array)\n", + "print(in_array.dtype)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([[6, 6, 7],\n", + " [9, 7, 6],\n", + " [9, 8, 6]])\n" + ] + } + ], + "source": [ + "in_array_2 = torch.randint(6,10,(3,3))\n", + "print(in_array_2)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "9\n" + ] + } + ], + "source": [ + "print(torch.numel(in_array))\n", + "print(torch.numel(in_array_2))" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([[0, 0, 0],\n", + " [0, 0, 0],\n", + " [0, 0, 0]])\n", + "torch.int64\n", + "tensor([[1., 1., 1.],\n", + " [1., 1., 1.],\n", + " [1., 1., 1.]])\n", + "torch.float32\n" + ] + } + ], + "source": [ + "z = torch.zeros(3,3, dtype=torch.long)\n", + "print(z)\n", + "print(z.dtype)\n", + "o = torch.ones(3,3)\n", + "print(o)\n", + "print(o.dtype)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([[ 0.0830, -0.5093, -0.0874, 0.4360],\n", + " [-0.4503, 1.4857, -0.9082, 0.9302],\n", + " [-1.1958, 0.2207, 1.0333, 0.1410],\n", + " [-0.0071, -2.7461, 0.8460, 0.8057]], dtype=torch.float64)\n" + ] + } + ], + "source": [ + "r2_like = torch.randn_like(r2, dtype=torch.double)\n", + "print(r2_like)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([[-0.3819, 0.1570, -0.0787, 2.5496],\n", + " [-0.6270, 0.9503, 0.2398, 1.2705],\n", + " [ 2.1711, 1.2629, 0.7444, 1.0246],\n", + " [ 0.1286, 2.3570, 1.9026, 1.0065]])\n" + ] + } + ], + "source": [ + "add_result = torch.add(r,r2)\n", + "print(add_result)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "torch.Size([4, 4])" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add_result.size()" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([[-0.3819, 0.1570, -0.0787, 2.5496],\n", + " [-0.6270, 0.9503, 0.2398, 1.2705],\n", + " [ 2.1711, 1.2629, 0.7444, 1.0246],\n", + " [ 0.1286, 2.3570, 1.9026, 1.0065]])\n" + ] + } + ], + "source": [ + "# this function reassings the value to r2\n", + "r2.add_(r)\n", + "print(r2)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([0.1570, 0.9503, 1.2629, 2.3570])\n", + "tensor([[-0.3819, 0.1570],\n", + " [-0.6270, 0.9503],\n", + " [ 2.1711, 1.2629],\n", + " [ 0.1286, 2.3570]])\n", + "tensor([[-0.3819, 0.1570, -0.0787, 2.5496],\n", + " [-0.6270, 0.9503, 0.2398, 1.2705],\n", + " [ 2.1711, 1.2629, 0.7444, 1.0246]])\n", + "tensor(1.0246)\n", + "1.0245939493179321\n", + "tensor([2.1711, 1.2629, 0.7444, 1.0246])\n" + ] + } + ], + "source": [ + "print(r2[:,1])\n", + "print(r2[:,:2])\n", + "print(r2[:3,:])\n", + "num_ten = r2[2,3]\n", + "print(num_ten)\n", + "print(num_ten.item())\n", + "print(r2[2,:])" + ] + }, + { + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}