MCPcopy Create free account
hub / github.com/google-research/language / l2_normalize

Function l2_normalize

language/common/inputs/embedding_utils.py:267–282  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

265
266
267def 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

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…