{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "source": [ "# MatRepr TensorFlow\n", "\n", "Neatly format dense `tf.Tensor` and sparse `tf.SparseTensor`.\n", "\n", "1D dense and sparse tensors are formatted as row vectors.\n", "\n", "2D dense and sparse tensors are formatted as matrices.\n", "\n", "3D+ sparse tensors are formatted as a list of index/value tuples. Dense 3D+ tensors rendered with native TensorFlow formatter." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.719162Z", "start_time": "2023-09-07T02:24:05.388424Z" }, "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "import tensorflow as tf\n", "\n", "tf.random.set_seed(1234)\n", "\n", "# so matrepr can be imported from the source tree.\n", "import sys\n", "sys.path.insert(0, '..')\n", "\n", "from matrepr import mdisplay, mprint" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.731957Z", "start_time": "2023-09-07T02:24:07.726804Z" }, "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "scalar = tf.constant(5)\n", "\n", "dense1D = tf.random.uniform(shape=(5000,))\n", "\n", "rand2D = tf.random.uniform(shape=(128, 64)).numpy()\n", "rand2D[rand2D < 0.6] = 0\n", "rand2D_sparse = tf.sparse.from_dense(tf.convert_to_tensor(rand2D))\n", "\n", "small3D = tf.constant([[[1., 0], [2., 3.]], [[4., 0], [5., 6.]]])\n", "coo3D = tf.sparse.from_dense(small3D)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## TensorFlow default formatting" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.736208Z", "start_time": "2023-09-07T02:24:07.733681Z" }, "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dense1D" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.738960Z", "start_time": "2023-09-07T02:24:07.736841Z" } }, "outputs": [ { "data": { "text/plain": [ "SparseTensor(indices=tf.Tensor(\n", "[[ 0 2]\n", " [ 0 6]\n", " [ 0 7]\n", " ...\n", " [127 44]\n", " [127 45]\n", " [127 46]], shape=(3304, 2), dtype=int64), values=tf.Tensor([0.64804935 0.7057973 0.7185966 ... 0.79349816 0.85203743 0.95652413], shape=(3304,), dtype=float32), dense_shape=tf.Tensor([128 64], shape=(2,), dtype=int64))" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand2D_sparse" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.757535Z", "start_time": "2023-09-07T02:24:07.739939Z" } }, "outputs": [ { "data": { "text/plain": [ "SparseTensor(indices=tf.Tensor(\n", "[[0 0 0]\n", " [0 1 0]\n", " [0 1 1]\n", " [1 0 0]\n", " [1 1 0]\n", " [1 1 1]], shape=(6, 3), dtype=int64), values=tf.Tensor([1. 2. 3. 4. 5. 6.], shape=(6,), dtype=float32), dense_shape=tf.Tensor([2 2 2], shape=(3,), dtype=int64))" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "coo3D" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.757704Z", "start_time": "2023-09-07T02:24:07.743212Z" } }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scalar" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2023-09-06T22:34:13.317717Z", "start_time": "2023-09-06T22:34:13.315433Z" } }, "source": [ "## MatRepr default\n", "\n", "Load the MatRepr Jupyter extension with `%load_ext matrepr` to render tensors with MatRepr by default.\n", "\n", "A single-use alternative is to use `matrepr.mdisplay()`. For console use `matrepr.mprint()`. " ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.757779Z", "start_time": "2023-09-07T02:24:07.746603Z" }, "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "%load_ext matrepr" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.757920Z", "start_time": "2023-09-07T02:24:07.748819Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
length=5000, '<dtype: 'float32'>' elements, tf.Tensor
01234564993499449954996499749984999
0.5380.36460.58160.24380.43030.90620.50720.58990.63220.2410.83750.54410.86710.5085
\n", "
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dense1D" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.762976Z", "start_time": "2023-09-07T02:24:07.757361Z" }, "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
128×64, 3304 '<dtype: 'float32'>' elements, tf.SparseTensor
012345657585960616263
00.6480.70580.61980.77380.8016
10.84060.730.78610.65250.90520.83480.66380.735
20.67360.92760.83740.88450.6323
30.70250.82440.75930.92230.8540.62560.71320.66760.7929
40.94820.73160.87790.76490.78840.6560.9197
1230.8794
1240.98920.89180.82950.66830.64790.78280.90910.99660.6415
1250.79460.7290.95760.6766
1260.90590.87070.98990.98930.77880.71750.6775
1270.95230.64850.98660.85370.8772
\n", "
" ], "text/plain": [ "SparseTensor(indices=tf.Tensor(\n", "[[ 0 2]\n", " [ 0 6]\n", " [ 0 7]\n", " ...\n", " [127 44]\n", " [127 45]\n", " [127 46]], shape=(3304, 2), dtype=int64), values=tf.Tensor([0.64804935 0.7057973 0.7185966 ... 0.79349816 0.85203743 0.95652413], shape=(3304,), dtype=float32), dense_shape=tf.Tensor([128 64], shape=(2,), dtype=int64))" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand2D_sparse" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.767371Z", "start_time": "2023-09-07T02:24:07.763316Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
shape=(2,2,2), 6 '<dtype: 'float32'>' elements, tf.SparseTensor
012val
00001
10102
20113
31004
41105
51116
\n", "
" ], "text/plain": [ "SparseTensor(indices=tf.Tensor(\n", "[[0 0 0]\n", " [0 1 0]\n", " [0 1 1]\n", " [1 0 0]\n", " [1 1 0]\n", " [1 1 1]], shape=(6, 3), dtype=int64), values=tf.Tensor([1. 2. 3. 4. 5. 6.], shape=(6,), dtype=float32), dense_shape=tf.Tensor([2 2 2], shape=(3,), dtype=int64))" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "coo3D" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.771256Z", "start_time": "2023-09-07T02:24:07.766799Z" } }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scalar" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Labels\n", "\n", "Specify title, row and/or column labels to help the reader quickly understand what they are looking at." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.773032Z", "start_time": "2023-09-07T02:24:07.770201Z" } }, "outputs": [], "source": [ "dense = tf.random.uniform(shape=(64, 32)).numpy()\n", "dense[dense < 0.3] = 0\n", "tensor = tf.sparse.from_dense(tf.convert_to_tensor(dense))\n", "\n", "obs_labels = [f\"observation {i}\" for i in range(tensor.shape[0])] # list of labels\n", "feature_labels = {i: f\"feature {i+1}\" for i in range(tensor.shape[1])} # map index to label works too" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.805805Z", "start_time": "2023-09-07T02:24:07.773744Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Random Dataset
feature 1feature 2feature 3feature 4feature 5feature 6feature 7feature 26feature 27feature 28feature 29feature 30feature 31feature 32
observation 00.59750.30310.99770.80830.41710.61050.72310.35320.35220.88340.9973
observation 10.38440.57020.3390.53190.64150.50320.83560.49360.93320.94750.42260.6899
observation 20.59640.52830.39820.38990.79320.56760.93340.91490.7552
observation 30.52330.52050.58630.99820.69520.44360.7584
observation 40.61050.9570.63810.6850.77760.30060.30440.97210.3244
observation 590.79090.83130.83690.90050.60930.48840.44280.84360.5978
observation 600.39020.6670.43580.38270.57850.95490.61730.5166
observation 610.76130.53930.62660.96740.51470.6740.41180.69960.73290.82750.66950.7466
observation 620.91730.70290.79950.47930.34440.46010.41840.30940.55780.90820.67660.7963
observation 630.73210.40440.73960.54920.74150.73270.31940.39590.98890.51870.82230.6995
\n", "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(tensor, row_labels=obs_labels, col_labels=feature_labels, title=\"Random Dataset\")" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2023-09-06T22:34:13.356497Z", "start_time": "2023-09-06T22:34:13.338373Z" } }, "source": [ "## LaTeX" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.810622Z", "start_time": "2023-09-07T02:24:07.780488Z" }, "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/latex": [ "$\\begin{bmatrix}\n", " & & 0.648 & & & & 0.7058 & \\dots & 0.6198 & & 0.7738 & & & 0.8016 & \\\\\n", " & & & 0.8406 & & & 0.73 & \\dots & 0.7861 & 0.6525 & 0.9052 & & 0.8348 & 0.6638 & 0.735 \\\\\n", " & & & & & 0.6736 & & \\dots & 0.9276 & 0.8374 & & & 0.8845 & & 0.6323 \\\\\n", " 0.7025 & & 0.8244 & & 0.7593 & 0.9223 & 0.854 & \\dots & & & 0.6256 & 0.7132 & 0.6676 & 0.7929 & \\\\\n", " 0.9482 & 0.7316 & & 0.8779 & & 0.7649 & & \\dots & & & 0.7884 & 0.656 & 0.9197 & & \\\\\n", " \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\ddots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots \\\\\n", " & 0.8794 & & & & & & \\dots & & & & & & & \\\\\n", " 0.9892 & 0.8918 & & 0.8295 & 0.6683 & 0.6479 & & \\dots & 0.7828 & & 0.9091 & 0.9966 & & 0.6415 & \\\\\n", " & & 0.7946 & & & & & \\dots & 0.729 & 0.9576 & & & & & 0.6766 \\\\\n", " & 0.9059 & 0.8707 & & & & & \\dots & & 0.9899 & & 0.9893 & 0.7788 & 0.7175 & 0.6775 \\\\\n", " & 0.9523 & 0.6485 & & 0.9866 & 0.8537 & 0.8772 & \\dots & & & & & & & \n", "\\end{bmatrix}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mdisplay(rand2D_sparse, \"latex\")" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2023-09-06T22:34:13.360119Z", "start_time": "2023-09-06T22:34:13.354250Z" }, "collapsed": false, "jupyter": { "outputs_hidden": false } }, "source": [ "## String" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.814564Z", "start_time": "2023-09-07T02:24:07.786166Z" }, "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<128×64, 3304 '' elements, tf.SparseTensor>\n", " 0 1 2 3 60 61 62 63\n", " ┌ ┐\n", " 0 │ 0.648 ... 0.8016 │\n", " 1 │ 0.6572 ... 0.8348 0.6638 0.735 │\n", " 2 │ ... 0.8845 0.6323 │\n", " 3 │ 0.7025 0.8244 0.9619 ... 0.7132 0.6676 0.7929 │\n", " 4 │ 0.9482 0.7316 ... 0.656 0.9197 │\n", " │ : : : : ... : : : : │\n", "123 │ 0.8794 ... │\n", "124 │ 0.9892 0.8918 0.8262 ... 0.9966 0.6415 │\n", "125 │ 0.7946 ... 0.6766 │\n", "126 │ 0.9059 0.8707 ... 0.9893 0.7788 0.7175 0.6775 │\n", "127 │ 0.9523 0.6485 0.9578 ... │\n", " └ ┘\n" ] } ], "source": [ "mprint(rand2D_sparse)" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2023-09-06T22:34:13.387721Z", "start_time": "2023-09-06T22:34:13.384682Z" } }, "source": [ "## More compact size" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.816173Z", "start_time": "2023-09-07T02:24:07.813042Z" }, "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "import matrepr\n", "matrepr.params.max_rows = 10\n", "matrepr.params.max_cols = 7\n", "matrepr.params.num_after_dots = 0" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.819530Z", "start_time": "2023-09-07T02:24:07.816963Z" }, "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
128×64, 3304 '<dtype: 'float32'>' elements, tf.SparseTensor
012345
00.648
10.8406
20.6736
30.70250.82440.75930.9223
40.94820.73160.87790.7649
50.890.6193
60.96390.7124
70.9725
80.96520.78690.98340.74220.776
\n", "
" ], "text/plain": [ "SparseTensor(indices=tf.Tensor(\n", "[[ 0 2]\n", " [ 0 6]\n", " [ 0 7]\n", " ...\n", " [127 44]\n", " [127 45]\n", " [127 46]], shape=(3304, 2), dtype=int64), values=tf.Tensor([0.64804935 0.7057973 0.7185966 ... 0.79349816 0.85203743 0.95652413], shape=(3304,), dtype=float32), dense_shape=tf.Tensor([128 64], shape=(2,), dtype=int64))" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand2D_sparse" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2023-09-07T02:24:07.821175Z", "start_time": "2023-09-07T02:24:07.819658Z" }, "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }