tf.linalg.trace
Stay organized with collections
Save and categorize content based on your preferences.
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, :, :])
For example:
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.
|
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.trace\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/math_ops.py#L3350-L3391) |\n\nCompute the trace of a tensor `x`.\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.trace`](https://www.tensorflow.org/api_docs/python/tf/linalg/trace)\n\n\u003cbr /\u003e\n\n tf.linalg.trace(\n x, name=None\n )\n\n`trace(x)` returns the sum along the main diagonal of each inner-most matrix\nin x. If x is of rank `k` with shape `[I, J, K, ..., L, M, N]`, then output\nis a tensor of rank `k-2` with dimensions `[I, J, K, ..., L]` where\n\n`output[i, j, k, ..., l] = trace(x[i, j, k, ..., l, :, :])`\n\n#### For example:\n\n x = tf.constant([[1, 2], [3, 4]])\n tf.linalg.trace(x) # 5\n\n x = tf.constant([[1, 2, 3],\n [4, 5, 6],\n [7, 8, 9]])\n tf.linalg.trace(x) # 15\n\n x = tf.constant([[[1, 2, 3],\n [4, 5, 6],\n [7, 8, 9]],\n [[-1, -2, -3],\n [-4, -5, -6],\n [-7, -8, -9]]])\n tf.linalg.trace(x) # [15, -15]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------|--------------------------------------|\n| `x` | tensor. |\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| The trace of input tensor. ||\n\n\u003cbr /\u003e"]]