Normalizes along dimension `axis` using an L2 norm. For a 1-D tensor with `axis = 0`, computes output = x / sqrt(max(sum(x**2), epsilon)) For `x` with more dimensions, independently normalizes each 1-D slice along dimension `axis`. Args: x: A `Tensor`. axis: Dimension along
(x, axis=None, epsilon=1e-12, name=None, do_fusion=True)
| 621 | |
| 622 | @tf_export("math.l2_normalize", "linalg.l2_normalize", "nn.l2_normalize", v1=[]) |
| 623 | def l2_normalize_v2(x, axis=None, epsilon=1e-12, name=None, do_fusion=True): |
| 624 | """Normalizes along dimension `axis` using an L2 norm. |
| 625 | |
| 626 | For a 1-D tensor with `axis = 0`, computes |
| 627 | |
| 628 | output = x / sqrt(max(sum(x**2), epsilon)) |
| 629 | |
| 630 | For `x` with more dimensions, independently normalizes each 1-D slice along |
| 631 | dimension `axis`. |
| 632 | |
| 633 | Args: |
| 634 | x: A `Tensor`. |
| 635 | axis: Dimension along which to normalize. A scalar or a vector of |
| 636 | integers. |
| 637 | epsilon: A lower bound value for the norm. Will use `sqrt(epsilon)` as the |
| 638 | divisor if `norm < sqrt(epsilon)`. |
| 639 | do_fusion: Whether fuse op when doing l2 norm on last axis, enabled by default. |
| 640 | name: A name for this operation (optional). |
| 641 | |
| 642 | Returns: |
| 643 | A `Tensor` with the same shape as `x`. |
| 644 | """ |
| 645 | if do_fusion and x.dtype == dtypes.float32 and ( |
| 646 | axis is None or axis== x.shape.rank - 1): |
| 647 | return fused_l2_normalize(x, epsilon=epsilon, name=name) |
| 648 | else: |
| 649 | with ops.name_scope(name, "l2_normalize", [x]) as name: |
| 650 | x = ops.convert_to_tensor(x, name="x") |
| 651 | square_sum = math_ops.reduce_sum(math_ops.square(x), axis, keepdims=True) |
| 652 | x_inv_norm = math_ops.rsqrt(math_ops.maximum(square_sum, epsilon)) |
| 653 | return math_ops.multiply(x, x_inv_norm, name=name) |
| 654 | |
| 655 | def fused_l2_normalize(x, epsilon=1e-12, name=None): |
| 656 | """Normalizes along last dimension using an L2 norm. |
no test coverage detected