chainer.functions.diagonal

chainer.functions.diagonal(x, offset=0, axis1=0, axis2=1)[source]

Take diagonal

Axes other than axis1 and axis2 are regarded as batch dimensions.

Parameters:
  • x (Variable or numpy.ndarray or cupy.ndarray) – A variable to be sliced.
  • offset (int) – Offset from the principal diagonal. An upper diagonal matrix can have nonzero diagonals with nonnegative offsets.
  • axis1 (int) – First axis (that has row indices) of matrix
  • axis2 (int) – Second axis (that has column indices) of matrix
Returns:

(Batched) diagonal vectors

Return type:

Variable

Example

>>> x = chainer.Variable(np.arange(9).reshape(3, 3).astype(np.float32))
>>> x
variable([[0., 1., 2.],
          [3., 4., 5.],
          [6., 7., 8.]])
>>> chainer.functions.diagonal(x, offset=1)
variable([1., 5.])