2015-11-03 47 views
1

给NotImplementedError()这怎么可能,它的作品T.hessian在theano

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) 

回答

1

我无法用已提供的有限代码重现此问题。然而,这里是一个完整的工作演示T.gradT.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.hessianwrt该值必须是一个矢量。