它是如何工作的
g_W = T.grad(cost=cost, wrt=classifier.vparamW)
而这个
H_W=T.hessian(cost=cost, wrt=classifier.vparamW)
给出NotImplementedError() 可能是这样的成本函数的问题:
-T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]), y])
这里 y 是从 0 到 n-1 的类标签向量,
self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b)
最佳答案
我无法使用所提供的有限代码重现此问题。不过,这里是 T.grad
和 T.hessian
的完整工作演示。
import numpy
import theano
import theano.tensor as T
x = T.matrix()
w_flat = theano.shared(numpy.random.randn(3, 2).astype(theano.config.floatX).flatten())
w = w_flat.reshape((3, 2))
cost = T.pow(theano.dot(x, w), 2).sum()
g_w = T.grad(cost=cost, wrt=[w])
h_w = T.hessian(cost=cost, wrt=[w_flat])
f = theano.function([x], outputs=g_w + h_w)
for output in f(numpy.random.randn(4, 3).astype(theano.config.floatX)):
print output.shape, '\n', output
请注意,T.hessian
的 wrt
值需要是一个向量。
关于python - T.hessian 在 theano 中给出 NotImplementedError(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33503025/