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

Apply funsor and .apply() method #486

Open
ordabayevy opened this issue Mar 7, 2021 · 1 comment
Open

Apply funsor and .apply() method #486

ordabayevy opened this issue Mar 7, 2021 · 1 comment

Comments

@ordabayevy
Copy link
Member

This is a suggestion to create an API similar to https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html that would allow to apply functions on named axes

class Apply(Funsor):
    ...

class Funsor(object, metaclass=FunsorMeta):
    ...
    def apply(self, op, applied_vars=None):
        return Apply(op, self, applied_vars)

# example
x.apply(ops.add, frozenset({"a", "b"})) # same as .reduce(ops.add ... )
x.apply(torch.mean, frozenset({"a", "b"}))
@eb8680
Copy link
Member

eb8680 commented Mar 7, 2021

As discussed on Friday, you can already achieve something like this with funsor.terms.Lambda:

ops.mean(Lambda("b", Lambda("a", x)), dims=(0, 1))

Clearly this pattern could be generalized into something like Apply.

I'm not sure this is a programming model we should be encouraging, though - using Lambda and black-box ops to bind variables prevents Funsor from introspecting or transforming computations like mean or variance that contain meaningful linear structure and ties otherwise abstract mathematical definitions to design and naming decisions specific to tensor operations.

Instead, we should prefer defining new computations with make_funsor and more basic Funsor primitives like Reduce, Binary and Subs wherever possible.

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

No branches or pull requests

2 participants