简体   繁体   中英

T.hessian gives NotImplementedError() in theano

How it can be that it works

    g_W = T.grad(cost=cost, wrt=classifier.vparamW) 

whereas this

    H_W=T.hessian(cost=cost, wrt=classifier.vparamW)

gives NotImplementedError() may it be that the problem in such cost function:

    -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]), y]) 

Here y is the vector of class labels from 0 to n-1 and

    self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b) 

I am unable to reproduce this problem with the limited code that has been provided. However, here is a fully working demo of T.grad and 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

Note that the wrt value for T.hessian needs to be a vector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM