📜  Python – tensorflow.math.l2_normalize()

📅  最后修改于: 2022-05-13 01:55:26.017000             🧑  作者: Mango

Python – tensorflow.math.l2_normalize()

TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

l2_normalize()用于使用 L2 范数对沿轴的张量进行归一化。

示例 1:

Python3
# Importing the library
import tensorflow as tf
 
# Initializing the input tensor
a = tf.constant([7, 8, 13, 11], dtype = tf.float64)
 
# Printing the input tensor
print('a: ', a)
 
# Calculating the result
res = tf.math.l2_normalize(a)
 
# Printing the result
print('Result: ', res)


Python3
# Importing the library
import tensorflow as tf
 
# Initializing the input tensor
a = tf.constant([[7, 8], [13, 11]], dtype = tf.float64)
 
# Printing the input tensor
print('a: ', a)
 
# Calculating the result
res = tf.math.l2_normalize(x = a, axis = 1)
 
# Printing the result
print('Result: ', res)


输出:

a:  tf.Tensor([ 7.  8. 13. 11.], shape=(4, ), dtype=float64)
Result:  tf.Tensor([0.34869484 0.39850839 0.64757613 0.54794903], shape=(4, ), dtype=float64)

示例 2:此示例使用二维张量。

Python3

# Importing the library
import tensorflow as tf
 
# Initializing the input tensor
a = tf.constant([[7, 8], [13, 11]], dtype = tf.float64)
 
# Printing the input tensor
print('a: ', a)
 
# Calculating the result
res = tf.math.l2_normalize(x = a, axis = 1)
 
# Printing the result
print('Result: ', res)

输出:

a:  tf.Tensor(
[[ 7.  8.]
 [13. 11.]], shape=(2, 2), dtype=float64)
Result:  tf.Tensor(
[[0.65850461 0.75257669]
 [0.76338629 0.64594224]], shape=(2, 2), dtype=float64)