Neatly format dense tf.Tensor
and sparse tf.SparseTensor
.
1D dense and sparse tensors are formatted as row vectors.
2D dense and sparse tensors are formatted as matrices.
3D+ sparse tensors are formatted as a list of index/value tuples. Dense 3D+ tensors rendered with native TensorFlow formatter.
import tensorflow as tf
tf.random.set_seed(1234)
# so matrepr can be imported from the source tree.
import sys
sys.path.insert(0, '..')
from matrepr import mdisplay, mprint
scalar = tf.constant(5)
dense1D = tf.random.uniform(shape=(5000,))
rand2D = tf.random.uniform(shape=(128, 64)).numpy()
rand2D[rand2D < 0.6] = 0
rand2D_sparse = tf.sparse.from_dense(tf.convert_to_tensor(rand2D))
small3D = tf.constant([[[1., 0], [2., 3.]], [[4., 0], [5., 6.]]])
coo3D = tf.sparse.from_dense(small3D)
dense1D
<tf.Tensor: shape=(5000,), dtype=float32, numpy= array([0.5380393 , 0.36461866, 0.5816301 , ..., 0.5440619 , 0.8671317 , 0.5084995 ], dtype=float32)>
rand2D_sparse
SparseTensor(indices=tf.Tensor( [[ 0 2] [ 0 6] [ 0 7] ... [127 44] [127 45] [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))
coo3D
SparseTensor(indices=tf.Tensor( [[0 0 0] [0 1 0] [0 1 1] [1 0 0] [1 1 0] [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))
scalar
<tf.Tensor: shape=(), dtype=int32, numpy=5>
Load the MatRepr Jupyter extension with %load_ext matrepr
to render tensors with MatRepr by default.
A single-use alternative is to use matrepr.mdisplay()
. For console use matrepr.mprint()
.
%load_ext matrepr
dense1D
0 | 1 | 2 | 3 | 4 | 5 | 6 | 4993 | 4994 | 4995 | 4996 | 4997 | 4998 | 4999 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0.538 | 0.3646 | 0.5816 | 0.2438 | 0.4303 | 0.9062 | 0.5072 | ⋯ | 0.5899 | 0.6322 | 0.241 | 0.8375 | 0.5441 | 0.8671 | 0.5085 |
rand2D_sparse
0 | 1 | 2 | 3 | 4 | 5 | 6 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.648 | 0.7058 | ⋯ | 0.6198 | 0.7738 | 0.8016 | |||||||||
1 | 0.8406 | 0.73 | ⋯ | 0.7861 | 0.6525 | 0.9052 | 0.8348 | 0.6638 | 0.735 | ||||||
2 | 0.6736 | ⋯ | 0.9276 | 0.8374 | 0.8845 | 0.6323 | |||||||||
3 | 0.7025 | 0.8244 | 0.7593 | 0.9223 | 0.854 | ⋯ | 0.6256 | 0.7132 | 0.6676 | 0.7929 | |||||
4 | 0.9482 | 0.7316 | 0.8779 | 0.7649 | ⋯ | 0.7884 | 0.656 | 0.9197 | |||||||
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋱ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | |
123 | 0.8794 | ⋯ | |||||||||||||
124 | 0.9892 | 0.8918 | 0.8295 | 0.6683 | 0.6479 | ⋯ | 0.7828 | 0.9091 | 0.9966 | 0.6415 | |||||
125 | 0.7946 | ⋯ | 0.729 | 0.9576 | 0.6766 | ||||||||||
126 | 0.9059 | 0.8707 | ⋯ | 0.9899 | 0.9893 | 0.7788 | 0.7175 | 0.6775 | |||||||
127 | 0.9523 | 0.6485 | 0.9866 | 0.8537 | 0.8772 | ⋯ |
coo3D
0 | 1 | 2 | val | |
---|---|---|---|---|
0 | 0 | 0 | 0 | 1 |
1 | 0 | 1 | 0 | 2 |
2 | 0 | 1 | 1 | 3 |
3 | 1 | 0 | 0 | 4 |
4 | 1 | 1 | 0 | 5 |
5 | 1 | 1 | 1 | 6 |
scalar
<tf.Tensor: shape=(), dtype=int32, numpy=5>
Specify title, row and/or column labels to help the reader quickly understand what they are looking at.
dense = tf.random.uniform(shape=(64, 32)).numpy()
dense[dense < 0.3] = 0
tensor = tf.sparse.from_dense(tf.convert_to_tensor(dense))
obs_labels = [f"observation {i}" for i in range(tensor.shape[0])] # list of labels
feature_labels = {i: f"feature {i+1}" for i in range(tensor.shape[1])} # map index to label works too
mdisplay(tensor, row_labels=obs_labels, col_labels=feature_labels, title="Random Dataset")
feature 1 | feature 2 | feature 3 | feature 4 | feature 5 | feature 6 | feature 7 | feature 26 | feature 27 | feature 28 | feature 29 | feature 30 | feature 31 | feature 32 | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
observation 0 | 0.5975 | 0.3031 | 0.9977 | 0.8083 | 0.4171 | 0.6105 | ⋯ | 0.7231 | 0.3532 | 0.3522 | 0.8834 | 0.9973 | |||
observation 1 | 0.3844 | 0.5702 | 0.339 | 0.5319 | 0.6415 | 0.5032 | ⋯ | 0.8356 | 0.4936 | 0.9332 | 0.9475 | 0.4226 | 0.6899 | ||
observation 2 | 0.5964 | 0.5283 | 0.3982 | 0.3899 | ⋯ | 0.7932 | 0.5676 | 0.9334 | 0.9149 | 0.7552 | |||||
observation 3 | 0.5233 | 0.5205 | 0.5863 | 0.9982 | ⋯ | 0.6952 | 0.4436 | 0.7584 | |||||||
observation 4 | 0.6105 | 0.957 | 0.6381 | 0.685 | ⋯ | 0.7776 | 0.3006 | 0.3044 | 0.9721 | 0.3244 | |||||
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋱ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | |
observation 59 | 0.7909 | 0.8313 | 0.8369 | 0.9005 | ⋯ | 0.6093 | 0.4884 | 0.4428 | 0.8436 | 0.5978 | |||||
observation 60 | 0.3902 | 0.667 | 0.4358 | 0.3827 | 0.5785 | ⋯ | 0.9549 | 0.6173 | 0.5166 | ||||||
observation 61 | 0.7613 | 0.5393 | 0.6266 | 0.9674 | 0.5147 | ⋯ | 0.674 | 0.4118 | 0.6996 | 0.7329 | 0.8275 | 0.6695 | 0.7466 | ||
observation 62 | 0.9173 | 0.7029 | 0.7995 | 0.4793 | 0.3444 | 0.4601 | ⋯ | 0.4184 | 0.3094 | 0.5578 | 0.9082 | 0.6766 | 0.7963 | ||
observation 63 | 0.7321 | 0.4044 | 0.7396 | 0.5492 | 0.7415 | 0.7327 | ⋯ | 0.3194 | 0.3959 | 0.9889 | 0.5187 | 0.8223 | 0.6995 |
mdisplay(rand2D_sparse, "latex")
mprint(rand2D_sparse)
<128×64, 3304 '<dtype: 'float32'>' elements, tf.SparseTensor> 0 1 2 3 60 61 62 63 ┌ ┐ 0 │ 0.648 ... 0.8016 │ 1 │ 0.6572 ... 0.8348 0.6638 0.735 │ 2 │ ... 0.8845 0.6323 │ 3 │ 0.7025 0.8244 0.9619 ... 0.7132 0.6676 0.7929 │ 4 │ 0.9482 0.7316 ... 0.656 0.9197 │ │ : : : : ... : : : : │ 123 │ 0.8794 ... │ 124 │ 0.9892 0.8918 0.8262 ... 0.9966 0.6415 │ 125 │ 0.7946 ... 0.6766 │ 126 │ 0.9059 0.8707 ... 0.9893 0.7788 0.7175 0.6775 │ 127 │ 0.9523 0.6485 0.9578 ... │ └ ┘
import matrepr
matrepr.params.max_rows = 10
matrepr.params.max_cols = 7
matrepr.params.num_after_dots = 0
rand2D_sparse
0 | 1 | 2 | 3 | 4 | 5 | ||
---|---|---|---|---|---|---|---|
0 | 0.648 | ⋯ | |||||
1 | 0.8406 | ⋯ | |||||
2 | 0.6736 | ⋯ | |||||
3 | 0.7025 | 0.8244 | 0.7593 | 0.9223 | ⋯ | ||
4 | 0.9482 | 0.7316 | 0.8779 | 0.7649 | ⋯ | ||
5 | 0.89 | 0.6193 | ⋯ | ||||
6 | 0.9639 | 0.7124 | ⋯ | ||||
7 | 0.9725 | ⋯ | |||||
8 | 0.9652 | 0.7869 | 0.9834 | 0.7422 | 0.776 | ⋯ | |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋱ |