tf.linalg.lstsq
Stay organized with collections
Save and categorize content based on your preferences.
Solves one or more linear least-squares problems.
tf.linalg.lstsq(
matrix, rhs, l2_regularizer=0.0, fast=True, name=None
)
matrix
is a tensor of shape [..., M, N]
whose inner-most 2 dimensions
form M
-by-N
matrices. Rhs is a tensor of shape [..., M, K]
whose
inner-most 2 dimensions form M
-by-K
matrices. The computed output is a
Tensor
of shape [..., N, K]
whose inner-most 2 dimensions form M
-by-K
matrices that solve the equations
matrix[..., :, :] * output[..., :, :] = rhs[..., :, :]
in the least squares
sense.
Below we will use the following notation for each pair of matrix and
right-hand sides in the batch:
matrix
=\(A \in \Re^{m \times n}\),
rhs
=\(B \in \Re^{m \times k}\),
output
=\(X \in \Re^{n \times k}\),
l2_regularizer
=\(\lambda\).
If fast
is True
, then the solution is computed by solving the normal
equations using Cholesky decomposition. Specifically, if \(m \ge n\) then
\(X = (A^T A + \lambda I)^{-1} A^T B\), which solves the least-squares
problem \(X = \mathrm{argmin}_{Z \in \Re^{n \times k} } ||A Z - B||_F^2 +
\lambda ||Z||_F^2\). If \(m \lt n\) then output
is computed as
\(X = A^T (A A^T + \lambda I)^{-1} B\), which (for \(\lambda = 0\)) is
the minimum-norm solution to the under-determined linear system, i.e.
\(X = \mathrm{argmin}_{Z \in \Re^{n \times k} } ||Z||_F^2 \), subject to
\(A Z = B\). Notice that the fast path is only numerically stable when
\(A\) is numerically full rank and has a condition number
\(\mathrm{cond}(A) \lt \frac{1}{\sqrt{\epsilon_{mach} } }\) or\(\lambda\)
is sufficiently large.
If fast
is False
an algorithm based on the numerically robust complete
orthogonal decomposition is used. This computes the minimum-norm
least-squares solution, even when \(A\) is rank deficient. This path is
typically 6-7 times slower than the fast path. If fast
is False
then
l2_regularizer
is ignored.
Args |
matrix
|
Tensor of shape [..., M, N] .
|
rhs
|
Tensor of shape [..., M, K] .
|
l2_regularizer
|
0-D double Tensor . Ignored if fast=False .
|
fast
|
bool. Defaults to True .
|
name
|
string, optional name of the operation.
|
Returns |
output
|
Tensor of shape [..., N, K] whose inner-most 2 dimensions form
M -by-K matrices that solve the equations
matrix[..., :, :] * output[..., :, :] = rhs[..., :, :] in the least
squares sense.
|
Raises |
NotImplementedError
|
linalg.lstsq is currently disabled for complex128
and l2_regularizer != 0 due to poor accuracy.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.linalg.lstsq\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/linalg_ops.py#L244-L379) |\n\nSolves one or more linear least-squares problems.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.linalg.lstsq`](https://www.tensorflow.org/api_docs/python/tf/linalg/lstsq), [`tf.compat.v1.matrix_solve_ls`](https://www.tensorflow.org/api_docs/python/tf/linalg/lstsq)\n\n\u003cbr /\u003e\n\n tf.linalg.lstsq(\n matrix, rhs, l2_regularizer=0.0, fast=True, name=None\n )\n\n`matrix` is a tensor of shape `[..., M, N]` whose inner-most 2 dimensions\nform `M`-by-`N` matrices. Rhs is a tensor of shape `[..., M, K]` whose\ninner-most 2 dimensions form `M`-by-`K` matrices. The computed output is a\n`Tensor` of shape `[..., N, K]` whose inner-most 2 dimensions form `M`-by-`K`\nmatrices that solve the equations\n`matrix[..., :, :] * output[..., :, :] = rhs[..., :, :]` in the least squares\nsense.\n\nBelow we will use the following notation for each pair of matrix and\nright-hand sides in the batch:\n\n`matrix`=\\\\(A \\\\in \\\\Re\\^{m \\\\times n}\\\\),\n`rhs`=\\\\(B \\\\in \\\\Re\\^{m \\\\times k}\\\\),\n`output`=\\\\(X \\\\in \\\\Re\\^{n \\\\times k}\\\\),\n`l2_regularizer`=\\\\(\\\\lambda\\\\).\n\nIf `fast` is `True`, then the solution is computed by solving the normal\nequations using Cholesky decomposition. Specifically, if \\\\(m \\\\ge n\\\\) then\n\\\\(X = (A\\^T A + \\\\lambda I)\\^{-1} A\\^T B\\\\), which solves the least-squares\nproblem \\\\(X = \\\\mathrm{argmin}_{Z \\\\in \\\\Re\\^{n \\\\times k} } \\|\\|A Z - B\\|\\|_F\\^2 +\n\\\\lambda \\|\\|Z\\|\\|_F\\^2\\\\). If \\\\(m \\\\lt n\\\\) then `output` is computed as\n\\\\(X = A\\^T (A A\\^T + \\\\lambda I)\\^{-1} B\\\\), which (for \\\\(\\\\lambda = 0\\\\)) is\nthe minimum-norm solution to the under-determined linear system, i.e.\n\\\\(X = \\\\mathrm{argmin}_{Z \\\\in \\\\Re\\^{n \\\\times k} } \\|\\|Z\\|\\|_F\\^2 \\\\), subject to\n\\\\(A Z = B\\\\). Notice that the fast path is only numerically stable when\n\\\\(A\\\\) is numerically full rank and has a condition number\n\\\\(\\\\mathrm{cond}(A) \\\\lt \\\\frac{1}{\\\\sqrt{\\\\epsilon_{mach} } }\\\\) or\\\\(\\\\lambda\\\\)\nis sufficiently large.\n\nIf `fast` is `False` an algorithm based on the numerically robust complete\northogonal decomposition is used. This computes the minimum-norm\nleast-squares solution, even when \\\\(A\\\\) is rank deficient. This path is\ntypically 6-7 times slower than the fast path. If `fast` is `False` then\n`l2_regularizer` is ignored.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------------|-------------------------------------------------|\n| `matrix` | `Tensor` of shape `[..., M, N]`. |\n| `rhs` | `Tensor` of shape `[..., M, K]`. |\n| `l2_regularizer` | 0-D `double` `Tensor`. Ignored if `fast=False`. |\n| `fast` | bool. Defaults to `True`. |\n| `name` | string, optional name of the operation. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `output` | `Tensor` of shape `[..., N, K]` whose inner-most 2 dimensions form `M`-by-`K` matrices that solve the equations `matrix[..., :, :] * output[..., :, :] = rhs[..., :, :]` in the least squares sense. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-----------------------|-------------------------------------------------------------------------------------------------|\n| `NotImplementedError` | linalg.lstsq is currently disabled for complex128 and l2_regularizer != 0 due to poor accuracy. |\n\n\u003cbr /\u003e"]]