Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytensor.tensor.cartesian does not work on TensorVariables #7633

Closed
AuguB opened this issue Jan 6, 2025 · 3 comments
Closed

pytensor.tensor.cartesian does not work on TensorVariables #7633

AuguB opened this issue Jan 6, 2025 · 3 comments
Labels

Comments

@AuguB
Copy link

AuguB commented Jan 6, 2025

Describe the issue:

pytensor.tensor.cartesian does not work on TensorVariables

Reproduceable code example:

a = pm.Normal("a", shape=(7,))
b = pm.Normal("b", shape=(2,7))

pm.math.cartesian(a, b).shape.eval()
# Throws an error
# I need [7,2,7]

pytensor.tensor.mul(a,b).shape()
# [2,7] <- not what I need

Error message:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/opt/anaconda3/envs/dev_refactor_2/lib/python3.12/site-packages/pymc/math.py", line 214, in cartesian
    arrays_integer = [np.arange(len(x)) for x in arrays_2d]
                                ^^^^^^
TypeError: len() of unsized object

PyMC version information:

5.19.1 conda

Context for the issue:

I don't know if there is another way to do this, but I have two TensorVariables with possibly matching dimension lengths, and I need the cartesian product of them. I can't seem to find a way to let the matching dimensions be combined.

@AuguB AuguB added the bug label Jan 6, 2025
Copy link

welcome bot commented Jan 6, 2025

Welcome Banner]
🎉 Welcome to PyMC! 🎉 We're really excited to have your input into the project! 💖

If you haven't done so already, please make sure you check out our Contributing Guidelines and Code of Conduct.

@AuguB AuguB changed the title BUG: <Please write a comprehensive title after the 'BUG: ' prefix> pytensor.tensor.cartesian does not work on TensorVariables Jan 6, 2025
@AuguB
Copy link
Author

AuguB commented Jan 6, 2025

I came up with a workaround, but if there are more elegant soltions I am very happy to hear them:

with pm.Model() as model:
    a = pm.Normal("a", 0, 1, shape=(3, 4))
    b = pm.Normal("b", 0, 1, shape=(3))

    a_nones = len(a.shape.eval()) * [None]
    b_nones = len(b.shape.eval()) * [None]

    c = pt.tensor.mul(a[*b_nones], b[..., *a_nones])
    print(c.shape.eval())
-[3,3,4]

@AuguB AuguB closed this as completed Jan 6, 2025
@ricardoV94
Copy link
Member

ricardoV94 commented Jan 10, 2025

pt.mul.outer(a, b), or pt.mul.outer(b, a), not sure about the order from the top of my mind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants