如何让计算机计算函数的导数值?
现在,让我们使用theano来做稍微更复杂的任务:创建一个函数,用来计算表达式y 关于它的参数x的导数。我们将会用到宏 T.grad 。例如,我们可以计算 关于 的梯度。注意:
https://www.cnblogs.com/qw12/p/6216377.html
https://www.cnblogs.com/shouhuxianjian/p/4590230.html
import theano.tensor as T
from theano import pp
from theano import function
x= T.dscalar('x')
y= x ** 2
gy= T.grad(y, x)
f= function([x], gy)
print(pp(gy))#((fill((x ** TensorConstant{2}), TensorConstant{1.0}) * TensorConstant{2}) * (x ** (TensorConstant{2} - TensorConstant{1})))
print(f(4))# 8.0
求的导数,只需将函数换一下。
import theano.tensor as T
from theano import pp
from theano import function
import numpy as np
x= T.dscalar('x')
# y= x ** 2
y = np.exp(2*x)
gy= T.grad(y, x)
f= function([x], gy)
print(pp(gy))#((fill((x ** TensorConstant{2}), TensorConstant{1.0}) * TensorConstant{2}) * (x ** (TensorConstant{2} - TensorConstant{1})))
print(f(1))# 8.0