View source on GitHub |
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.
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]])