我正在尝试使用ad
我知道 y(x) 和 z(y) 的库来区分 z(x) wrt x。如果我无法通过分析找到 z(x),我该如何进行微分?换句话说,我试图避免如下所示的链式规则计算:
from ad import gh
def y(x):
return 2*x
def z(y):
return 3*y
dzdy,hy = gh(z)
dydx,hz = gh(y)
x0 = 0 # does not matter for this example
dydx_x0 = dydx(x0)
y0 = y(x0)
dzdy_y0 = dzdy(y0)
dzdx_x0 = dzdy_y0[0] * dydx_x0[0]
print(dzdx_x0) # dz/dx = dz/dy*dy/dx = 3*2 = 6