Python – tensorflow.math.l2_normalize()
Last Updated :
16 Feb, 2022
TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks.
l2_normalize() is used to normalize a tensor along axis using L2 norm.
Syntax: tensorflow.math.l2_normalize( x, axis, epsilon, name)
Parameters:
- x: It’s the input tensor.
- axis: It defines the dimension along which tensor will be normalized.
- epsilon: It defines the lower bound value for norm. Default value is 1e-12. It uses sqrt(epsilon) as divisor if norm<sqrt(divisor).
- name: An optional parameter that defines the name for the operation.
Returns:
It returns a tensor of same shape as x.
Example 1:
Python3
import tensorflow as tf
a = tf.constant([ 7 , 8 , 13 , 11 ], dtype = tf.float64)
print ( 'a: ' , a)
res = tf.math.l2_normalize(a)
print ( 'Result: ' , res)
|
Output:
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)
Example 2: This example uses 2-D tensor.
Python3
import tensorflow as tf
a = tf.constant([[ 7 , 8 ], [ 13 , 11 ]], dtype = tf.float64)
print ( 'a: ' , a)
res = tf.math.l2_normalize(x = a, axis = 1 )
print ( 'Result: ' , res)
|
Output:
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...