Comments (7)
As to your question, you need the cost function (and anything it calls that uses numpy code) to use autograd's wrapped numpy. The easiest fix would be to change the numpy import statement in my_module.py to use autograd.numpy
.
As a hack, your code could munge the numpy module before importing my_module
, as in
import autograd.numpy as np
from autograd import grad
import numpy as unwrapped_numpy
unwrapped_numpy.log = np.log
from my_module import cost
The import of my_module
has to happen after munging the loaded numpy module in case any numpy functions are bound directly to names in my_module.py, i.e. in case there are lines in my_module.py like from numpy import log
. If the code in my_module.py only uses import numpy as np
and references numpy functions through that module reference, then the import order doesn't matter.
This hack is not a good long-term recipe, and it might even depend on CPython module loading details. (I didn't test it, but something along these lines should work.)
from autograd.
Actually, I wasn't able to test this fix (the error doesn't happen on my Python 2.7.10 setup), so I'm reopening the issue until I can test it later today. If you notice it's still broken, please paste the full error traceback.
from autograd.
Thank you for the swift response! I tested the fix but I don't think it works.. (Note that I manually hacked fix #64 in my code, as I am using pip. But from the error output you can see that the change is there).
Here is the full error traceback:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-d12611e5619b> in <module>()
14
15 print(grad_cost(x)) # works correctly!
---> 16 print(jaco_cost(x)) # error
/Users/bruno/anaconda3/lib/python3.4/site-packages/autograd/core.py in gradfun(*args, **kwargs)
51 grads = list(map(partial(backward_pass, start_node, tape=tape), end_nodes))
52 shape = dummy.outshape + getshape(args[argnum])
---> 53 return np.reshape(concatenate(grads), shape) if shape else grads[0]
54 return gradfun
55
/Users/bruno/anaconda3/lib/python3.4/site-packages/autograd/core.py in <lambda>(lst)
42 return list(np.ravel(val))
43
---> 44 concatenate = lambda lst: np.concatenate(map(np.atleast_1d, lst))
45
46 @attach_name_and_doc(fun, argnum, 'Jacobian')
TypeError: The first input argument needs to be a sequence
As regarding my question, I will look into your proposed hack solution, thanks!
from autograd.
Oh, the traceback shows the problem! The call to map
on line 44 needs a call to list
wrapped around it. That's how it was in the git master, but apparently the pip version doesn't include that fix.
I'll revert the change I made; it wasn't necessary because the fix is already in master. We'll have to update the pip version.
I'm going to close the issue because the bug doesn't exist on master, and we'll update the pip version at some point soon.
from autograd.
Perfect, it works and sorry for the confusion.
For other who might be interested, I used pip to install the latest master as follows:
pip install git+git://github.com/HIPS/autograd.git
from autograd.
I'm not sure whether this is the right place for this follow up (as the original issue has been closed) but I put some more thought into my question regarding switching between the stripped down autograd.numpy and the original numpy. This is what I came up with.
My my_module.py
starts as follows:
import config
if not config.AUTOGRAD:
import numpy as np
from scipy import special
else:
import autograd.numpy as np
from autograd.scipy import special
# rest of my_module remains unchanged.
The config
module that is imported contains the following boolean:
# Set autograd to active or not
AUTOGRAD = True
If is True, the autograd.numpy is loaded, else the normal numpy is loaded.
For my use case, this means I no longer have to copy paste functions from my_module.py
to an external Python file. I can just set AUTOGRAD to True and import the functions on which I want to apply autograd methods. I believe that this handles my situation pretty well, as I don't impose any restrictions on the functions within my_module.py
. If I want to use autograd on any of them, I can now directly import these functions if AUTOGRAD is set to True.
The price I pay is that if AUTOGRAD is set to True, then functions in my_module.py
that rely on contents from 'regular numpy' that is not in autograd.numpy won't work. But at least I implicitly acknowledged this by setting AUTOGRAD to True.
Another option could be to move all the functions in my_module.py
that are autograd 'related' to a different module, but to me that feels like I'm changing too much in the structure of my code. This seems like a nice solution that handles my situation pretty well, with very limited impact on my current codebase.
from autograd.
That seems like a reasonable enough solution. Of course, if there are specific things in 'regular numpy' that are not in autograd.numpy, you can let us know by opening an issue!
from autograd.
Related Issues (20)
- support for Jax-like custom forward pass definition? HOT 1
- Gradient become Nan for 0 value test HOT 1
- Is it possible to see gradient function? HOT 1
- Four scipy tests are failing HOT 6
- Add np.float128,np.complex256 dtypes to vspaces and boxes registers HOT 2
- unsafe URL HOT 1
- Numpy 1.25 breaks a few linalg functions HOT 1
- `autograd` 1.6 breaks Apple M-series macOS and Windows builds (module `numpy` has no attribute `float128`) HOT 3
- Can I differentiate this function?
- Python 2 and dependency on future HOT 2
- `'ArrayBox' object has no attribute 'dot'` when differentiating function containing `x.dot(y)` HOT 1
- How do I create a scalar value that does not depend on the independent variables ?
- AttributeError: module 'autograd.numpy' has no attribute 'numpy_extra'
- Autograd for quantum circuits
- [BUG] Differentiating `autograd.numpy.linalg.norm` gives incorrect results
- Support for advanced library based on autograd HOT 4
- Release new version 1.6.3 on PyPI
- autograd return nan with to norm function
- Saving `ArrayBox` to hdf5 file
- Incompatibility with numpy 2.0.0 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autograd.