From 564c2bbd406ec3e695a00bf570b478a04fd2cdff Mon Sep 17 00:00:00 2001 From: WHOIM1205 Date: Wed, 11 Feb 2026 12:36:05 -0800 Subject: [PATCH 1/2] Fix MatrixPinv.make_node output shape for non-square matrices Signed-off-by: WHOIM1205 --- pytensor/tensor/nlinalg.py | 2 +- tests/tensor/test_nlinalg.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pytensor/tensor/nlinalg.py b/pytensor/tensor/nlinalg.py index dd9b132af4..8df97934f7 100644 --- a/pytensor/tensor/nlinalg.py +++ b/pytensor/tensor/nlinalg.py @@ -42,7 +42,7 @@ def make_node(self, x): out_dtype = "float64" else: out_dtype = x.dtype - return Apply(self, [x], [matrix(shape=x.type.shape, dtype=out_dtype)]) + return Apply(self, [x], [matrix(shape=(x.type.shape[1], x.type.shape[0]), dtype=out_dtype)]) def perform(self, node, inputs, outputs): (x,) = inputs diff --git a/tests/tensor/test_nlinalg.py b/tests/tensor/test_nlinalg.py index 0aea17af7e..a9624fd47d 100644 --- a/tests/tensor/test_nlinalg.py +++ b/tests/tensor/test_nlinalg.py @@ -70,6 +70,21 @@ def test_pseudoinverse_grad(): utt.verify_grad(pinv, [r]) +def test_pseudoinverse_static_shape(): + x = matrix(shape=(3, 5)) + z = pinv(x) + assert z.type.shape == (5, 3) + + g = pytensor.grad(z.sum(), x) + f = function([x], g) + + rng = np.random.default_rng(utt.fetch_seed()) + r = rng.standard_normal((3, 5)).astype(config.floatX) + assert f(r).shape == (3, 5) + + utt.verify_grad(pinv, [r]) + + class TestMatrixInverse(utt.InferShapeTester): def setup_method(self): super().setup_method() From 27f804f56aa503b9d472ff649e2308bf1bce3e62 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Feb 2026 20:41:33 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pytensor/tensor/nlinalg.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pytensor/tensor/nlinalg.py b/pytensor/tensor/nlinalg.py index 8df97934f7..2ccc1c5930 100644 --- a/pytensor/tensor/nlinalg.py +++ b/pytensor/tensor/nlinalg.py @@ -42,7 +42,11 @@ def make_node(self, x): out_dtype = "float64" else: out_dtype = x.dtype - return Apply(self, [x], [matrix(shape=(x.type.shape[1], x.type.shape[0]), dtype=out_dtype)]) + return Apply( + self, + [x], + [matrix(shape=(x.type.shape[1], x.type.shape[0]), dtype=out_dtype)], + ) def perform(self, node, inputs, outputs): (x,) = inputs