HIPS / autograd

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

autograd.numpy import in Eclipse #90

Open deepers opened 8 years ago

deepers commented 8 years ago

I'm using autograd for variational inference and loving it. Thanks!

Unfortunately, when I import autograd.numpy, Eclipse does not recognize the wrapped numpy functions in that namespace. For example, if I have

from autograd import numpy as np
np.exp(1)

Eclipse complains that exp is not defined. I'm not sure if this is easy to fix in autograd, but I thought I'd ask.

Thanks, Deepee

mattjj commented 8 years ago

My guess is that the way the numpy wrapper module is populated, grabbing the names from numpy.__dict__ and then importing them into our numpy module, is too indirect for Eclipse to follow. We'd have to experiment to figure out how to trick it into working. One possibility might be to add a from numpy import * just before the wrap_namespace call, since that might import the names in a more Eclipse-friendly way while still re-binding them.

(However, I don't have Eclipse set up, so it's not currently easy for me to try things out!)

deepers commented 8 years ago

Thanks Matt. I'll give that a go myself. Deepee

deepers commented 8 years ago

Hi Matt,

Your suggestion seems to work...I made similar changes to the random and linalg subpackages. Do you want me to create a pull request? (I'm new to making changes on Github.)

Thanks, Deepee

deepee@entropy:~/.local/opt/autograd$ git diff
diff --git a/autograd/numpy/linalg.py b/autograd/numpy/linalg.py
index e0aaf33..1200d22 100644
--- a/autograd/numpy/linalg.py
+++ b/autograd/numpy/linalg.py
@@ -7,6 +7,7 @@ from . import numpy_wrapper as anp
 from ..core import primitive
 from builtins import range

+from numpy.linalg import *
 wrap_namespace(npla.__dict__, globals())

 # Some formulas are from
diff --git a/autograd/numpy/numpy_wrapper.py b/autograd/numpy/numpy_wrapper.py
index e1d175f..5a0d30e 100644
--- a/autograd/numpy/numpy_wrapper.py
+++ b/autograd/numpy/numpy_wrapper.py
@@ -38,6 +38,7 @@ def wrap_namespace(old, new):
         elif type(obj) in unchanged_types:
             new[name] = obj

+from numpy import *
 wrap_namespace(np.__dict__, globals())

 # ----- Special treatment of list-input functions -----
diff --git a/autograd/numpy/random.py b/autograd/numpy/random.py
index de045b5..771e092 100644
--- a/autograd/numpy/random.py
+++ b/autograd/numpy/random.py
@@ -2,4 +2,5 @@ from __future__ import absolute_import
 import numpy.random as npr
 from .numpy_wrapper import wrap_namespace

+from numpy.random import *
 wrap_namespace(npr.__dict__, globals())
deepers commented 8 years ago

Oops, I spoke too soon. It turns out this does mess things up. It looks like what happens now is that from .numpy_wrapper import * brings in linalg from regular numpy, and then somehow from . import linalg has no effect. That what I'm guessing. Anyway, the result is that with the above change autograd.numpy.linalg ends up being the regular numpy.linalg package.

mattjj commented 8 years ago

I guess the Python import statement doesn't clobber existing name bindings, at least with that syntax. If you add del linalg, fft, random right before their wrapper imports in autograd/numpy/__init__.py it should work. Can you give that a shot and see how things look?

deepers commented 8 years ago

That seems to work. Are we sure the from numpy import * isn't importing anything else we don't want?

I'm happy to keep this change in my local repo if you think it's too hacky for general release, but I can create a pull request if you like.

Thanks, Deepee

mattjj commented 8 years ago

Yeah, it does seem a bit dangerous. Do the nose tests pass? (Just run nosetests tests in the git root, or if you make a PR they'll get run automatically by travis.)

@dougalm @duvenaud any thoughts? The goal here is to tweak the importing that the wrappers do to provide better IDE support.

deepers commented 8 years ago

nose tests seem okay, apart from the warning.

deepee@entropy:~/.local/opt/autograd$ nosetests tests
....../home/deepee/.local/opt/autograd/autograd/core.py:37: UserWarning: Output seems independent of input. Returning zero gradient.
  warnings.warn("Output seems independent of input. Returning zero gradient.")
.........................................................................................................................................................................................................................................................................................................................................................................................................................................................
----------------------------------------------------------------------
Ran 447 tests in 44.980s

OK