Hacker News new | ask | show | jobs
by simonbyrne 3148 days ago
No nested differentiation:

  >>> import tangent
  >>> import numpy as np
  >>> tangent.grad(tangent.grad(np.sin))
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/local/lib/python3.6/site-packages/tangent/grad_util.py", line 178, in grad
      node, namespace = grad_tree(func, wrt, motion, mode, preserve_result, verbose)
    File "/usr/local/lib/python3.6/site-packages/tangent/grad_util.py", line 97, in grad_tree
      namespace.update(six.get_function_globals(func))
  AttributeError: 'numpy.ufunc' object has no attribute '__globals__'
1 comments

^I get that error with just `tangent.grad(np.sin)`.

So, it's not a nested differentiation problem, so much as a problem with "ufuncs". If you wrap np.sin in your own function then it takes the gradient just fine.

def sin(x): return np.sin(x)

negative_sin = tangent.grad(tangent.grad(sin))

Ah, good point, thanks!