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

Logic expression #6

Open
dalek-who opened this issue Jan 13, 2023 · 1 comment
Open

Logic expression #6

dalek-who opened this issue Jan 13, 2023 · 1 comment

Comments

@dalek-who
Copy link

dalek-who commented Jan 13, 2023

What a nice tools! However, current codes has trouble dealing with logic expression with more than 2 terms, for example:

x, y, z = sympy.symbols("x,y,z")
expr =x  | y | z 
model = SymPyModule(expressions=[expr])
terms = {
        "x": torch.randint(0, 2, (10,)).bool(),
        "y": torch.randint(0, 2, (10,)).bool(),
        "z": torch.randint(0, 2, (10,)).bool(),
}
result = model(**terms)

Then it will raise an error:

TypeError: logical_or() takes 2 positional arguments but 3 were given

Here's the solution: modify

sympy.And: torch.logical_and,
sympy.Or: torch.logical_or,

to

sympy.And: _reduce(torch.logical_and),
sympy.Or: _reduce(torch.logical_or),

, which is the same as mul and add.

Also, besides the modification above, I suggest two more improvements:

  1. make this function public
    def _reduce(fn):
    def fn_(*args):
    return ft.reduce(fn, args)
    return fn_
  2. modify
    _func_lookup = co.ChainMap(_global_func_lookup, extra_funcs)

    to
_func_lookup = co.ChainMap(extra_funcs, _global_func_lookup)

so that users can overload default lookup tables.

@patrick-kidger
Copy link
Owner

That all sounds reasonable to me! I'd happily accept a PR on these.

(I probably wouldn't bother making reduce public though, that's a pretty small helper function.)

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

No branches or pull requests

2 participants