Divide by the L2-norm to normalize the input vector(s). For the edge case of the zero vector, this simply returns zero. Args: v: Numpy vector or matrix. axis: Dimension along which to normalize. Returns: normalized_v: Vector(s) `v` normalized to a unit vector.
(v, axis=0)
| 265 | |
| 266 | |
| 267 | def l2_normalize(v, axis=0): |
| 268 | """Divide by the L2-norm to normalize the input vector(s). |
| 269 | |
| 270 | For the edge case of the zero vector, this simply returns zero. |
| 271 | |
| 272 | Args: |
| 273 | v: Numpy vector or matrix. |
| 274 | axis: Dimension along which to normalize. |
| 275 | |
| 276 | Returns: |
| 277 | normalized_v: Vector(s) `v` normalized to a unit vector. |
| 278 | """ |
| 279 | norm = np.sqrt(np.square(v).sum(axis=axis, keepdims=True)) |
| 280 | # Set zero-norm elements to 1 to avoid NaN when dividing. |
| 281 | norm[norm == 0] = 1. |
| 282 | return v / norm |
no outgoing calls
no test coverage detected
searching dependent graphs…