funsor icon indicating copy to clipboard operation
funsor copied to clipboard

Apply funsor and .apply() method

Open ordabayevy opened this issue 3 years ago • 1 comments

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"}))

ordabayevy avatar Mar 07 '21 21:03 ordabayevy

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.

eb8680 avatar Mar 07 '21 23:03 eb8680