From 9b711e3601ba45241c60af0f8efa0210cadcd327 Mon Sep 17 00:00:00 2001 From: Jean Kossaifi Date: Mon, 15 Apr 2019 14:26:43 +0100 Subject: [PATCH] FIX: dtype for tensor created from np.arange --- 01_tensor_basics/tensor_manipulation.ipynb | 414 ++++++++++++------ .../tucker_decomposition.ipynb | 33 +- ...ecomposition_with_mxnet_and_tensorly.ipynb | 2 +- .../tensor_regression_layer_pytorch.ipynb | 2 +- 4 files changed, 285 insertions(+), 166 deletions(-) diff --git a/01_tensor_basics/tensor_manipulation.ipynb b/01_tensor_basics/tensor_manipulation.ipynb index d0f256f..9eb3169 100644 --- a/01_tensor_basics/tensor_manipulation.ipynb +++ b/01_tensor_basics/tensor_manipulation.ipynb @@ -4,15 +4,7 @@ "cell_type": "code", "execution_count": 1, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Using numpy backend.\n" - ] - } - ], + "outputs": [], "source": [ "import numpy as np\n", "import tensorly as tl" @@ -75,7 +67,7 @@ "metadata": {}, "outputs": [], "source": [ - "X = tl.tensor(np.arange(24).reshape((3, 4, 2)))" + "X = tl.tensor(np.arange(24).reshape((3, 4, 2)), dtype=tl.float32)" ] }, { @@ -86,20 +78,20 @@ { "data": { "text/plain": [ - "array([[[ 0., 1.],\n", - " [ 2., 3.],\n", - " [ 4., 5.],\n", - " [ 6., 7.]],\n", + "array([[[ 0., 1.],\n", + " [ 2., 3.],\n", + " [ 4., 5.],\n", + " [ 6., 7.]],\n", "\n", - " [[ 8., 9.],\n", - " [ 10., 11.],\n", - " [ 12., 13.],\n", - " [ 14., 15.]],\n", + " [[ 8., 9.],\n", + " [10., 11.],\n", + " [12., 13.],\n", + " [14., 15.]],\n", "\n", - " [[ 16., 17.],\n", - " [ 18., 19.],\n", - " [ 20., 21.],\n", - " [ 22., 23.]]])" + " [[16., 17.],\n", + " [18., 19.],\n", + " [20., 21.],\n", + " [22., 23.]]], dtype=float32)" ] }, "execution_count": 3, @@ -126,9 +118,9 @@ { "data": { "text/plain": [ - "array([[ 0., 2., 4., 6.],\n", - " [ 8., 10., 12., 14.],\n", - " [ 16., 18., 20., 22.]])" + "array([[ 0., 2., 4., 6.],\n", + " [ 8., 10., 12., 14.],\n", + " [16., 18., 20., 22.]], dtype=float32)" ] }, "execution_count": 4, @@ -148,9 +140,9 @@ { "data": { "text/plain": [ - "array([[ 1., 3., 5., 7.],\n", - " [ 9., 11., 13., 15.],\n", - " [ 17., 19., 21., 23.]])" + "array([[ 1., 3., 5., 7.],\n", + " [ 9., 11., 13., 15.],\n", + " [17., 19., 21., 23.]], dtype=float32)" ] }, "execution_count": 5, @@ -195,26 +187,16 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "By default, the backend is set to NumPy, here is how to change it, first to MXNet:" + "By default, the backend is set to NumPy, here is how to change it to PyTorch:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Using mxnet backend.\n", - "/anaconda3/lib/python3.6/site-packages/urllib3/contrib/pyopenssl.py:46: DeprecationWarning: OpenSSL.rand is deprecated - you should use os.urandom instead\n", - " import OpenSSL.SSL\n" - ] - } - ], + "outputs": [], "source": [ - "tl.set_backend('mxnet')" + "tl.set_backend('pytorch')" ] }, { @@ -225,7 +207,7 @@ { "data": { "text/plain": [ - "mxnet.ndarray.ndarray.NDArray" + "torch.Tensor" ] }, "execution_count": 8, @@ -238,53 +220,6 @@ "type(X)" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As expected tensors are now represented as MXNet NDArrays. \n", - "\n", - "We can also set the backend to PyTorch:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Using pytorch backend.\n" - ] - } - ], - "source": [ - "tl.set_backend('pytorch')" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "torch.FloatTensor" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "X = tl.tensor(np.arange(24).reshape((3, 4, 2)))\n", - "type(X)" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -294,24 +229,16 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 9, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Using numpy backend.\n" - ] - } - ], + "outputs": [], "source": [ "tl.set_backend('numpy')" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -320,7 +247,7 @@ "numpy.ndarray" ] }, - "execution_count": 12, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -428,7 +355,7 @@ "Finally, the 2-mode unfolding is the unfolding along the last axis:\n", "\n", "$$\n", - " \\tilde X_{[2]} =\n", + " \\tilde X_{[2]} =\n", " \\left[ \\begin{matrix}\n", " 0 & 2 & 4 & 6 & 8 & 10 & 12 & 14 & 16 & 18 & 20 & 22\\\\\n", " 1 & 3 & 5 & 7 & 9 & 11 & 13 & 15 & 17 & 19 & 21 & 23\\\\\n", @@ -440,18 +367,18 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([[ 0., 1., 2., 3., 4., 5., 6., 7.],\n", - " [ 8., 9., 10., 11., 12., 13., 14., 15.],\n", - " [ 16., 17., 18., 19., 20., 21., 22., 23.]])" + "array([[ 0, 1, 2, 3, 4, 5, 6, 7],\n", + " [ 8, 9, 10, 11, 12, 13, 14, 15],\n", + " [16, 17, 18, 19, 20, 21, 22, 23]])" ] }, - "execution_count": 13, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -462,19 +389,19 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([[ 0., 1., 8., 9., 16., 17.],\n", - " [ 2., 3., 10., 11., 18., 19.],\n", - " [ 4., 5., 12., 13., 20., 21.],\n", - " [ 6., 7., 14., 15., 22., 23.]])" + "array([[ 0, 1, 8, 9, 16, 17],\n", + " [ 2, 3, 10, 11, 18, 19],\n", + " [ 4, 5, 12, 13, 20, 21],\n", + " [ 6, 7, 14, 15, 22, 23]])" ] }, - "execution_count": 14, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -485,19 +412,17 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([[ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20.,\n", - " 22.],\n", - " [ 1., 3., 5., 7., 9., 11., 13., 15., 17., 19., 21.,\n", - " 23.]])" + "array([[ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22],\n", + " [ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23]])" ] }, - "execution_count": 15, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -517,29 +442,29 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([[[ 0., 1.],\n", - " [ 2., 3.],\n", - " [ 4., 5.],\n", - " [ 6., 7.]],\n", + "array([[[ 0, 1],\n", + " [ 2, 3],\n", + " [ 4, 5],\n", + " [ 6, 7]],\n", "\n", - " [[ 8., 9.],\n", - " [ 10., 11.],\n", - " [ 12., 13.],\n", - " [ 14., 15.]],\n", + " [[ 8, 9],\n", + " [10, 11],\n", + " [12, 13],\n", + " [14, 15]],\n", "\n", - " [[ 16., 17.],\n", - " [ 18., 19.],\n", - " [ 20., 21.],\n", - " [ 22., 23.]]])" + " [[16, 17],\n", + " [18, 19],\n", + " [20, 21],\n", + " [22, 23]]])" ] }, - "execution_count": 16, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -579,7 +504,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -604,7 +529,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 16, "metadata": { "collapsed": true }, @@ -622,7 +547,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -631,7 +556,7 @@ "(3, 5, 2)" ] }, - "execution_count": 19, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -651,7 +576,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -667,6 +592,59 @@ "print(v.shape)" ] }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0, 1, 2, 3])" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ 0, 1],\n", + " [ 2, 3],\n", + " [ 4, 5],\n", + " [ 6, 7]],\n", + "\n", + " [[ 8, 9],\n", + " [10, 11],\n", + " [12, 13],\n", + " [14, 15]],\n", + "\n", + " [[16, 17],\n", + " [18, 19],\n", + " [20, 21],\n", + " [22, 23]]])" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X" + ] + }, { "cell_type": "code", "execution_count": 21, @@ -682,7 +660,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Since we have multiplied by a vector, we have effectively contracted out one mode of the tensor so the result is a matrix:" + "Recall that X is of shape (3, 4, 2). We are here contracting over its 2nd mode. \n", + "\n", + "Since we have multiplied by a vector, we have effectively contracted out one mode of the tensor so the result will be of matrix of size (3, 2). " ] }, { @@ -704,6 +684,160 @@ "source": [ "res.shape" ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 28, 34],\n", + " [ 76, 82],\n", + " [124, 130]])" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "res" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here, we could form each column (fiber) of the result, by taking the dot product between the frontal slices of X and the vector:" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 28, 76, 124])" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X[:, :, 0] @ v" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 34, 82, 130])" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X[:, :, 1] @ v" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can form the whole product by concanating these as the column of the resulting matrix:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 28, 34],\n", + " [ 76, 82],\n", + " [124, 130]])" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.stack([X[:, :, 0] @ v, X[:, :, 1] @ v]).T" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We could equivalently use the mode-0 slices:" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[28 34]\n", + "[76 82]\n", + "[124 130]\n" + ] + } + ], + "source": [ + "for i in range(3):\n", + " print(X[i, ...].T @ v)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Again, we can form the whole result by stacking these as the rows of the matrix:" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 28, 34],\n", + " [ 76, 82],\n", + " [124, 130]])" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.vstack([X[i, ...].T @ v for i in range(3)])" + ] } ], "metadata": { @@ -722,7 +856,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.7.1" } }, "nbformat": 4, diff --git a/02_tensor_decomposition/tucker_decomposition.ipynb b/02_tensor_decomposition/tucker_decomposition.ipynb index 3ce02b0..6d5a615 100644 --- a/02_tensor_decomposition/tucker_decomposition.ipynb +++ b/02_tensor_decomposition/tucker_decomposition.ipynb @@ -4,15 +4,7 @@ "cell_type": "code", "execution_count": 1, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Using numpy backend.\n" - ] - } - ], + "outputs": [], "source": [ "import tensorly as tl\n", "import numpy as np" @@ -53,16 +45,16 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "X = tl.tensor(np.arange(24).reshape((3, 4, 2)))" + "X = tl.tensor(np.arange(24).reshape((3, 4, 2)), dtype=tl.float32)" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -71,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -80,7 +72,7 @@ "(3, 4, 2)" ] }, - "execution_count": 6, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -98,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -107,7 +99,7 @@ "(3, 4, 2)" ] }, - "execution_count": 7, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -116,13 +108,6 @@ "full_tensor = tl.tucker_to_tensor(core, factors)\n", "full_tensor.shape" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -141,7 +126,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.7.1" } }, "nbformat": 4, diff --git a/04_mxnet_backend/tucker_decomposition_with_mxnet_and_tensorly.ipynb b/04_mxnet_backend/tucker_decomposition_with_mxnet_and_tensorly.ipynb index 6455664..5a2a2ec 100644 --- a/04_mxnet_backend/tucker_decomposition_with_mxnet_and_tensorly.ipynb +++ b/04_mxnet_backend/tucker_decomposition_with_mxnet_and_tensorly.ipynb @@ -260,7 +260,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.6.4" } }, "nbformat": 4, diff --git a/05_pytorch_backend/tensor_regression_layer_pytorch.ipynb b/05_pytorch_backend/tensor_regression_layer_pytorch.ipynb index dd1a0bf..2890052 100644 --- a/05_pytorch_backend/tensor_regression_layer_pytorch.ipynb +++ b/05_pytorch_backend/tensor_regression_layer_pytorch.ipynb @@ -599,7 +599,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.6.6" } }, "nbformat": 4,