View source on GitHub |
Computes the singular value decompositions of one or more matrices.
tf.linalg.svd(
tensor, full_matrices=False, compute_uv=True, name=None
)
Computes the SVD of each inner matrix in tensor
such that
tensor[..., :, :] = u[..., :, :] * diag(s[..., :, :]) *
transpose(conj(v[..., :, :]))
# a is a tensor.
# s is a tensor of singular values.
# u is a tensor of left singular vectors.
# v is a tensor of right singular vectors.
s, u, v = svd(a)
s = svd(a, compute_uv=False)
numpy compatibility
Mostly equivalent to numpy.linalg.svd, except that
- The order of output arguments here is
s
,u
,v
whencompute_uv
isTrue
, as opposed tou
,s
,v
for numpy.linalg.svd. - full_matrices is
False
by default as opposed toTrue
for numpy.linalg.svd. - tf.linalg.svd uses the standard definition of the SVD
\(A = U \Sigma V^H\), such that the left singular vectors of
a
are the columns ofu
, while the right singular vectors ofa
are the columns ofv
. On the other hand, numpy.linalg.svd returns the adjoint \(V^H\) as the third output argument.
import tensorflow as tf
import numpy as np
s, u, v = tf.linalg.svd(a)
tf_a_approx = tf.matmul(u, tf.matmul(tf.linalg.diag(s), v, adjoint_b=True))
u, s, v_adj = np.linalg.svd(a, full_matrices=False)
np_a_approx = np.dot(u, np.dot(np.diag(s), v_adj))
# tf_a_approx and np_a_approx should be numerically close.