"order"参数在tf.keras.utils.normalize()中意味着什么? [英] What does the `order` argument mean in `tf.keras.utils.normalize()`?
问题描述
考虑以下代码:
import numpy as np
A = 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. ]]
我了解如何通过 order = 1
计算 B1
,以便将 A
中的每个条目除以其中元素的总和柱子.例如, 0.8
变为 0.8/(0.8 + 0.1)= 0.888
.但是,我只是无法弄清楚 order = 2
是如何产生 B2
的,也找不到关于它的任何文档.
I understand how B1
is computed via order=1
such that each entry in A
is divided by the sum of the elements in its column. For example, 0.8
becomes 0.8/(0.8+0.1) = 0.888
. However, I just can't figure out how order=2
produces B2
nor can I find any documentation about it.
推荐答案
但是,我只是无法弄清order = 2如何产生B2,也找不到关于它的任何文档.
However, I just can't figure out how order=2 produces B2 nor can I find any documentation about it.
order = 1
表示L1规范,而 order = 2
表示L2规范.对于L2范数,您需要在对各个平方求和后取平方根.哪些元素要平方取决于轴.
order=1
means L1 norm while order=2
means L2 norm. For L2 norm, You need to take the square root after summing the individual squares. Which elements to square depends on the axis.
凯拉斯
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. ]])
您可以在此处查找不同类型的Norm: https://en.wikipedia.org/wiki/Norm_(mathematics)工作示例: https://docs.scipy.org/doc/numpy/reference/generation/numpy.linalg.norm.html
You can look up the different types of Norm here: https://en.wikipedia.org/wiki/Norm_(mathematics) Worked examples: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html
这篇关于"order"参数在tf.keras.utils.normalize()中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!