Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grad(odeint) #563

Open
tesla-cat opened this issue Oct 30, 2020 · 1 comment
Open

grad(odeint) #563

tesla-cat opened this issue Oct 30, 2020 · 1 comment

Comments

@tesla-cat
Copy link

def intFunc(x):
    def func(y, t):
        return np.sin( x * t )
    y0 = 0.
    t = [0., 1.]
    y = odeint(func, y0, t)[-1]
    return y
print(intFunc(0.5))
intFuncGrad = grad(intFunc)
print(intFuncGrad(1.0))
  • error:
[0.24483481]
TypeError: float() argument must be a string or a number, not 'ArrayBox'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:/Users/e0134117/Desktop/qoc/1 Implementations/AQUA/HIPS-autograd/basic-tests.py", line 16, in <module>
    print(intFuncGrad(1.0))
  File "C:\Python38\lib\site-packages\autograd\wrap_util.py", line 20, in nary_f
    return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
  File "C:\Python38\lib\site-packages\autograd\differential_operators.py", line 25, in grad
    vjp, ans = _make_vjp(fun, x)
  File "C:\Python38\lib\site-packages\autograd\core.py", line 10, in make_vjp
    end_value, end_node =  trace(start_node, fun, x)
  File "C:\Python38\lib\site-packages\autograd\tracer.py", line 10, in trace
    end_box = fun(start_box)
  File "C:\Python38\lib\site-packages\autograd\wrap_util.py", line 15, in unary_f
    return fun(*subargs, **kwargs)
  File "c:/Users/e0134117/Desktop/qoc/1 Implementations/AQUA/HIPS-autograd/basic-tests.py", line 12, in intFunc
    y = odeint(func, y0, t)[-1]
  File "C:\Python38\lib\site-packages\autograd\tracer.py", line 48, in f_wrapped
    return f_raw(*args, **kwargs)
  File "C:\Python38\lib\site-packages\scipy\integrate\odepack.py", line 242, in odeint
    output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
ValueError: setting an array element with a sequence.
@hannesmeinlschmidt
Copy link

hannesmeinlschmidt commented Jul 18, 2022

It seems that autograd requires that the parameter in the ODE with respect to which you want the derivative must be passed in odeint as a parameter:

import autograd.numpy as np
from autograd.scipy.integrate import odeint
from autograd import grad
from autograd.builtins import tuple

def intFunc(x):
    def func(y, t, alpha):
        return np.sin(alpha * t)
    y0 = 0.
    t = np.linspace(0., 1., 10)
    y = odeint(func, y0, t, tuple([x]))[-1]
    return y

print(intFunc(0.5))
intFuncGrad = grad(intFunc)
print(intFuncGrad(1.0))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@tesla-cat @hannesmeinlschmidt and others