python - T.hessian 在 theano 中给出 NotImplementedError()

标签 python 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) 

最佳答案

我无法使用所提供的有限代码重现此问题。不过,这里是 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 值需要是一个向量。

关于python - T.hessian 在 theano 中给出 NotImplementedError(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33503025/

相关文章:

python - 当 pandas DataFrame 有层次索引时选择一个单元格

python - 是否有一种可接受的方法让函数从参数中弹出一个值?

python - Theano,类型错误: Unknown parameter type: <class 'tensorflow.python.framework.ops.Tensor' >

python - 我不明白有什么问题 InvalidArgumentError : Conv2DCustomBackpropInputOp only supports NHWC

python - Windows 缺少 Python.h

python - 从 PyCharm 的根目录导入 python 模块时遇到问题

python - 在一系列条件中获取列表内的值

python - Flask 未在数据库中插入值

python - 尝试从训练模型中获取 val_loss

python - 更新 theano 函数中的参数