Transposes `a`. Permutes the dimensions according to `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 ma
(a, perm=None, name="transpose", conjugate=False)
| 1770 | |
| 1771 | @tf_export(v1=["transpose"]) |
| 1772 | def transpose(a, perm=None, name="transpose", conjugate=False): |
| 1773 | """Transposes `a`. |
| 1774 | |
| 1775 | Permutes the dimensions according to `perm`. |
| 1776 | |
| 1777 | The returned tensor's dimension i will correspond to the input dimension |
| 1778 | `perm[i]`. If `perm` is not given, it is set to (n-1...0), where n is |
| 1779 | the rank of the input tensor. Hence by default, this operation performs a |
| 1780 | regular matrix transpose on 2-D input Tensors. If conjugate is True and |
| 1781 | `a.dtype` is either `complex64` or `complex128` then the values of `a` |
| 1782 | are conjugated and transposed. |
| 1783 | |
| 1784 | @compatibility(numpy) |
| 1785 | In `numpy` transposes are memory-efficient constant time operations as they |
| 1786 | simply return a new view of the same data with adjusted `strides`. |
| 1787 | |
| 1788 | TensorFlow does not support strides, so `transpose` returns a new tensor with |
| 1789 | the items permuted. |
| 1790 | @end_compatibility |
| 1791 | |
| 1792 | For example: |
| 1793 | |
| 1794 | ```python |
| 1795 | x = tf.constant([[1, 2, 3], [4, 5, 6]]) |
| 1796 | tf.transpose(x) # [[1, 4] |
| 1797 | # [2, 5] |
| 1798 | # [3, 6]] |
| 1799 | |
| 1800 | # Equivalently |
| 1801 | tf.transpose(x, perm=[1, 0]) # [[1, 4] |
| 1802 | # [2, 5] |
| 1803 | # [3, 6]] |
| 1804 | |
| 1805 | # If x is complex, setting conjugate=True gives the conjugate transpose |
| 1806 | x = tf.constant([[1 + 1j, 2 + 2j, 3 + 3j], |
| 1807 | [4 + 4j, 5 + 5j, 6 + 6j]]) |
| 1808 | tf.transpose(x, conjugate=True) # [[1 - 1j, 4 - 4j], |
| 1809 | # [2 - 2j, 5 - 5j], |
| 1810 | # [3 - 3j, 6 - 6j]] |
| 1811 | |
| 1812 | # 'perm' is more useful for n-dimensional tensors, for n > 2 |
| 1813 | x = tf.constant([[[ 1, 2, 3], |
| 1814 | [ 4, 5, 6]], |
| 1815 | [[ 7, 8, 9], |
| 1816 | [10, 11, 12]]]) |
| 1817 | |
| 1818 | # Take the transpose of the matrices in dimension-0 |
| 1819 | # (this common operation has a shorthand `linalg.matrix_transpose`) |
| 1820 | tf.transpose(x, perm=[0, 2, 1]) # [[[1, 4], |
| 1821 | # [2, 5], |
| 1822 | # [3, 6]], |
| 1823 | # [[7, 10], |
| 1824 | # [8, 11], |
| 1825 | # [9, 12]]] |
| 1826 | ``` |
| 1827 | |
| 1828 | Args: |
| 1829 | a: A `Tensor`. |
no test coverage detected