How to calculate euclidean norm in TensorFlow

This post describes how to calculate euclidean norm in TensorFlow. Lets understand euclidean norm implementation with below code snippets

Calculating euclidean norm with TensorFlow


import tensorflow as tf

tensor = tf.constant([[1,2], [3,4]], dtype=tf.float32)

print(tf.norm(
    tensor, ord='euclidean', axis=None, keepdims=None, name=None
))

Output

tf.Tensor(5.477226, shape=(), dtype=float32)

Now lets see how tf.norm is calculating the norm, what all math operation it is performing behind the scenes and produce the same result without using tf.norm


  import tensorflow as tf
  import math
  from tensorflow.python.ops import math_ops

  tensor = tf.constant([[1,2], [3,4]], dtype=tf.float32)
  ## get conjugate of tensor
  tensor1 = math_ops.conj(tensor)
  print(tensor1)

  ## multiply tensor to conjugate tensor
  tensor2 = tensor*math_ops.conj(tensor)
  print(tensor2)

  ## apply reduce_sum on tensor2
  tensor3 = math_ops.reduce_sum(tensor2)
  print(tensor3)

  ## apply sqrt on tensor3
  output = math_ops.sqrt(tensor3)
  print(output)

  ## output using tf.norm
  out_norm = tf.norm(tensor, ord='euclidean', axis=None, keepdims=None, name=None)
  print(out_norm)


Output

tf.Tensor(
[[1. 2.]
 [3. 4.]], shape=(2, 2), dtype=float32)

tf.Tensor(
[[ 1.  4.]
 [ 9. 16.]], shape=(2, 2), dtype=float32)

tf.Tensor(30.0, shape=(), dtype=float32)

tf.Tensor(5.477226, shape=(), dtype=float32)

tf.Tensor(5.477226, shape=(), dtype=float32)

In the above snippet we calculated euclidean norm with axis=None, now we will calculate along the axis=0


import tensorflow as tf
import math
from tensorflow.python.ops import math_ops

tensor = tf.constant([[1,2], [3,4]], dtype=tf.float32)
## get conjugate of tensor
tensor1 = math_ops.conj(tensor)
print(tensor1)

## multiply tensor to conjugate tensor
tensor2 = tensor*math_ops.conj(tensor)
print(tensor2)

## apply reduce_sum on tensor2
tensor3 = math_ops.reduce_sum(tensor2, axis=0)
print(tensor3)

## apply sqrt on tensor3
output = math_ops.sqrt(tensor3)
print(output)

## output using tf.norm
out_norm = tf.norm(tensor, ord='euclidean', axis=0, keepdims=None, name=None)
print(out_norm)


Output

  tf.Tensor(
  [[1. 2.]
   [3. 4.]], shape=(2, 2), dtype=float32)
  tf.Tensor(
  [[ 1.  4.]
   [ 9. 16.]], shape=(2, 2), dtype=float32)
  tf.Tensor([10. 20.], shape=(2,), dtype=float32)
  tf.Tensor([3.1622777 4.472136 ], shape=(2,), dtype=float32)
  tf.Tensor([3.1622777 4.472136 ], shape=(2,), dtype=float32)

So we got the same output with and without using tf.norm

Change axis=1 and calculate euclidean norm


import tensorflow as tf
import math
from tensorflow.python.ops import math_ops

tensor = tf.constant([[1,2], [3,4]], dtype=tf.float32)
## get conjugate of tensor
tensor1 = math_ops.conj(tensor)
print(tensor1)

## multiply tensor to conjugate tensor
tensor2 = tensor*math_ops.conj(tensor)
print(tensor2)

## apply reduce_sum on tensor2
tensor3 = math_ops.reduce_sum(tensor2, axis=1)
print(tensor3)

## apply sqrt on tensor3
output = math_ops.sqrt(tensor3)
print(output)

## output using tf.norm
out_norm = tf.norm(tensor, ord='euclidean', axis=1, keepdims=None, name=None)
print(out_norm)


Output

tf.Tensor(
[[1. 2.]
 [3. 4.]], shape=(2, 2), dtype=float32)
tf.Tensor(
[[ 1.  4.]
 [ 9. 16.]], shape=(2, 2), dtype=float32)
tf.Tensor([ 5. 25.], shape=(2,), dtype=float32)
tf.Tensor([2.236068 5.      ], shape=(2,), dtype=float32)
tf.Tensor([2.236068 5.      ], shape=(2,), dtype=float32)


Category: TensorFlow