tf.keras.ops.diagonal

Return specified diagonals.

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.

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).

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]])