tf.keras.ops.diagonal
Stay organized with collections
Save and categorize content based on your preferences.
Return specified diagonals.
tf.keras.ops.diagonal(
x, offset=0, axis1=0, axis2=1
)
If x
is 2-D, returns the diagonal of x
with the given offset, i.e., the
collection of elements of the form x[i, i+offset]
.
If x
has more than two dimensions, the axes specified by axis1
and axis2
are used to determine the 2-D sub-array whose diagonal
is returned.
The shape of the resulting array can be determined by removing axis1
and axis2
and appending an index to the right equal to the size of
the resulting diagonals.
Args |
x
|
Input tensor.
|
offset
|
Offset of the diagonal from the main diagonal.
Can be positive or negative. Defaults to 0 .(main diagonal).
|
axis1
|
Axis to be used as the first axis of the 2-D sub-arrays.
Defaults to 0 .(first axis).
|
axis2
|
Axis to be used as the second axis of the 2-D sub-arrays.
Defaults to 1 (second axis).
|
Returns |
Tensor of diagonals.
|
Examples:
from keras.src import ops
x = ops.arange(4).reshape((2, 2))
x
array([[0, 1],
[2, 3]])
x.diagonal()
array([0, 3])
x.diagonal(1)
array([1])
x = ops.arange(8).reshape((2, 2, 2))
x
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
x.diagonal(0, 0, 1)
array([[0, 6],
[1, 7]])
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-06-07 UTC.
[null,null,["Last updated 2024-06-07 UTC."],[],[],null,["# tf.keras.ops.diagonal\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/ops/numpy.py#L1819-L1878) |\n\nReturn specified diagonals.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.keras.ops.numpy.diagonal`](https://www.tensorflow.org/api_docs/python/tf/keras/ops/diagonal)\n\n\u003cbr /\u003e\n\n tf.keras.ops.diagonal(\n x, offset=0, axis1=0, axis2=1\n )\n\nIf `x` is 2-D, returns the diagonal of `x` with the given offset, i.e., the\ncollection of elements of the form `x[i, i+offset]`.\n\nIf `x` has more than two dimensions, the axes specified by `axis1`\nand `axis2` are used to determine the 2-D sub-array whose diagonal\nis returned.\n\nThe shape of the resulting array can be determined by removing `axis1`\nand `axis2` and appending an index to the right equal to the size of\nthe resulting diagonals.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|--------------------------------------------------------------------------------------------------------------|\n| `x` | Input tensor. |\n| `offset` | Offset of the diagonal from the main diagonal. Can be positive or negative. Defaults to `0`.(main diagonal). |\n| `axis1` | Axis to be used as the first axis of the 2-D sub-arrays. Defaults to `0`.(first axis). |\n| `axis2` | Axis to be used as the second axis of the 2-D sub-arrays. Defaults to `1` (second axis). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Tensor of diagonals. ||\n\n\u003cbr /\u003e\n\n#### Examples:\n\n from keras.src import ops\n x = ops.arange(4).reshape((2, 2))\n x\n array([[0, 1],\n [2, 3]])\n x.diagonal()\n array([0, 3])\n x.diagonal(1)\n array([1])\n\n x = ops.arange(8).reshape((2, 2, 2))\n x\n array([[[0, 1],\n [2, 3]],\n [[4, 5],\n [6, 7]]])\n x.diagonal(0, 0, 1)\n array([[0, 6],\n [1, 7]])"]]