张量流中的频谱范数2x2矩阵 [英] Spectral norm 2x2 matrix in tensorflow
问题描述
我有一个2x2矩阵,该矩阵由来自其他输入的变量J00, J01, J10, J11
定义.由于矩阵很小,因此我能够通过先计算迹线和行列式来计算光谱范数
I've got a 2x2 matrix defined by the variables J00, J01, J10, J11
coming in from other inputs. Since the matrix is small, I was able to compute the spectral norm by first computing the trace and determinant
J_T = tf.reduce_sum([J00, J11])
J_ad = tf.reduce_prod([J00, J11])
J_cb = tf.reduce_prod([J01, J10])
J_det = tf.reduce_sum([J_ad, -J_cb])
然后求解二次方
L1 = J_T/2.0 + tf.sqrt(J_T**2/4.0 - J_det)
L2 = J_T/2.0 - tf.sqrt(J_T**2/4.0 - J_det)
spectral_norm = tf.maximum(L1, L2)
这有效,但是看起来很丑陋,并且不能推广到更大的矩阵.有没有更简洁的方法(也许是我所缺少的方法调用)来计算spectral_norm
?
This works, but it looks rather ugly and it isn't generalizable to larger matrices. Is there cleaner way (maybe a method call that I'm missing) to compute spectral_norm
?
推荐答案
矩阵J
的频谱范数等于最大的矩阵的奇异值.
The spectral norm of a matrix J
equals the largest singular value of the matrix.
因此,您可以使用 tf.svd()
进行奇异值分解,并采用最大的奇异值:
Therefore you can use tf.svd()
to perform the singular value decomposition, and take the largest singular value:
spectral_norm = tf.svd(J,compute_uv=False)[...,0]
其中J
是您的矩阵.
注意:
- 我使用
compute_uv=False
是因为我们只对奇异值感兴趣,而对奇异矢量不感兴趣. -
J
不必是正方形. - 此解决方案也适用于
J
具有任意数量的批次尺寸(只要最后两个尺寸是矩阵尺寸)的情况. - 省略号
...
操作与NumPy中的操作相同. - 我选择
0
索引是因为我们只对最大的奇异值感兴趣.
- I use
compute_uv=False
since we are interested only in singular values, not singular vectors. J
does not need to be square.- This solution works also for the case where
J
has any number of batch dimensions (as long as the two last dimensions are the matrix dimensions). - The elipsis
...
operation works as in NumPy. - I take the
0
index because we are interested only in the largest singular value.
这篇关于张量流中的频谱范数2x2矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!