`tf.keras.utils.normalize()`中的`order`参数是什么意思?

考虑以下代码:

import numpy as npA = np.array([[.8, .6], [.1, 0]])B1 = tf.keras.utils.normalize(A, axis=0, order=1)B2 = tf.keras.utils.normalize(A, axis=0, order=2)print('A:')print(A)print('B1:')print(B1)print('B2:')print(B2)

返回结果如下:

A:[[0.8 0.6] [0.1 0. ]]B1:[[0.88888889 1.        ] [0.11111111 0.        ]]B2:[[0.99227788 1.        ] [0.12403473 0.        ]]

我理解B1是通过order=1计算的,即A中的每个条目除以其所在列的元素总和。例如,0.8变为0.8/(0.8+0.1) = 0.888。然而,我无法理解order=2是如何产生B2的,也找不到任何相关文档。


回答:

然而,我无法理解order=2是如何产生B2的,也找不到任何相关文档。

order=1表示L1范数,而order=2表示L2范数。对于L2范数,你需要在对各个元素平方求和后再取平方根。具体要平方哪些元素取决于轴的选择。

Keras

A = np.array([[.8, .6], [.1, 0]])B2 = tf.keras.utils.normalize(A, axis=0, order=2)print(B2)array([[0.99227788, 1.        ],       [0.12403473, 0.        ]])

手动计算

B2_manual = np.zeros((2,2))B2_manual[0][0] = 0.8/np.sqrt(0.8 ** 2 + 0.1 ** 2)B2_manual[1][0] = 0.1/np.sqrt(0.8 ** 2 + 0.1 ** 2)B2_manual[0][1] = 0.6/np.sqrt(0.6 ** 2 + 0 ** 2)B2_manual[1][1] =  0 /np.sqrt(0.6 ** 2 + 0 ** 2)print(B2_manual)array([[0.99227788, 1.        ],       [0.12403473, 0.        ]])

你可以在这里查找不同类型的范数:https://en.wikipedia.org/wiki/Norm_(mathematics)工作示例:https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注