TensorFlow matrix rate

I need to calculate the Frobenius norm to get this formula using the TensorFlow framework:

Formula

where wis a matrix with 50 rows and 100 columns.

I tried to write something, but I do not understand how to fill in the argument axis.

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

According to the TensorFlow docs, I have to use a 2-tuple (or 2-list) because it defines the axes in the tensor by which to calculate the matrix norm, but I just need a simple Frobenius norm. In SciPy, for example, I can do this without specifying any axis.

So what should I use as a axisfunction to emulate SciPy?

+6
source share
4 answers

Thus, the Frobenius norm is the sum over the matrix nxm, but tf.normallows you to process several vectors and matrices in batch mode.

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

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

This can be seen as several matrices aligned in one direction, but the function cannot determine which one. This may be 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]

Thus, it axismainly indicates which directions you want to consider when summing in the Frobenius norm.

In your case, either of [1,2]or [-2,-1]will work.

+6
source

. . , , , = [- 2, -1] = , , .

[-2, -1] .

+2

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

.

+2

,

tf.reduce_sum(tf.multiply(x, x))

norm, , pow, , , , .

+1

Source: https://habr.com/ru/post/1676920/


All Articles