(l-onnx-doc-LpNormalization)= # LpNormalization (l-onnx-op-lpnormalization-22)= ## LpNormalization - 22 ### Version - **name**: [LpNormalization (GitHub)](https://github.com/onnx/onnx/blob/main/docs/Operators.md#LpNormalization) - **domain**: `main` - **since_version**: `22` - **function**: `False` - **support_level**: `SupportType.COMMON` - **shape inference**: `True` This version of the operator has been available **since version 22**. ### Summary Given a matrix, apply Lp-normalization along the provided axis. The output is computed as: `output = input / Lp_norm(input, axis)`. When the Lp norm is zero (i.e., all elements along the axis are zero), the output is defined to be zero to avoid division by zero. ### Attributes * **axis - INT** (default is `-1`): The axis on which to apply normalization, -1 mean last axis. * **p - INT** (default is `2`): The order of the normalization, only 1 or 2 are supported. ### Inputs - **input** (heterogeneous) - **T**: Input matrix ### Outputs - **output** (heterogeneous) - **T**: Matrix after normalization ### Type Constraints * **T** in ( `tensor(bfloat16)`, `tensor(double)`, `tensor(float)`, `tensor(float16)` ): Constrain input and output types to float tensors. ### Examples #### _l2normalization_axis_0 ```python import numpy as np import onnx node = onnx.helper.make_node( "LpNormalization", inputs=["x"], outputs=["y"], axis=0, p=2 ) x = np.array( [[[1.0, 2.0, 2.0], [3.0, 4.0, 0.0]], [[0.0, 5.0, 5.0], [6.0, 8.0, 0.0]]], dtype=np.float32, ) l2_norm_axis_0 = np.sqrt(np.sum(x**2, axis=0, keepdims=True)) # When norm is 0, output is 0 (0/0 = 0) y = np.where(l2_norm_axis_0 == 0, 0, x / l2_norm_axis_0) expect(node, inputs=[x], outputs=[y], name="test_l2normalization_axis_0") ``` #### _l2normalization_axis_1 ```python import numpy as np import onnx node = onnx.helper.make_node( "LpNormalization", inputs=["x"], outputs=["y"], axis=1, p=2 ) x = np.array([[3.0, 4.0], [6.0, 8.0]], dtype=np.float32) l2_norm_axis_1 = np.sqrt(np.sum(x**2, axis=1, keepdims=True)) y = x / l2_norm_axis_1 expect(node, inputs=[x], outputs=[y], name="test_l2normalization_axis_1") ``` #### _l1normalization_axis_0 ```python import numpy as np import onnx node = onnx.helper.make_node( "LpNormalization", inputs=["x"], outputs=["y"], axis=0, p=1 ) x = np.array([3.0, 4.0], dtype=np.float32) l1_norm_axis_0 = np.sum(abs(x), axis=0, keepdims=True) y = x / l1_norm_axis_0 expect(node, inputs=[x], outputs=[y], name="test_l1normalization_axis_0") ``` #### _l1normalization_negative_values ```python import numpy as np import onnx node = onnx.helper.make_node( "LpNormalization", inputs=["x"], outputs=["y"], axis=0, p=1 ) x = np.array([1.0, -1.0], dtype=np.float32) l1_norm = np.sum(abs(x), axis=0, keepdims=True) y = x / l1_norm expect( node, inputs=[x], outputs=[y], name="test_l1normalization_negative_values", ) ``` #### _l1normalization_axis_1 ```python import numpy as np import onnx node = onnx.helper.make_node( "LpNormalization", inputs=["x"], outputs=["y"], axis=1, p=1 ) x = np.array([[3.0, 4.0], [6.0, 8.0]], dtype=np.float32) l1_norm_axis_1 = np.sum(abs(x), axis=1, keepdims=True) y = x / l1_norm_axis_1 expect(node, inputs=[x], outputs=[y], name="test_l1normalization_axis_1") ``` #### _l1normalization_axis_last ```python import numpy as np import onnx node = onnx.helper.make_node( "LpNormalization", inputs=["x"], outputs=["y"], axis=-1, p=1 ) x = np.array( [[[1.0, 2.0, 2.0], [3.0, 4.0, 0.0]], [[0.0, 5.0, 5.0], [6.0, 8.0, 0.0]]], dtype=np.float32, ) l1_norm_axis_last = np.sum(abs(x), axis=-1, keepdims=True) y = x / l1_norm_axis_last expect(node, inputs=[x], outputs=[y], name="test_l1normalization_axis_last") ``` #### _default ```python import numpy as np import onnx node = onnx.helper.make_node("LpNormalization", inputs=["x"], outputs=["y"]) x = np.array( [[[1.0, 2.0, 2.0], [3.0, 4.0, 0.0]], [[0.0, 5.0, 5.0], [6.0, 8.0, 0.0]]], dtype=np.float32, ) lp_norm_default = np.sqrt(np.sum(x**2, axis=-1, keepdims=True)) y = x / lp_norm_default expect(node, inputs=[x], outputs=[y], name="test_lpnormalization_default") ``` ```{toctree} text_diff_LpNormalization_1_22 ``` (l-onnx-op-lpnormalization-1)= ## LpNormalization - 1 ### Version - **name**: [LpNormalization (GitHub)](https://github.com/onnx/onnx/blob/main/docs/Operators.md#LpNormalization) - **domain**: `main` - **since_version**: `1` - **function**: `False` - **support_level**: `SupportType.COMMON` - **shape inference**: `True` This version of the operator has been available **since version 1**. ### Summary Given a matrix, apply Lp-normalization along the provided axis. The output is computed as: `output = input / Lp_norm(input, axis)`. When the Lp norm is zero (i.e., all elements along the axis are zero), the output is defined to be zero to avoid division by zero. ### Attributes * **axis - INT** (default is `-1`): The axis on which to apply normalization, -1 mean last axis. * **p - INT** (default is `2`): The order of the normalization, only 1 or 2 are supported. ### Inputs - **input** (heterogeneous) - **T**: Input matrix ### Outputs - **output** (heterogeneous) - **T**: Matrix after normalization ### Type Constraints * **T** in ( `tensor(double)`, `tensor(float)`, `tensor(float16)` ): Constrain input and output types to float tensors.