How do you compute a length for each two-dimensional embedding?

linalg.norm can measure each vector along a specified axis. The Euclidean norm is the square root of the sum of squared components.

numpy.linalg.norm in practice

Rows [3,4] and [0,2] have lengths five and two. Decide how to treat a zero vector before dividing by row lengths for normalization.

import numpy as np
np.linalg.norm([[3,4],[0,2]],axis=1)

Result [5.0, 2.0]

中文

如何计算每个二维嵌入的长度?

linalg.norm 在指定轴上可计算向量范数。二范数是分量平方和的平方根。

numpy.linalg.norm 算例

两行嵌入 [3,4] 和 [0,2] 的长度分别为 5 和 2。后续若要除以长度做归一化,零向量应有单独策略。

import numpy as np
np.linalg.norm([[3,4],[0,2]],axis=1)

结果 [5.0, 2.0]

References / 参考来源

Keep reading / 继续阅读