| View source on GitHub | 
Compute the trace of a tensor x.
tf.linalg.trace(
    x, name=None
)
  trace(x) returns the sum along the main diagonal of each inner-most matrix in x. If x is of rank k with shape [I, J, K, ..., L, M, N], then output is a tensor of rank k-2 with dimensions [I, J, K, ..., L] where
output[i, j, k, ..., l] = trace(x[i, j, k, ..., l, :, :])
x = tf.constant([[1, 2], [3, 4]])
tf.linalg.trace(x)  # 5
x = tf.constant([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])
tf.linalg.trace(x)  # 15
x = tf.constant([[[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]],
                 [[-1, -2, -3],
                  [-4, -5, -6],
                  [-7, -8, -9]]])
tf.linalg.trace(x)  # [15, -15]
  
| Args | |
|---|---|
| x | tensor. | 
| name | A name for the operation (optional). | 
| Returns | |
|---|---|
| The trace of input tensor. | 
    © 2022 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 4.0.
Code samples licensed under the Apache 2.0 License.
    https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/linalg/trace