TensorFlow Calculate Matrix L1, L2 and L Infinity Norm: A Beginner Guide – TensorFlow Tutorial

By | July 6, 2020

Matrix Norm usually contain L1, L2 and L infinity Norm. Here is a basic introduction for beginners.

Understand Matrix Norm: A Beginner Introduction

In this tutorial, we will calculate the L1, L2 and L infinity Norm of a matrix using tensorflow.

Preliminary

We should create a matrix tensor first.

import tensorflow as tf
import numpy as np

xs = tf.Variable(np.array([[1, 2],[3, 4]]), dtype = tf.float32)

Here xs is 2*2 matrix.

Calculate xs l1 norm

We can use tf.norm() to calculate. Here is the examplde code.

l1_norm = tf.reduce_max(tf.norm(xs, ord = 1, axis = 0))

Calculate xs l2 norm

To get the l2 norm of a matrix, we should get its eigenvalue, we can use tf.svd() to compute the eigenvalue of a matrix.

s, u, v = tf.svd(xs)
l2_norm = tf.reduce_max(s)

Notice: you can not calculate the l2 norm of a matrix by this code:

l2_norm = tf.norm(xs, ord = 2)

Calculate xs l infinity norm

Similar to xs l1 norm, we can get the l infinity norm as:

l_infinity_norm = tf.reduce_max(tf.norm(xs, ord = 1, axis = 1))

Then print all values

init = tf.global_variables_initializer() 
init_local = tf.local_variables_initializer()
with tf.Session() as sess:
    sess.run([init, init_local])
    print(sess.run(l1_norm))
    print(sess.run(l2_norm))
    print(sess.run(l_infinity_norm))

Run this code, we will get the l1, l2 and l infinity norm of xs is:

6.0
5.4649854
7.0

Leave a Reply