From 13c79b624e8a5b948738b9cdb2c8ba870bb6a237 Mon Sep 17 00:00:00 2001 From: raunakbhupal Date: Thu, 1 Apr 2021 23:10:46 +0530 Subject: [PATCH 1/5] Added pinv to tensorflow backend --- .../backends/tensorflow/tensorflow_backend.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tensornetwork/backends/tensorflow/tensorflow_backend.py b/tensornetwork/backends/tensorflow/tensorflow_backend.py index 0cf960838..054400dd4 100644 --- a/tensornetwork/backends/tensorflow/tensorflow_backend.py +++ b/tensornetwork/backends/tensorflow/tensorflow_backend.py @@ -430,3 +430,19 @@ 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. + """ + raise NotImplementedError( + "Backend '{}' has not implemented pinv".format(self.name)) From 14016a1a328e67a948e0cdea0292a495e1e4f161 Mon Sep 17 00:00:00 2001 From: raunakbhupal Date: Fri, 2 Apr 2021 12:24:26 +0530 Subject: [PATCH 2/5] added return function to pinv --- tensornetwork/backends/tensorflow/tensorflow_backend.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tensornetwork/backends/tensorflow/tensorflow_backend.py b/tensornetwork/backends/tensorflow/tensorflow_backend.py index 054400dd4..4def9a232 100644 --- a/tensornetwork/backends/tensorflow/tensorflow_backend.py +++ b/tensornetwork/backends/tensorflow/tensorflow_backend.py @@ -444,5 +444,4 @@ def pinv(self, tensor: Tensor, rcond: float = 1E-15, hermitian: bool = False) -> Returns: tensor: The pseudo inverse of tensor. """ - raise NotImplementedError( - "Backend '{}' has not implemented pinv".format(self.name)) + return np.linalg.pinv(tensor) \ No newline at end of file From 59739a581fe6ef1a68bb6e8da48a49508a026376 Mon Sep 17 00:00:00 2001 From: raunakbhupal Date: Fri, 2 Apr 2021 12:30:45 +0530 Subject: [PATCH 3/5] added pinv to numpy backend --- tensornetwork/backends/numpy/numpy_backend.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tensornetwork/backends/numpy/numpy_backend.py b/tensornetwork/backends/numpy/numpy_backend.py index 2d3753911..1ba96a32a 100644 --- a/tensornetwork/backends/numpy/numpy_backend.py +++ b/tensornetwork/backends/numpy/numpy_backend.py @@ -795,3 +795,18 @@ 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) \ No newline at end of file From d0fb749bad7de245531ef4f89d5d858b6dc42e78 Mon Sep 17 00:00:00 2001 From: raunakbhupal Date: Fri, 2 Apr 2021 12:46:26 +0530 Subject: [PATCH 4/5] fixed line spacing --- tensornetwork/backends/numpy/numpy_backend.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tensornetwork/backends/numpy/numpy_backend.py b/tensornetwork/backends/numpy/numpy_backend.py index 1ba96a32a..652c63460 100644 --- a/tensornetwork/backends/numpy/numpy_backend.py +++ b/tensornetwork/backends/numpy/numpy_backend.py @@ -796,7 +796,8 @@ def eps(self, dtype: Type[np.number]) -> float: """ return np.finfo(dtype).eps - def pinv(self, tensor: Tensor, rcond: float = 1E-15, hermitian: bool = False) -> Tensor: + 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. @@ -804,7 +805,9 @@ def pinv(self, tensor: Tensor, rcond: float = 1E-15, hermitian: bool = False) -> 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. + 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. From aed20ba8548da62666ab599202efa0e7913c010a Mon Sep 17 00:00:00 2001 From: raunakbhupal Date: Fri, 2 Apr 2021 12:55:18 +0530 Subject: [PATCH 5/5] fixed spacing --- tensornetwork/backends/numpy/numpy_backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensornetwork/backends/numpy/numpy_backend.py b/tensornetwork/backends/numpy/numpy_backend.py index 652c63460..f08b2934f 100644 --- a/tensornetwork/backends/numpy/numpy_backend.py +++ b/tensornetwork/backends/numpy/numpy_backend.py @@ -797,7 +797,7 @@ def eps(self, dtype: Type[np.number]) -> float: return np.finfo(dtype).eps def pinv(self, tensor: Tensor, rcond: float = 1E-15, - hermitian: bool = False) -> Tensor: + hermitian: bool = False) -> Tensor: """ Compute the (Moore-Penrose) pseudo-inverse of a tensor. Returns the pseudo-inverse of tensor.