Transposes last two dimensions of tensor `a`. For example: ```python 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],
(a, name="matrix_transpose", conjugate=False)
| 1865 | v1=["linalg.transpose", "linalg.matrix_transpose", "matrix_transpose"]) |
| 1866 | @deprecation.deprecated_endpoints("matrix_transpose", "linalg.transpose") |
| 1867 | def matrix_transpose(a, name="matrix_transpose", conjugate=False): |
| 1868 | """Transposes last two dimensions of tensor `a`. |
| 1869 | |
| 1870 | For example: |
| 1871 | |
| 1872 | ```python |
| 1873 | x = tf.constant([[1, 2, 3], [4, 5, 6]]) |
| 1874 | tf.linalg.matrix_transpose(x) # [[1, 4], |
| 1875 | # [2, 5], |
| 1876 | # [3, 6]] |
| 1877 | |
| 1878 | x = tf.constant([[1 + 1j, 2 + 2j, 3 + 3j], |
| 1879 | [4 + 4j, 5 + 5j, 6 + 6j]]) |
| 1880 | tf.linalg.matrix_transpose(x, conjugate=True) # [[1 - 1j, 4 - 4j], |
| 1881 | # [2 - 2j, 5 - 5j], |
| 1882 | # [3 - 3j, 6 - 6j]] |
| 1883 | |
| 1884 | # Matrix with two batch dimensions. |
| 1885 | # x.shape is [1, 2, 3, 4] |
| 1886 | # tf.linalg.matrix_transpose(x) is shape [1, 2, 4, 3] |
| 1887 | ``` |
| 1888 | |
| 1889 | Note that `tf.matmul` provides kwargs allowing for transpose of arguments. |
| 1890 | This is done with minimal cost, and is preferable to using this function. E.g. |
| 1891 | |
| 1892 | ```python |
| 1893 | # Good! Transpose is taken at minimal additional cost. |
| 1894 | tf.matmul(matrix, b, transpose_b=True) |
| 1895 | |
| 1896 | # Inefficient! |
| 1897 | tf.matmul(matrix, tf.linalg.matrix_transpose(b)) |
| 1898 | ``` |
| 1899 | |
| 1900 | @compatibility(numpy) |
| 1901 | In `numpy` transposes are memory-efficient constant time operations as they |
| 1902 | simply return a new view of the same data with adjusted `strides`. |
| 1903 | |
| 1904 | TensorFlow does not support strides, `linalg.matrix_transpose` returns a new |
| 1905 | tensor with the items permuted. |
| 1906 | @end_compatibility |
| 1907 | |
| 1908 | Args: |
| 1909 | a: A `Tensor` with `rank >= 2`. |
| 1910 | name: A name for the operation (optional). |
| 1911 | conjugate: Optional bool. Setting it to `True` is mathematically equivalent |
| 1912 | to tf.math.conj(tf.linalg.matrix_transpose(input)). |
| 1913 | |
| 1914 | Returns: |
| 1915 | A transposed batch matrix `Tensor`. |
| 1916 | |
| 1917 | Raises: |
| 1918 | ValueError: If `a` is determined statically to have `rank < 2`. |
| 1919 | """ |
| 1920 | with ops.name_scope(name, values=[a]): |
| 1921 | a = ops.convert_to_tensor(a, name="a") |
| 1922 | |
| 1923 | # If we know the number of dimensions (statically), we can do two things: |
| 1924 | # 1. Check that `a` is a (batch) matrix. |