For Python 3.8 and TensorFlow 2.5, I have a 3-D tensor of shape (3, 3, 3) where the goal is to compute the L2-norm for each of the three (3, 3) square matrices. The code that I came up with is:

a = tf.random.normal(shape = (3, 3, 3)) a.shape # TensorShape([3, 3, 3]) a.numpy() ''' array([[[-0.30071023, 0.9958398 , -0.77897555], [-1.4251901 , 0.8463568 , -0.6138699 ], [ 0.23176959, -2.1303613 , 0.01905925]], [[-1.0487134 , -0.36724553, -1.0881581 ], [-0.12025198, 0.20973174, -2.1444907 ], [ 1.4264063 , -1.5857363 , 0.31582597]], [[ 0.8316077 , -0.7645084 , 1.5271858 ], [-0.95836663, -1.868056 , -0.04956183], [-0.16384012, -0.18928945, 1.04647 ]]], dtype=float32) '''

I am using axis = 2 since the 3rd axis should contain three 3×3 square matrices. The output I get is:

tf.math.reduce_euclidean_norm(input_tensor = a, axis = 2).numpy() ''' array([[1.299587 , 1.7675754, 2.1430166], [1.5552354, 2.158075 , 2.15614 ], [1.8995634, 2.1001325, 1.0759989]], dtype=float32) '''

How are these values computed? The formula for computing L2-norm is this. What am I missing?

Also, I was expecting three L2-norm values, one for each of the three (3, 3) matrices. The code I have to achieve this is:

tf.math.reduce_euclidean_norm(a[0]).numpy() # 3.0668826 tf.math.reduce_euclidean_norm(a[1]).numpy() # 3.4241767 tf.math.reduce_euclidean_norm(a[2]).numpy() # 3.0293021

Is there any better way to get this without having to explicitly refer to each indices of tensor ‘a’?

Thanks!

submitted by /u/grid_world

[visit reddit] [comments]