tf.linalg.pinv
Stay organized with collections
Save and categorize content based on your preferences.
Compute the Moore-Penrose pseudo-inverse of one or more matrices.
tf.linalg.pinv(
a, rcond=None, validate_args=False, name=None
)
Calculate the generalized inverse of a matrix using its
singular-value decomposition (SVD) and including all large singular values.
The pseudo-inverse of a matrix A
, is defined as: 'the matrix that 'solves'
[the least-squares problem] A @ x = b
,' i.e., if x_hat
is a solution, then
A_pinv
is the matrix such that x_hat = A_pinv @ b
. It can be shown that if
U @ Sigma @ V.T = A
is the singular value decomposition of A
, then
A_pinv = V @ inv(Sigma) U^T
. [(Strang, 1980)][1]
This function is analogous to numpy.linalg.pinv
.
It differs only in default value of rcond
. In numpy.linalg.pinv
, the
default rcond
is 1e-15
. Here the default is
10. * max(num_rows, num_cols) * np.finfo(dtype).eps
.
Args |
a
|
(Batch of) float -like matrix-shaped Tensor (s) which are to be
pseudo-inverted.
|
rcond
|
Tensor of small singular value cutoffs. Singular values smaller
(in modulus) than rcond * largest_singular_value (again, in modulus) are
set to zero. Must broadcast against tf.shape(a)[:-2] .
Default value: 10. * max(num_rows, num_cols) * np.finfo(a.dtype).eps .
|
validate_args
|
When True , additional assertions might be embedded in the
graph.
Default value: False (i.e., no graph assertions are added).
|
name
|
Python str prefixed to ops created by this function.
Default value: 'pinv'.
|
Returns |
a_pinv
|
(Batch of) pseudo-inverse of input a . Has same shape as a except
rightmost two dimensions are transposed.
|
Raises |
TypeError
|
if input a does not have float -like dtype .
|
ValueError
|
if input a has fewer than 2 dimensions.
|
Examples
import tensorflow as tf
import tensorflow_probability as tfp
a = tf.constant([[1., 0.4, 0.5],
[0.4, 0.2, 0.25],
[0.5, 0.25, 0.35]])
tf.matmul(tf.linalg.pinv(a), a)
# ==> array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]], dtype=float32)
a = tf.constant([[1., 0.4, 0.5, 1.],
[0.4, 0.2, 0.25, 2.],
[0.5, 0.25, 0.35, 3.]])
tf.matmul(tf.linalg.pinv(a), a)
# ==> array([[ 0.76, 0.37, 0.21, -0.02],
[ 0.37, 0.43, -0.33, 0.02],
[ 0.21, -0.33, 0.81, 0.01],
[-0.02, 0.02, 0.01, 1. ]], dtype=float32)
References
[1]: G. Strang. 'Linear Algebra and Its Applications, 2nd Ed.' Academic Press,
Inc., 1980, pp. 139-142.
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.pinv\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/linalg/linalg_impl.py#L807-L934) |\n\nCompute the Moore-Penrose pseudo-inverse of one or more matrices.\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.pinv`](https://www.tensorflow.org/api_docs/python/tf/linalg/pinv)\n\n\u003cbr /\u003e\n\n tf.linalg.pinv(\n a, rcond=None, validate_args=False, name=None\n )\n\nCalculate the [generalized inverse of a matrix](https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse) using its\nsingular-value decomposition (SVD) and including all large singular values.\n\nThe pseudo-inverse of a matrix `A`, is defined as: 'the matrix that 'solves'\n\\[the least-squares problem\\] `A @ x = b`,' i.e., if `x_hat` is a solution, then\n`A_pinv` is the matrix such that `x_hat = A_pinv @ b`. It can be shown that if\n`U @ Sigma @ V.T = A` is the singular value decomposition of `A`, then\n`A_pinv = V @ inv(Sigma) U^T`. \\[(Strang, 1980)\\]\\[1\\]\n\nThis function is analogous to [`numpy.linalg.pinv`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.pinv.html).\nIt differs only in default value of `rcond`. In `numpy.linalg.pinv`, the\ndefault `rcond` is `1e-15`. Here the default is\n`10. * max(num_rows, num_cols) * np.finfo(dtype).eps`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `a` | (Batch of) `float`-like matrix-shaped `Tensor`(s) which are to be pseudo-inverted. |\n| `rcond` | `Tensor` of small singular value cutoffs. Singular values smaller (in modulus) than `rcond` \\* largest_singular_value (again, in modulus) are set to zero. Must broadcast against `tf.shape(a)[:-2]`. Default value: `10. * max(num_rows, num_cols) * np.finfo(a.dtype).eps`. |\n| `validate_args` | When `True`, additional assertions might be embedded in the graph. Default value: `False` (i.e., no graph assertions are added). |\n| `name` | Python `str` prefixed to ops created by this function. Default value: 'pinv'. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|----------|---------------------------------------------------------------------------------------------------------------|\n| `a_pinv` | (Batch of) pseudo-inverse of input `a`. Has same shape as `a` except rightmost two dimensions are transposed. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|--------------------------------------------------|\n| `TypeError` | if input `a` does not have `float`-like `dtype`. |\n| `ValueError` | if input `a` has fewer than 2 dimensions. |\n\n\u003cbr /\u003e\n\n#### Examples\n\n import tensorflow as tf\n import tensorflow_probability as tfp\n\n a = tf.constant([[1., 0.4, 0.5],\n [0.4, 0.2, 0.25],\n [0.5, 0.25, 0.35]])\n tf.matmul(tf.linalg.pinv(a), a)\n # ==\u003e array([[1., 0., 0.],\n [0., 1., 0.],\n [0., 0., 1.]], dtype=float32)\n\n a = tf.constant([[1., 0.4, 0.5, 1.],\n [0.4, 0.2, 0.25, 2.],\n [0.5, 0.25, 0.35, 3.]])\n tf.matmul(tf.linalg.pinv(a), a)\n # ==\u003e array([[ 0.76, 0.37, 0.21, -0.02],\n [ 0.37, 0.43, -0.33, 0.02],\n [ 0.21, -0.33, 0.81, 0.01],\n [-0.02, 0.02, 0.01, 1. ]], dtype=float32)\n\n#### References\n\n\\[1\\]: G. Strang. 'Linear Algebra and Its Applications, 2nd Ed.' Academic Press,\nInc., 1980, pp. 139-142."]]