# tf.linalg.pinv

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/linalg/linalg_impl.py#L807-L934) |

Compute the Moore-Penrose pseudo-inverse of one or more matrices.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.linalg.pinv`](https://www.tensorflow.org/api_docs/python/tf/linalg/pinv)

<br />

    tf.linalg.pinv(
        a, rcond=None, validate_args=False, name=None
    )

Calculate the [generalized inverse of a matrix](https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse) 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`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.pinv.html).
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`.

<br />

<br />

<br />

| ## 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'. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| `a_pinv` | (Batch of) pseudo-inverse of input `a`. Has same shape as `a` except rightmost two dimensions are transposed. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `TypeError` | if input `a` does not have `float`-like `dtype`. |
| `ValueError` | if input `a` has fewer than 2 dimensions. |

<br />

#### 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.