HIPS / autograd

Efficiently computes derivatives of NumPy code.
MIT License
7.02k stars 913 forks source link

Bug in concatenate? #399

Open deepers opened 6 years ago

deepers commented 6 years ago

I seem to be getting the wrong answer when I compute the Jacobian of a function defined using concatenate. The Jacobian of the function below should be always be [[1]] but it's coming out [[0]]. I'm running off the master branch of autograd.

Thanks, Deepee

(deepee) deepee@entropy:~$ cat concatenate.py 
from autograd import jacobian, numpy as anp
import numpy

def f(x):
    return anp.concatenate([[x[0]]])

print(f(anp.array([1.])))
print(f(anp.array([2.])))
print(jacobian(f)(anp.array([1.])))
print(jacobian(f)(anp.array([2.])))
print(numpy.version.version)

(deepee) deepee@entropy:~$ python --version
Python 3.6.4 :: Anaconda, Inc.

(deepee) deepee@entropy:~$ python concatenate.py 
[1.]
[2.]
/home/deepee/.local/opt/anaconda3/envs/deepee/lib/python3.6/site-packages/autograd-1.2-py3.6.egg/autograd/tracer.py:14: UserWarning: Output seems independent of input.
  warnings.warn("Output seems independent of input.")
[[0.]]
[[0.]]
1.14.0
deepers commented 6 years ago

It seems like this bug occurs when you pass in a list as one of the items being concatenated. If you first wrap the list in an array, then everything is fine.

(deepee) deepee@entropy:~$ cat concatenate.py 
from autograd import jacobian, numpy as anp
import numpy

def f(x):
    return anp.concatenate([anp.array([x[0]])])

print(f(anp.array([1.])))
print(f(anp.array([2.])))
print(jacobian(f)(anp.array([1.])))
print(jacobian(f)(anp.array([2.])))

(deepee) deepee@entropy:~$ python concatenate.py 
[1.]
[2.]
[[1.]]
[[1.]]
j-towns commented 6 years ago

Thanks for raising this. This bug would also occur with tuples and is a result of the fact that concatenate only descends one level into (nested) tuples and lists looking for things to differentiate w.r.t.

Since the argument you’re passing to concatenate is a nested list it doesn’t get picked up. We are working on some changes to the Autograd core that will make this easy to fix. Hopefully should be merged soon. On Fri, 18 May 2018 at 09:01, deepers notifications@github.com wrote:

It seems like this bug occurs when you pass in a list as one of the items being concatenated. If you first wrap the list in an array, then everything is fine.

(deepee) deepee@entropy:~$ cat concatenate.py from autograd import jacobian, numpy as anp import numpy

def f(x): return anp.concatenate([anp.array([x[0]])])

print(f(anp.array([1.]))) print(f(anp.array([2.]))) print(jacobian(f)(anp.array([1.]))) print(jacobian(f)(anp.array([2.])))

(deepee) deepee@entropy:~$ python concatenate.py [1.] [2.] [[1.]] [[1.]]

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/HIPS/autograd/issues/399#issuecomment-390253774, or mute the thread https://github.com/notifications/unsubscribe-auth/AOjgu7BuBqT0Oc0am7OFWm5i14QLCzekks5tzvBjgaJpZM4UDlFv .

deepers commented 6 years ago

Awesome! Thanks @j-towns! Deepee