tf.linalg.matrix_transpose
Stay organized with collections
Save and categorize content based on your preferences.
Transposes last two dimensions of tensor a
.
tf.linalg.matrix_transpose(
a, name='matrix_transpose', conjugate=False
)
Used in the notebooks
For example:
x = tf.constant([[1, 2, 3], [4, 5, 6]])
tf.linalg.matrix_transpose(x) # [[1, 4],
# [2, 5],
# [3, 6]]
x = tf.constant([[1 + 1j, 2 + 2j, 3 + 3j],
[4 + 4j, 5 + 5j, 6 + 6j]])
tf.linalg.matrix_transpose(x, conjugate=True) # [[1 - 1j, 4 - 4j],
# [2 - 2j, 5 - 5j],
# [3 - 3j, 6 - 6j]]
# Matrix with two batch dimensions.
# x.shape is [1, 2, 3, 4]
# tf.linalg.matrix_transpose(x) is shape [1, 2, 4, 3]
Note that tf.matmul
provides kwargs allowing for transpose of arguments.
This is done with minimal cost, and is preferable to using this function. E.g.
# Good! Transpose is taken at minimal additional cost.
tf.matmul(matrix, b, transpose_b=True)
# Inefficient!
tf.matmul(matrix, tf.linalg.matrix_transpose(b))
Args |
a
|
A Tensor with rank >= 2 .
|
name
|
A name for the operation (optional).
|
conjugate
|
Optional bool. Setting it to True is mathematically equivalent
to tf.math.conj(tf.linalg.matrix_transpose(input)).
|
Returns |
A transposed batch matrix Tensor .
|
Raises |
ValueError
|
If a is determined statically to have rank < 2 .
|
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, linalg.matrix_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.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.linalg.matrix_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#L1962-L2039) |\n\nTransposes last two dimensions of tensor `a`.\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.matrix_transpose`](https://www.tensorflow.org/api_docs/python/tf/linalg/matrix_transpose)\n\n\u003cbr /\u003e\n\n tf.linalg.matrix_transpose(\n a, name='matrix_transpose', conjugate=False\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [TensorFlow Probability Case Study: Covariance Estimation](https://www.tensorflow.org/probability/examples/TensorFlow_Probability_Case_Study_Covariance_Estimation) |\n\n#### For example:\n\n x = tf.constant([[1, 2, 3], [4, 5, 6]])\n tf.linalg.matrix_transpose(x) # [[1, 4],\n # [2, 5],\n # [3, 6]]\n\n x = tf.constant([[1 + 1j, 2 + 2j, 3 + 3j],\n [4 + 4j, 5 + 5j, 6 + 6j]])\n tf.linalg.matrix_transpose(x, conjugate=True) # [[1 - 1j, 4 - 4j],\n # [2 - 2j, 5 - 5j],\n # [3 - 3j, 6 - 6j]]\n\n # Matrix with two batch dimensions.\n # x.shape is [1, 2, 3, 4]\n # tf.linalg.matrix_transpose(x) is shape [1, 2, 4, 3]\n\nNote that [`tf.matmul`](../../tf/linalg/matmul) provides kwargs allowing for transpose of arguments.\nThis is done with minimal cost, and is preferable to using this function. E.g. \n\n # Good! Transpose is taken at minimal additional cost.\n tf.matmul(matrix, b, transpose_b=True)\n\n # Inefficient!\n tf.matmul(matrix, tf.linalg.matrix_transpose(b))\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|----------------------------------------------------------------------------------------------------------------------|\n| `a` | A `Tensor` with `rank \u003e= 2`. |\n| `name` | A name for the operation (optional). |\n| `conjugate` | Optional bool. Setting it to `True` is mathematically equivalent to tf.math.conj(tf.linalg.matrix_transpose(input)). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A transposed batch matrix `Tensor`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-----------------------------------------------------|\n| `ValueError` | If `a` is determined statically to have `rank \u003c 2`. |\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, [`linalg.matrix_transpose`](../../tf/linalg/matrix_transpose) returns a new\ntensor with the items permuted.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e"]]