From 9063a3bf5f2268cc02f27e4d3eb6c1c482ad9163 Mon Sep 17 00:00:00 2001 From: Eduardo Cueto Mendoza Date: Wed, 15 Apr 2020 12:11:12 -0600 Subject: [PATCH] [MOD] added more examples --- Numpy.ipynb | 37 +++++- Pytorch_1.ipynb | 330 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 366 insertions(+), 1 deletion(-) diff --git a/Numpy.ipynb b/Numpy.ipynb index 28119fb..e9b1e57 100644 --- a/Numpy.ipynb +++ b/Numpy.ipynb @@ -496,6 +496,41 @@ "mat[my_filter]" ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def find_max(nums):\n", + " max_num = float(\"-inf\") # smaller than all other numbers\n", + " for num in nums:\n", + " if num > max_num:\n", + " # (Fill in the missing line here)\n", + " max_num = num\n", + " return max_num" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30.1" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "find_max([10.0,30.1,21.5])" + ] + }, { "cell_type": "code", "execution_count": null, @@ -520,7 +555,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.7.6" } }, "nbformat": 4, diff --git a/Pytorch_1.ipynb b/Pytorch_1.ipynb index cf3d422..094e22e 100644 --- a/Pytorch_1.ipynb +++ b/Pytorch_1.ipynb @@ -38,6 +38,336 @@ "torch.cuda.is_available()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Torch tensors" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([2, 2, 1])\n" + ] + } + ], + "source": [ + "# This is a 1D tensor\n", + "a = torch.tensor([2,2,1])\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([[ 2, 3, 4],\n", + " [ 1, 2, 3],\n", + " [ 0, 8, 7],\n", + " [10, 11, 12]])\n" + ] + } + ], + "source": [ + "# This is a 2D tensor\n", + "b = torch.tensor([[2,3,4],[1,2,3],[0,8,7],[10,11,12]])\n", + "print(b)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch.Size([3])\n", + "torch.Size([3])\n", + "torch.Size([4, 3])\n", + "torch.Size([4, 3])\n" + ] + } + ], + "source": [ + "# the size of a tensor\n", + "print(a.shape)\n", + "print(a.size())\n", + "print(b.shape)\n", + "print(b.size())" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "# get the number of rows\n", + "print(b.shape[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "c = torch.FloatTensor([[2,3,4],[1,2,3],[0,8,7],[10,11,12]])" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "d = torch.DoubleTensor([[2,3,4],[1,2,3],[0,8,7],[10,11,12]])" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([[ 2., 3., 4.],\n", + " [ 1., 2., 3.],\n", + " [ 0., 8., 7.],\n", + " [10., 11., 12.]])\n", + "torch.float32\n" + ] + } + ], + "source": [ + "print(c)\n", + "print(c.dtype)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([[ 2., 3., 4.],\n", + " [ 1., 2., 3.],\n", + " [ 0., 8., 7.],\n", + " [10., 11., 12.]], dtype=torch.float64)\n", + "torch.float64\n" + ] + } + ], + "source": [ + "print(d)\n", + "print(d.dtype)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor(5.2500)\n" + ] + } + ], + "source": [ + "print(c.mean())" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor(5.2500, dtype=torch.float64)\n" + ] + } + ], + "source": [ + "print(d.mean())" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor(4.1588)\n" + ] + } + ], + "source": [ + "print(c.std())" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor(4.1588, dtype=torch.float64)\n" + ] + } + ], + "source": [ + "print(d.std())" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([[ 2],\n", + " [ 3],\n", + " [ 4],\n", + " [ 1],\n", + " [ 2],\n", + " [ 3],\n", + " [ 0],\n", + " [ 8],\n", + " [ 7],\n", + " [10],\n", + " [11],\n", + " [12]])\n", + "tensor([ 2, 3, 4, 1, 2, 3, 0, 8, 7, 10, 11, 12])\n", + "tensor([[ 2, 3, 4, 1],\n", + " [ 2, 3, 0, 8],\n", + " [ 7, 10, 11, 12]])\n", + "tensor([[ 2, 3, 4, 1, 2, 3, 0, 8, 7, 10, 11, 12]])\n", + "torch.Size([1, 12])\n", + "\n", + "\n", + "\n", + "\n", + "tensor([[[ 2.3594, 0.3172, -2.0944, 0.5019],\n", + " [ 0.4300, 0.4082, -0.1532, 0.6677],\n", + " [-1.0891, 1.3434, -0.2586, -1.2845]],\n", + "\n", + " [[-2.2463, -0.6665, 0.5781, 1.1152],\n", + " [-1.9292, -0.5129, -0.2301, -0.1059],\n", + " [-0.5839, 2.9470, -0.6610, 1.1021]]])\n", + "tensor([[ 2.3594, 0.3172, -2.0944, 0.5019, 0.4300, 0.4082, -0.1532, 0.6677,\n", + " -1.0891, 1.3434, -0.2586, -1.2845],\n", + " [-2.2463, -0.6665, 0.5781, 1.1152, -1.9292, -0.5129, -0.2301, -0.1059,\n", + " -0.5839, 2.9470, -0.6610, 1.1021]])\n", + "tensor([[ 2.3594, 0.3172, -2.0944, 0.5019, 0.4300, 0.4082, -0.1532, 0.6677,\n", + " -1.0891, 1.3434, -0.2586, -1.2845],\n", + " [-2.2463, -0.6665, 0.5781, 1.1152, -1.9292, -0.5129, -0.2301, -0.1059,\n", + " -0.5839, 2.9470, -0.6610, 1.1021]])\n" + ] + } + ], + "source": [ + "print(b.view(-1,1))\n", + "print(b.view(12))\n", + "print(b.view(-1,4))\n", + "\n", + "b = b.view(1,-1)\n", + "print(b)\n", + "print(b.shape)\n", + "print(\"\\n\")\n", + "#this is the format of a Tensor (channels,rows,columns)\n", + "three_dim = torch.randn(2,3,4)\n", + "print(\"\\n\")\n", + "print(three_dim)\n", + "print(three_dim.view(2,12))\n", + "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,