Comments (2)
Yup!
Here's a test just to make sure I understand what you mean:
import autograd.numpy as np
from autograd import grad
from autograd.util import quick_grad_check
def some_function(x, y):
return x + y + x*y
class A(object):
def call(self, X):
self.ans = np.zeros(X.shape)
for t in range(X.shape[0]):
self.ans = some_function(X[t], self.ans)
return np.sum(self.ans**2)
a = A()
print grad(a.call)(np.random.randn(5))
quick_grad_check(a.call, np.random.randn(5))
One thing to keep in mind is that ans
will come out boxed at the end:
In [1]: run issue67
[ 323.38327233 185.12997922 283.66369298 168.29411013 807.31095824]
Checking gradient of <bound method A.call of <__main__.A object at 0x10d2d9610>> at [-0.10688095 0.66919977 -0.45675244 -1.08241973 -0.91352716]
Gradient projection OK (numeric grad: -0.105982859395, analytic grad: -0.105982859699)
In [2]: print a.ans
Autograd ArrayNode with value [-1.005772 -1.005772 -1.005772 -1.005772 -1.005772] and 1 tape(s)
but its computation tape is completed and so it will act just like a regular array.
The code also works if the updated value of self.ans
gets reused in future calls to call
instead of getting reset to zeros like in the example I wrote. That just means the function changes every time you call it, which autograd can handle but quick_grad_check
can't (because it invokes the function multiple times to check its numerical gradient):
class A(object):
def __init__(self, ans):
self.ans = ans
def call(self, X):
for t in range(X.shape[0]):
self.ans = some_function(X[t], self.ans)
return np.sum(self.ans**2)
a = A(5.)
print grad(a.call)(np.random.randn(3))
print grad(a.call)(np.random.randn(3))
In [1]: run issue67
[ 8.91513093 60.30447614 -32.37465155]
[ 2.43210897 17.48004748 5.28757384]
from autograd.
Thanks!
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 2
- 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 HOT 3
- autograd return nan with to norm function
- Saving `ArrayBox` to hdf5 file
- Incompatibility with numpy 2.0.0 HOT 3
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.