Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matrix norm in TensorFlow

I need to compute the Frobenius norm in order to achieve this formula using the TensorFlow framework:

Formula

where w is a matrix with 50 rows and 100 columns.

I tried to write something, but I don't understand how to fill out the axis argument.

tf.pow(
    tf.norm(x, ord='fro', axis=?), 2
)

According to the TensorFlow docs I have to use a 2-tuple (or a 2-list) because it determines the axies in tensor over which to compute a matrix norm, but I simply need a plain Frobenius norm. In SciPy, for example, I can do it without specify any axis.

So, what should I use as axis to emulate the SciPy function?

like image 449
Samuele Colombo Avatar asked May 11 '17 13:05

Samuele Colombo


3 Answers

So the Frobenius norm is a sum over a nxm matrix, but tf.norm allows to process several vectors and matrices in batch.

To better understand, imagine you have a rank 3 tensor:

t = [[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]]

It can be seen as several matrices aligned over one direction, but the function can't figure by itself which one. It could be either a batch of the following matrices:

[2, 4, 6] , [8 ,10, 12], [14, 16, 18]

or

[2 8 14], [4, 10, 16], [6, 12, 18]

So basically axis tells which directions you want to consider when doing the summation in the Frobenius norm.

In your case, any of [1,2] or [-2,-1] would do the trick.

like image 176
Grasshopper Avatar answered Sep 26 '22 09:09

Grasshopper


Independent of the number of dimensions of the tensor,

tf.sqrt(tf.reduce_sum(tf.square(w)))

should do the trick.

like image 33
gizzmole Avatar answered Sep 25 '22 09:09

gizzmole


Negative indices are supported. Example: If you are passing a tensor that can be either a matrix or a batch of matrices at runtime, pass axis=[-2,-1] instead of axis=None to make sure that matrix norms are computed.

I just tested and [-2,-1] works.

like image 27
Manuel Avatar answered Sep 22 '22 09:09

Manuel