funsor
funsor copied to clipboard
Apply funsor and .apply() method
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"}))
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.