Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Pinv numpy backend #918

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tensornetwork/backends/numpy/numpy_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,3 +795,21 @@ def eps(self, dtype: Type[np.number]) -> float:
float: Machine epsilon.
"""
return np.finfo(dtype).eps

def pinv(self, tensor: Tensor, rcond: float = 1E-15,
hermitian: bool = False) -> Tensor:
"""
Compute the (Moore-Penrose) pseudo-inverse of a tensor.
Returns the pseudo-inverse of tensor.

Args:
tensor: A tensor.
rcond: Cutoff for small singular values.
hermitian(optional): If True, matrix provided is
assumed to be Hermitian (symmetric if real-valued).
Defaults to False.

Returns:
tensor: The pseudo inverse of tensor.
"""
return np.linalg.pinv(tensor)
15 changes: 15 additions & 0 deletions tensornetwork/backends/tensorflow/tensorflow_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,18 @@ def eps(self, dtype: Type[np.number]) -> float:
float: Machine epsilon.
"""
return tf.experimental.numpy.finfo(dtype).eps

def pinv(self, tensor: Tensor, rcond: float = 1E-15, hermitian: bool = False) -> Tensor:
"""
Compute the (Moore-Penrose) pseudo-inverse of a tensor.
Returns the pseudo-inverse of tensor.

Args:
tensor: A tensor.
rcond: Cutoff for small singular values.
hermitian(optional): If True, matrix provided is assumed to be Hermitian (symmetric if real-valued). Defaults to False.

Returns:
tensor: The pseudo inverse of tensor.
"""
return np.linalg.pinv(tensor)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should use the tensorflow version here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi.
In a StackOverflow article someone has mentioned that the Tensorflow version doesn't work for complex matrices and that's why I went with the Numpy Version.
So shall I still use the Tensorflow version only?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, please use tensorflow. The point here is to use backend-native operations.