parameters: (self, offset=0, axis1=0, axis2=1)
return_statement: return res.astype(dtype)
docstring: Return specified diagonals.
  
          Args:
              offset (int, optional): Offset of the diagonal from the main diagonal.
                  Can be positive or negative. Defaults to main diagonal.
              axis1 (int, optional): Axis to be used as the first axis of the 2-D
                  sub-arrays from which the diagonals should be taken. Defaults to
                  first axis (0).
              axis2 (int, optional): Axis to be used as the second axis of the 2-D
                  sub-arrays from which the diagonals should be taken. Defaults to
                  second axis.
  
          Returns:
              Tensor, if Tensor is 2-D, return a 1-D Tensor containing the diagonal.
  
          Raises:
              ValueError: if the input tensor has less than two dimensions.
  
          Supported Platforms:
              ``Ascend`` ``GPU`` ``CPU``
  
          See also:
              :func:`mindspore.Tensor.trace`: Return the sum along diagonals of the tensor.
  
          Examples:
              >>> import numpy as np
              >>> from mindspore import Tensor
              >>> a = Tensor(np.arange(4).reshape(2, 2))
              >>> print(a)
              [[0 1]
              [2 3]]
              >>> output = a.diagonal()
              >>> print(output)
              [0 3]
docstring_summary: Return specified diagonals.
docstring_tokens: Return specified diagonals .
function: def diagonal(self, offset=0, axis1=0, axis2=1):
          """
          Return specified diagonals.
  
          Args:
              offset (int, optional): Offset of the diagonal from the main diagonal.
                  Can be positive or negative. Defaults to main diagonal.
              axis1 (int, optional): Axis to be used as the first axis of the 2-D
                  sub-arrays from which the diagonals should be taken. Defaults to
                  first axis (0).
              axis2 (int, optional): Axis to be used as the second axis of the 2-D
                  sub-arrays from which the diagonals should be taken. Defaults to
                  second axis.
  
          Returns:
              Tensor, if Tensor is 2-D, return a 1-D Tensor containing the diagonal.
  
          Raises:
              ValueError: if the input tensor has less than two dimensions.
  
          Supported Platforms:
              ``Ascend`` ``GPU`` ``CPU``
  
          See also:
              :func:`mindspore.Tensor.trace`: Return the sum along diagonals of the tensor.
  
          Examples:
              >>> import numpy as np
              >>> from mindspore import Tensor
              >>> a = Tensor(np.arange(4).reshape(2, 2))
              >>> print(a)
              [[0 1]
              [2 3]]
              >>> output = a.diagonal()
              >>> print(output)
              [0 3]
          """
          ndim = self.ndim
          if ndim < 2:
              raise ValueError("For 'Tensor.diagonal', the original tensor requires at least two dimensions, "
                               "but got {}.".format(ndim))
          dtype = self.dtype
  
          axes = validator.check_axis_valid((axis1, axis2), ndim)
          perm = ()
          for i in range(ndim):
              if i not in axes:
                  perm += (i,)
          perm += axes
          a = self.transpose(perm)
  
          shape = a.shape
          n, m = shape[-2:]
  
          e = tensor_operator_registry.get('eye')(n, m, dtype)
          if offset >= m or offset <= -n:
              e = tensor_operator_registry.get('fill')(dtype, (n, m), 0)
          elif offset != 0:
              e = e.astype(mstype.float32)
              if offset > 0:
                  e_left = tensor_operator_registry.get('fill')(dtype, (n, offset), 0)
                  e_right = e[..., 0:m-offset:1]
                  e = tensor_operator_registry.get('concatenate')(1)((e_left, e_right)).astype(dtype)
              elif offset < 0:
                  e_upper = tensor_operator_registry.get('fill')(dtype, (-offset, m), 0)
                  e_lower = e[0:n+offset:1, ...]
                  e = tensor_operator_registry.get('concatenate')(0)((e_upper, e_lower)).astype(dtype)
          e = tensor_operator_registry.get('broadcast_to')(shape)(e)
  
          prod = tensor_operator_registry.get('__mul__')(a, e)
          res = tensor_operator_registry.get('reduce_sum')(prod.astype(mstype.float32), -1)
  
          begin = ()
          for i in range(ndim-2):
              begin += (0,)
          last_dim_begin = max(0, -offset)
          begin += (last_dim_begin,)
          size = res.shape[:-1]
          last_dim_end = min(
              shape[-2], max(0, shape[-1] - offset)) - last_dim_begin
          if last_dim_end <= 0:
              return Tensor([])
          size += (last_dim_end,)
          res = tensor_operator_registry.get('tensor_slice')(res, begin, size)
          return res.astype(dtype)
function_tokens: def diagonal ( self , offset = 0 , axis1 = 0 , axis2 = 1 ) : ndim = self . ndim if ndim < 2 : raise ValueError ( "For 'Tensor.diagonal', the original tensor requires at least two dimensions, " "but got {}." . format ( ndim ) ) dtype = self . dtype axes = validator . check_axis_valid ( ( axis1 , axis2 ) , ndim ) perm = ( ) for i in range ( ndim ) : if i not in axes : perm += ( i , ) perm += axes a = self . transpose ( perm ) shape = a . shape n , m = shape [ - 2 : ] e = tensor_operator_registry . get ( 'eye' ) ( n , m , dtype ) if offset >= m or offset <= - n : e = tensor_operator_registry . get ( 'fill' ) ( dtype , ( n , m ) , 0 ) elif offset != 0 : e = e . astype ( mstype . float32 ) if offset > 0 : e_left = tensor_operator_registry . get ( 'fill' ) ( dtype , ( n , offset ) , 0 ) e_right = e [ ... , 0 : m - offset : 1 ] e = tensor_operator_registry . get ( 'concatenate' ) ( 1 ) ( ( e_left , e_right ) ) . astype ( dtype ) elif offset < 0 : e_upper = tensor_operator_registry . get ( 'fill' ) ( dtype , ( - offset , m ) , 0 ) e_lower = e [ 0 : n + offset : 1 , ... ] e = tensor_operator_registry . get ( 'concatenate' ) ( 0 ) ( ( e_upper , e_lower ) ) . astype ( dtype ) e = tensor_operator_registry . get ( 'broadcast_to' ) ( shape ) ( e ) prod = tensor_operator_registry . get ( '__mul__' ) ( a , e ) res = tensor_operator_registry . get ( 'reduce_sum' ) ( prod . astype ( mstype . float32 ) , - 1 ) begin = ( ) for i in range ( ndim - 2 ) : begin += ( 0 , ) last_dim_begin = max ( 0 , - offset ) begin += ( last_dim_begin , ) size = res . shape [ : - 1 ] last_dim_end = min ( shape [ - 2 ] , max ( 0 , shape [ - 1 ] - offset ) ) - last_dim_begin if last_dim_end <= 0 : return Tensor ( [ ] ) size += ( last_dim_end , ) res = tensor_operator_registry . get ( 'tensor_slice' ) ( res , begin , size ) return res . astype ( dtype )