【问题标题】:`sklearn.preprocessing.normalize` (L2 norm) equivalent in Tensorflow or TFX`sklearn.preprocessing.normalize`(L2 范数)等效于 Tensorflow 或 TFX
【发布时间】:2022-06-21 21:33:20
【问题描述】:

如何在 Tensorflow 中进行 L2 规范?我正在 Tensorflow 或 tfx 中寻找相当于 sklearn.preprocessing.normalize 的内容。

【问题讨论】:

  • 这是 tf.math.l2_normalize()

标签: tensorflow


【解决方案1】:

您可以将tensorflow.keras.utils.normalize 用于 L2 规范,如下所示。

使用sklearn.preprocessing.normalize

X = [[ 1., -1.,  2.],
     [ 2.,  0.,  0.],
     [ 0.,  1., -1.]]

X_normalized = sklearn.preprocessing.normalize(X, norm='l2')
X_normalized

输出:

array([[ 0.40824829, -0.40824829,  0.81649658],
       [ 1.        ,  0.        ,  0.        ],
       [ 0.        ,  0.70710678, -0.70710678]])

使用 tf.keras.utils.normalize 得到与上面相同的输出

X = [[ 1., -1.,  2.],
     [ 2.,  0.,  0.],
     [ 0.,  1., -1.]]

tf.keras.utils.normalize(
    X, order=2
)

输出:

array([[ 0.40824829, -0.40824829,  0.81649658],
       [ 1.        ,  0.        ,  0.        ],
       [ 0.        ,  0.70710678, -0.70710678]])

【讨论】:

    猜你喜欢
    • 2018-06-22
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2021-08-01
    • 2020-09-26
    相关资源
    最近更新 更多