tf.transpose
Stay organized with collections
Save and categorize content based on your preferences.
Transposes a
, where a
is a Tensor.
tf.transpose(
a, perm=None, conjugate=False, name='transpose'
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
Permutes the dimensions according to the value of perm
.
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.
If conjugate is True
and a.dtype
is either complex64
or complex128
then the values of a
are conjugated and transposed.
For example:
x = tf.constant([[1, 2, 3], [4, 5, 6]])
tf.transpose(x)
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 4],
[2, 5],
[3, 6]], dtype=int32)>
Equivalently, you could call tf.transpose(x, perm=[1, 0])
.
If x
is complex, setting conjugate=True gives the conjugate transpose:
x = tf.constant([[1 + 1j, 2 + 2j, 3 + 3j],
[4 + 4j, 5 + 5j, 6 + 6j]])
tf.transpose(x, conjugate=True)
<tf.Tensor: shape=(3, 2), dtype=complex128, numpy=
array([[1.-1.j, 4.-4.j],
[2.-2.j, 5.-5.j],
[3.-3.j, 6.-6.j]])>
'perm' is more useful for n-dimensional tensors where n > 2:
x = tf.constant([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
As above, simply calling tf.transpose
will default to perm=[2,1,0]
.
To take the transpose of the matrices in dimension-0 (such as when you are
transposing matrices where 0 is the batch dimension), you would set
perm=[0,2,1]
.
tf.transpose(x, perm=[0, 2, 1])
<tf.Tensor: shape=(2, 3, 2), dtype=int32, numpy=
array([[[ 1, 4],
[ 2, 5],
[ 3, 6]],
[[ 7, 10],
[ 8, 11],
[ 9, 12]]], dtype=int32)>
Args |
a
|
A Tensor .
|
perm
|
A permutation of the dimensions of a . This should be a vector.
|
conjugate
|
Optional bool. Setting it to True is mathematically equivalent
to tf.math.conj(tf.transpose(input)).
|
name
|
A name for the operation (optional).
|
Returns |
A transposed Tensor .
|
In numpy
transposes are memory-efficient constant time operations as they
simply return a new view of the same data with adjusted strides
.
TensorFlow does not support strides, so transpose
returns a new tensor with
the items permuted.
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 2024-04-26 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 2024-04-26 UTC."],[],[],null,["# tf.transpose\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/array_ops.py#L1792-L1870) |\n\nTransposes `a`, where `a` is a Tensor. \n\n tf.transpose(\n a, perm=None, conjugate=False, name='transpose'\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Matrix approximation with Core APIs](https://www.tensorflow.org/guide/core/matrix_core) - [Effective Tensorflow 2](https://www.tensorflow.org/guide/effective_tf2) - [Better performance with tf.function](https://www.tensorflow.org/guide/function) - [Advanced automatic differentiation](https://www.tensorflow.org/guide/advanced_autodiff) - [TensorFlow basics](https://www.tensorflow.org/guide/basics) | - [Scalable model compression](https://www.tensorflow.org/tutorials/optimization/compression) - [Time series forecasting](https://www.tensorflow.org/tutorials/structured_data/time_series) - [Client-efficient large-model federated learning via \\`federated_select\\` and sparse aggregation](https://www.tensorflow.org/federated/tutorials/sparse_federated_learning) - [Quantum data](https://www.tensorflow.org/quantum/tutorials/quantum_data) - [Neural machine translation with a Transformer and Keras](https://www.tensorflow.org/text/tutorials/transformer) |\n\nPermutes the dimensions according to the value of `perm`.\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\nIf conjugate is `True` and `a.dtype` is either `complex64` or `complex128`\nthen the values of `a` are conjugated and transposed.\n\n#### For example:\n\n x = tf.constant([[1, 2, 3], [4, 5, 6]])\n tf.transpose(x)\n \u003ctf.Tensor: shape=(3, 2), dtype=int32, numpy=\n array([[1, 4],\n [2, 5],\n [3, 6]], dtype=int32)\u003e\n\nEquivalently, you could call `tf.transpose(x, perm=[1, 0])`.\n\nIf `x` is complex, setting conjugate=True gives the conjugate transpose: \n\n x = tf.constant([[1 + 1j, 2 + 2j, 3 + 3j],\n [4 + 4j, 5 + 5j, 6 + 6j]])\n tf.transpose(x, conjugate=True)\n \u003ctf.Tensor: shape=(3, 2), dtype=complex128, numpy=\n array([[1.-1.j, 4.-4.j],\n [2.-2.j, 5.-5.j],\n [3.-3.j, 6.-6.j]])\u003e\n\n'perm' is more useful for n-dimensional tensors where n \\\u003e 2: \n\n x = tf.constant([[[ 1, 2, 3],\n [ 4, 5, 6]],\n [[ 7, 8, 9],\n [10, 11, 12]]])\n\nAs above, simply calling [`tf.transpose`](../tf/transpose) will default to `perm=[2,1,0]`.\n\nTo take the transpose of the matrices in dimension-0 (such as when you are\ntransposing matrices where 0 is the batch dimension), you would set\n`perm=[0,2,1]`. \n\n tf.transpose(x, perm=[0, 2, 1])\n \u003ctf.Tensor: shape=(2, 3, 2), dtype=int32, numpy=\n array([[[ 1, 4],\n [ 2, 5],\n [ 3, 6]],\n [[ 7, 10],\n [ 8, 11],\n [ 9, 12]]], dtype=int32)\u003e\n\n| **Note:** This has a shorthand [`linalg.matrix_transpose`](../tf/linalg/matrix_transpose)):\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|--------------------------------------------------------------------------------------------------------|\n| `a` | A `Tensor`. |\n| `perm` | A permutation of the dimensions of `a`. This should be a vector. |\n| `conjugate` | Optional bool. Setting it to `True` is mathematically equivalent to tf.math.conj(tf.transpose(input)). |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A transposed `Tensor`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nnumpy compatibility\n-------------------\n\n\u003cbr /\u003e\n\nIn `numpy` transposes are memory-efficient constant time operations as they\nsimply return a new view of the same data with adjusted `strides`.\n\nTensorFlow does not support strides, so `transpose` returns a new tensor with\nthe items permuted.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e"]]