tf.sparse.transpose
Stay organized with collections
Save and categorize content based on your preferences.
Transposes a SparseTensor
.
tf.sparse.transpose(
sp_input, perm=None, name=None
)
Permutes the dimensions according to the value of perm
. This is the sparse
version of tf.transpose
.
The returned tensor's dimension i
will correspond to the input dimension
perm[i]
. If perm
is not given, it is set to (n-1...0), where n is the rank
of the input tensor. Hence, by default, this operation performs a regular
matrix transpose on 2-D input Tensors.
For example:
x = tf.SparseTensor(indices=[[0, 1], [0, 3], [2, 3], [3, 1]],
values=[1.1, 2.2, 3.3, 4.4],
dense_shape=[4, 5])
print('x =', tf.sparse.to_dense(x))
x = tf.Tensor(
[[0. 1.1 0. 2.2 0. ]
[0. 0. 0. 0. 0. ]
[0. 0. 0. 3.3 0. ]
[0. 4.4 0. 0. 0. ]], shape=(4, 5), dtype=float32)
x_transpose = tf.sparse.transpose(x)
print('x_transpose =', tf.sparse.to_dense(x_transpose))
x_transpose = tf.Tensor(
[[0. 0. 0. 0. ]
[1.1 0. 0. 4.4]
[0. 0. 0. 0. ]
[2.2 0. 3.3 0. ]
[0. 0. 0. 0. ]], shape=(5, 4), dtype=float32)
Equivalently, you could call tf.sparse.transpose(x, perm=[1, 0])
. The
perm
argument is more useful for n-dimensional tensors where n > 2.
x = tf.SparseTensor(indices=[[0, 0, 1], [0, 0, 3], [1, 2, 3], [1, 3, 1]],
values=[1.1, 2.2, 3.3, 4.4],
dense_shape=[2, 4, 5])
print('x =', tf.sparse.to_dense(x))
x = tf.Tensor(
[[[0. 1.1 0. 2.2 0. ]
[0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]]
[[0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]
[0. 0. 0. 3.3 0. ]
[0. 4.4 0. 0. 0. ]]], shape=(2, 4, 5), dtype=float32)
As above, simply calling tf.sparse.transpose
will default to perm=[2,1,0]
.
To take the transpose of a batch of sparse matrices, where 0 is the batch
dimension, you would set perm=[0,2,1]
.
x_transpose = tf.sparse.transpose(x, perm=[0, 2, 1])
print('x_transpose =', tf.sparse.to_dense(x_transpose))
x_transpose = tf.Tensor(
[[[0. 0. 0. 0. ]
[1.1 0. 0. 0. ]
[0. 0. 0. 0. ]
[2.2 0. 0. 0. ]
[0. 0. 0. 0. ]]
[[0. 0. 0. 0. ]
[0. 0. 0. 4.4]
[0. 0. 0. 0. ]
[0. 0. 3.3 0. ]
[0. 0. 0. 0. ]]], shape=(2, 5, 4), dtype=float32)
Args |
sp_input
|
The input SparseTensor .
|
perm
|
A permutation vector of the dimensions of sp_input .
|
name
|
A name prefix for the returned tensors (optional).
|
Returns |
A transposed SparseTensor .
|
Raises |
TypeError
|
If sp_input is not a SparseTensor .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2023-10-06 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-10-06 UTC."],[],[],null,["# tf.sparse.transpose\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/sparse_ops.py#L2813-L2918) |\n\nTransposes a `SparseTensor`.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.sparse.transpose`](https://www.tensorflow.org/api_docs/python/tf/sparse/transpose), [`tf.compat.v1.sparse_transpose`](https://www.tensorflow.org/api_docs/python/tf/sparse/transpose)\n\n\u003cbr /\u003e\n\n tf.sparse.transpose(\n sp_input, perm=None, name=None\n )\n\nPermutes the dimensions according to the value of `perm`. This is the sparse\nversion of [`tf.transpose`](../../tf/transpose).\n\nThe returned tensor's dimension `i` will correspond to the input dimension\n`perm[i]`. If `perm` is not given, it is set to (n-1...0), where n is the rank\nof the input tensor. Hence, by default, this operation performs a regular\nmatrix transpose on 2-D input Tensors.\n\n#### For example:\n\n x = tf.SparseTensor(indices=[[0, 1], [0, 3], [2, 3], [3, 1]],\n values=[1.1, 2.2, 3.3, 4.4],\n dense_shape=[4, 5])\n print('x =', tf.sparse.to_dense(x))\n x = tf.Tensor(\n [[0. 1.1 0. 2.2 0. ]\n [0. 0. 0. 0. 0. ]\n [0. 0. 0. 3.3 0. ]\n [0. 4.4 0. 0. 0. ]], shape=(4, 5), dtype=float32)\n\n x_transpose = tf.sparse.transpose(x)\n print('x_transpose =', tf.sparse.to_dense(x_transpose))\n x_transpose = tf.Tensor(\n [[0. 0. 0. 0. ]\n [1.1 0. 0. 4.4]\n [0. 0. 0. 0. ]\n [2.2 0. 3.3 0. ]\n [0. 0. 0. 0. ]], shape=(5, 4), dtype=float32)\n\nEquivalently, you could call `tf.sparse.transpose(x, perm=[1, 0])`. The\n`perm` argument is more useful for n-dimensional tensors where n \\\u003e 2. \n\n x = tf.SparseTensor(indices=[[0, 0, 1], [0, 0, 3], [1, 2, 3], [1, 3, 1]],\n values=[1.1, 2.2, 3.3, 4.4],\n dense_shape=[2, 4, 5])\n print('x =', tf.sparse.to_dense(x))\n x = tf.Tensor(\n [[[0. 1.1 0. 2.2 0. ]\n [0. 0. 0. 0. 0. ]\n [0. 0. 0. 0. 0. ]\n [0. 0. 0. 0. 0. ]]\n [[0. 0. 0. 0. 0. ]\n [0. 0. 0. 0. 0. ]\n [0. 0. 0. 3.3 0. ]\n [0. 4.4 0. 0. 0. ]]], shape=(2, 4, 5), dtype=float32)\n\nAs above, simply calling [`tf.sparse.transpose`](../../tf/sparse/transpose) will default to `perm=[2,1,0]`.\n\nTo take the transpose of a batch of sparse matrices, where 0 is the batch\ndimension, you would set `perm=[0,2,1]`. \n\n x_transpose = tf.sparse.transpose(x, perm=[0, 2, 1])\n print('x_transpose =', tf.sparse.to_dense(x_transpose))\n x_transpose = tf.Tensor(\n [[[0. 0. 0. 0. ]\n [1.1 0. 0. 0. ]\n [0. 0. 0. 0. ]\n [2.2 0. 0. 0. ]\n [0. 0. 0. 0. ]]\n [[0. 0. 0. 0. ]\n [0. 0. 0. 4.4]\n [0. 0. 0. 0. ]\n [0. 0. 3.3 0. ]\n [0. 0. 0. 0. ]]], shape=(2, 5, 4), dtype=float32)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------|-------------------------------------------------------|\n| `sp_input` | The input `SparseTensor`. |\n| `perm` | A permutation vector of the dimensions of `sp_input`. |\n| `name` | A name prefix for the returned tensors (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A transposed `SparseTensor`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|----------------------------------------|\n| `TypeError` | If `sp_input` is not a `SparseTensor`. |\n\n\u003cbr /\u003e"]]