r"""Computes the singular value decompositions of one or more matrices. Computes the SVD of each inner matrix in `tensor` such that `tensor[..., :, :] = u[..., :, :] * diag(s[..., :, :]) * transpose(conj(v[..., :, :]))` ```python # a is a tensor. # s is a tensor of singular values.
(tensor, full_matrices=False, compute_uv=True, name=None)
| 355 | @tf_export('linalg.svd', v1=['linalg.svd', 'svd']) |
| 356 | @deprecation.deprecated_endpoints('svd') |
| 357 | def svd(tensor, full_matrices=False, compute_uv=True, name=None): |
| 358 | r"""Computes the singular value decompositions of one or more matrices. |
| 359 | |
| 360 | Computes the SVD of each inner matrix in `tensor` such that |
| 361 | `tensor[..., :, :] = u[..., :, :] * diag(s[..., :, :]) * |
| 362 | transpose(conj(v[..., :, :]))` |
| 363 | |
| 364 | ```python |
| 365 | # a is a tensor. |
| 366 | # s is a tensor of singular values. |
| 367 | # u is a tensor of left singular vectors. |
| 368 | # v is a tensor of right singular vectors. |
| 369 | s, u, v = svd(a) |
| 370 | s = svd(a, compute_uv=False) |
| 371 | ``` |
| 372 | |
| 373 | Args: |
| 374 | tensor: `Tensor` of shape `[..., M, N]`. Let `P` be the minimum of `M` and |
| 375 | `N`. |
| 376 | full_matrices: If true, compute full-sized `u` and `v`. If false |
| 377 | (the default), compute only the leading `P` singular vectors. |
| 378 | Ignored if `compute_uv` is `False`. |
| 379 | compute_uv: If `True` then left and right singular vectors will be |
| 380 | computed and returned in `u` and `v`, respectively. Otherwise, only the |
| 381 | singular values will be computed, which can be significantly faster. |
| 382 | name: string, optional name of the operation. |
| 383 | |
| 384 | Returns: |
| 385 | s: Singular values. Shape is `[..., P]`. The values are sorted in reverse |
| 386 | order of magnitude, so s[..., 0] is the largest value, s[..., 1] is the |
| 387 | second largest, etc. |
| 388 | u: Left singular vectors. If `full_matrices` is `False` (default) then |
| 389 | shape is `[..., M, P]`; if `full_matrices` is `True` then shape is |
| 390 | `[..., M, M]`. Not returned if `compute_uv` is `False`. |
| 391 | v: Right singular vectors. If `full_matrices` is `False` (default) then |
| 392 | shape is `[..., N, P]`. If `full_matrices` is `True` then shape is |
| 393 | `[..., N, N]`. Not returned if `compute_uv` is `False`. |
| 394 | |
| 395 | @compatibility(numpy) |
| 396 | Mostly equivalent to numpy.linalg.svd, except that |
| 397 | * The order of output arguments here is `s`, `u`, `v` when `compute_uv` is |
| 398 | `True`, as opposed to `u`, `s`, `v` for numpy.linalg.svd. |
| 399 | * full_matrices is `False` by default as opposed to `True` for |
| 400 | numpy.linalg.svd. |
| 401 | * tf.linalg.svd uses the standard definition of the SVD |
| 402 | \\(A = U \Sigma V^H\\), such that the left singular vectors of `a` are |
| 403 | the columns of `u`, while the right singular vectors of `a` are the |
| 404 | columns of `v`. On the other hand, numpy.linalg.svd returns the adjoint |
| 405 | \\(V^H\\) as the third output argument. |
| 406 | ```python |
| 407 | import tensorflow as tf |
| 408 | import numpy as np |
| 409 | s, u, v = tf.linalg.svd(a) |
| 410 | tf_a_approx = tf.matmul(u, tf.matmul(tf.linalg.diag(s), v, adjoint_b=True)) |
| 411 | u, s, v_adj = np.linalg.svd(a, full_matrices=False) |
| 412 | np_a_approx = np.dot(u, np.dot(np.diag(s), v_adj)) |
| 413 | # tf_a_approx and np_a_approx should be numerically close. |
| 414 | ``` |
no outgoing calls
no test coverage detected