MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / p_matrix_norm

Function p_matrix_norm

python/paddle/tensor/linalg.py:879–1052  ·  view source on GitHub ↗

Calculate the p-order matrix norm for certain dimension of Tensor `input`. Args: input (Variable): Tensor, data type float32, float64. porder (int|float,str): p in ['fro', 'nuc', ±1, ±2, ±inf] Default 1. axis (list): Two dimensions. keepdim (

(
        input: Tensor,
        porder: float | _POrder = 1.0,
        axis: int | list[int] | tuple[int, int] = axis,
        keepdim: bool = False,
        name: str | None = None,
    )

Source from the content-addressed store, hash-verified

877 return out
878
879 def p_matrix_norm(
880 input: Tensor,
881 porder: float | _POrder = 1.0,
882 axis: int | list[int] | tuple[int, int] = axis,
883 keepdim: bool = False,
884 name: str | None = None,
885 ) -> Tensor:
886 """
887 Calculate the p-order matrix norm for certain dimension of Tensor `input`.
888 Args:
889 input (Variable): Tensor, data type float32, float64.
890 porder (int|float,str): p in ['fro', 'nuc', ±1, ±2, ±inf] Default 1.
891 axis (list): Two dimensions.
892 keepdim (bool, optional): Whether keep the dimensions as the `input`, Default False.
893 name (str, optional): The default value is None. Normally there is no need for
894 user to set this property. For more information, please refer to :ref:`api_guide_Name`.
895 """
896
897 perm = _backshift_permutation(axis[0], axis[1], len(input.shape))
898 inv_perm = _inverse_permutation(perm)
899
900 if in_dynamic_or_pir_mode():
901 abs_ord = abs(porder)
902
903 max_min = _C_ops.max if porder > 0.0 else _C_ops.min
904
905 if abs_ord == 2.0:
906 transpose_out = _C_ops.transpose(input, perm)
907 u, s, vh = _C_ops.svd(transpose_out, False)
908 result = max_min(s, -1, keepdim)
909 if keepdim:
910 result = _C_ops.transpose(
911 _C_ops.unsqueeze(result, -1), inv_perm
912 )
913 return result
914 else: # 1,-1,inf,-inf
915 rank = len(x.shape)
916 dim0, dim1 = (d % rank for d in axis)
917 if abs_ord == np.float64("inf"):
918 dim0, dim1 = dim1, dim0
919 if not keepdim and (dim0 < dim1):
920 dim1 -= 1
921 return max_min(
922 vector_norm(input, 1.0, axis=dim0, keepdim=keepdim),
923 dim1,
924 keepdim,
925 )
926
927 check_variable_and_dtype(
928 input,
929 'input',
930 ['float16', 'uint16', 'float32', 'float64'],
931 'p_matrix_norm',
932 )
933
934 block = LayerHelper('p_matrix_norm', **locals())
935 out = block.create_variable_for_type_inference(
936 dtype=block.input_dtype()

Callers 1

matrix_normFunction · 0.85

Calls 13

input_dtypeMethod · 0.95
append_opMethod · 0.95
_backshift_permutationFunction · 0.85
_inverse_permutationFunction · 0.85
in_dynamic_or_pir_modeFunction · 0.85
vector_normFunction · 0.85
check_variable_and_dtypeFunction · 0.85
LayerHelperClass · 0.85
_get_reduce_axisFunction · 0.85
unsqueezeMethod · 0.80
absFunction · 0.50

Tested by

no test coverage detected