l2_normalize Normalizes along the dimension `axis` using an L2 norm. Args: x: Input tensor. axis: Dimension along which to normalize. eps: A lower bound value for the norm. Defaults to 1e-8. Returns: A Tensor with the same shape as x.
(x: Tensor, eps: float = 1e-8, axis: int = -1)
| 8 | |
| 9 | |
| 10 | def l2_normalize(x: Tensor, eps: float = 1e-8, axis: int = -1) -> Tensor: |
| 11 | """l2_normalize Normalizes along the dimension `axis` using an L2 norm. |
| 12 | |
| 13 | Args: |
| 14 | x: Input tensor. |
| 15 | axis: Dimension along which to normalize. |
| 16 | eps: A lower bound value for the norm. Defaults to 1e-8. |
| 17 | |
| 18 | Returns: |
| 19 | A Tensor with the same shape as x. |
| 20 | """ |
| 21 | sum2 = (x * x).sum(axis=axis, keepdims=True) |
| 22 | return x * jax.lax.rsqrt(sum2 + eps) |
no outgoing calls