Giter VIP home page Giter VIP logo

Comments (2)

mattjj avatar mattjj commented on August 28, 2024

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.

pranv avatar pranv commented on August 28, 2024

Thanks!

from autograd.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.