代码1:(求雅可比矩阵, jacobian矩阵求解)
import theano
from theano import tensor
# Creating a vector
x = tensor.dvector('x')
# Creating 'y' expression
y = (2 * x ** 3)
# Computing derivative
Output, updates = theano.scan(lambda i, y, x : tensor.grad(y[i], x),\
sequences=tensor.arange(y.shape[0]),\
non_sequences=[y, x])
# Creating function
# fun = theano.function([x], Output, updates=updates)
fun = theano.function([x], Output)
# Calling function
print( fun([3,3]) )
运行结果:
代码2:(求黑森矩阵, Hession矩阵求解)
import theano
from theano import tensor
# Creating a vector
x = tensor.dvector('x')
# Creating 'y' expression
y = (2 * x ** 3)
# Calculating cost
cost = y.sum()
# Computing derivative
derivative = tensor.grad(cost, x)
output, updates = theano.scan(lambda i, derivative,x : \
tensor.grad(derivative[i], x),\
sequences=tensor.arange(derivative.shape[0]),\
non_sequences=[derivative, x])
# Creating function
# fun = theano.function([x], output, updates=updates)
fun = theano.function([x], output)
# Calling function
print( fun([3,3]) )
运行结果:
标签:function,tensor,框架,示例,grandfather,updates,derivative,theano,Creating From: https://www.cnblogs.com/devilmaycry812839668/p/18014507