简体   繁体   中英

Gradient using autograd function error python

I am trying to compute the gradient of some function using autograd but fails and shows the below error

from autograd import grad
def f(a): 
   return a[0]*np.sin(2*np.pi*a[1]) + a[2]*np.sin(2*np.pi*a[3])

a=[1.0,1.0,1.0,1.0] 
gr = grad(f,0)
print(gr(a))

File "C:\\Users\\user\\Desktop\\auto.py", line 23, in f return a[0]*np.sin(2*np.pi*a[1]) + a[2]*np.sin(2*np.pi*a[3])

TypeError: loop of ufunc does not support argument 0 of type ArrayBox which has no callable sin method

I had the same issue. I think this a problem with autograd. You have to import numpy from autograd! Add at the beginning : import autograd.numpy as np

from autograd import grad
import autograd.numpy as np ; <-----------

def f(a): 
   return a[0]*np.sin(2*np.pi*a[1]) + a[2]*np.sin(2*np.pi*a[3])

a=[1.0,1.0,1.0,1.0] 
gr = grad(f,0)
print(gr(a))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM