<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/ops/numpy.py#L2739-L2806) |

Return specified diagonals.

#### View aliases


**Main aliases**

[`tf.keras.ops.numpy.diagonal`](https://www.tensorflow.org/api_docs/python/tf/keras/ops/diagonal)

<br />

    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.

<br />

<br />

<br />

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

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| Tensor of diagonals. ||

<br />

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