-
Notifications
You must be signed in to change notification settings - Fork 913
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
Implement ufunc.outer #234
Comments
I don't think our current primitive-wrapping strategy supports using any ufunc methods, but some of that functionality (like In [1]: a = b = np.arange(3)
In [2]: np.add.outer(a, b)
Out[2]:
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4]])
In [3]: a[:,None] + b
Out[3]:
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4]]) Is it possible you can use broadcasting for your use case? If not, can you provide an example of the kind of code you need to run with It's possible we might be able to add ufunc method support easily by introducing a |
Yes, absolutely. My request is that As a first order approximation, this should suffice to implement outer:
Right now, |
Elaborating on that ufunc comment, in 1.13 you can handle ufuncs as: class ArrayNode(autograd.core.Node, np.lib.mixins.NDArrayOperatorsMixin):
# ...
def __array_ufunc__(self, ufunc, method, *args, **kwargs):
p = get_the_autograd_primitive_for(ufunc)
if method == '__call__:
return p(*args, **kwargs)
elif method == 'outer':
do_the_above
else:
raise NotImplementedError('Request support for ufunc.{} on the issue tracker'.format(method))
# no need to implement any operator overloads here Leaving |
I think this would be straighforward to implement in the way you describe above, with a So just to be explicit, this should work (I'd probably put it in numpy_extra.py): class binary_ufunc_primitive(autograd.core.primitive):
def outer(self, a, b, **kwargs):
a = a.reshape(a.shape + (1,)*b.ndim)
b = b.reshape((1,)*a.ndim + b.shape)
return self(a, b, **kwargs) Then just have the binary ufuncs in numpy_grads.py and use that class. |
Otherwise you could do something like this (I find the subclassing syntax in Python pretty confusing so forgive me if this isn't correct): class binary_ufunc_primitive(autograd.core.primitive):
def __init__(self, fun):
self.outer = autograd.core.primitive(self.fun.outer)
super(binary_ufunc_primitive, self).__init__() This would probably give you slightly better performance, but at some point you'd also need to define the vjp of |
I reckon I could incorporate this (and the other binary ufunc methods) into #312. |
Would be handy if
np.add.outer
workedThe text was updated successfully, but these errors were encountered: