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

'ArrayBox' object has no attribute 'dot' when differentiating function containing x.dot(y) #608

Open
ForceBru opened this issue Sep 2, 2023 · 1 comment

Comments

@ForceBru
Copy link

ForceBru commented Sep 2, 2023

Looks like ArrayBox has not attribute dot, so I can't differentiate x.dot(y) wrt x:

>>> import autograd, autograd.numpy as np
>>> x = np.array([1., 3,4])
>>> cs = np.array([0.2,0.6,0.1])
>>> autograd.grad(lambda x: x.dot(cs))(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/forcebru/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autograd/wrap_util.py", line 20, in nary_f
    return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/forcebru/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autograd/differential_operators.py", line 28, in grad
    vjp, ans = _make_vjp(fun, x)
               ^^^^^^^^^^^^^^^^^
  File "/Users/forcebru/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autograd/core.py", line 10, in make_vjp
    end_value, end_node =  trace(start_node, fun, x)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/forcebru/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autograd/tracer.py", line 10, in trace
    end_box = fun(start_box)
              ^^^^^^^^^^^^^^
  File "/Users/forcebru/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autograd/wrap_util.py", line 15, in unary_f
    return fun(*subargs, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "<stdin>", line 1, in <lambda>
AttributeError: 'ArrayBox' object has no attribute 'dot'

I managed to fix this by adding 'dot' to diff_methods here:

# These numpy.ndarray methods are just refs to an equivalent numpy function
nondiff_methods = ['all', 'any', 'argmax', 'argmin', 'argpartition',
'argsort', 'nonzero', 'searchsorted', 'round']
diff_methods = ['clip', 'compress', 'cumprod', 'cumsum', 'diagonal',
'max', 'mean', 'min', 'prod', 'ptp', 'ravel', 'repeat',
'reshape', 'squeeze', 'std', 'sum', 'swapaxes', 'take',
'trace', 'transpose', 'var']
for method_name in nondiff_methods + diff_methods:
setattr(ArrayBox, method_name, anp.__dict__[method_name])

Now the error is gone and my code seems to produce correct results. According to NumPy, numpy.dot is the equivalent function for the numpy.ndarray.dot method, so this also seems to align with what the comment says.


Should 'dot' indeed be added to diff_methods, or is this expected behavior?

@Benja-Vera
Copy link

Hi, I had the same problem and I think you're right. Especially because when replacing the last line in your code by

autograd.grad(lambda x: cs.dot(x))(x)

(i.e, just swapping x and cs in the function definition) it works as expected.

I do think that 'dot' should be added to the diff_methods, but in the meantime my fix was to just replace the dot by the numpy @ operator. That works in any case.

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

3 participants
@ForceBru @Benja-Vera and others