在theano中使用grad的错误

时间:2016-03-05 07:09:22

标签: python theano

我正在学习如何使用Theano实现逻辑回归的教程。列出的行给了我一个错误。我不知道如何解决它。

from theano import tensor
TS = tensor.matrix('training-set')
W = tensor.matrix('weights')
E = tensor.matrix('expected')
O = tensor.dot(TS,W)
def_err = ((E-O)**2).sum()
e = function([W,TS,E],def_err)
grad_err = function([W,TS,E],grad(e,W))

这是我得到的错误:

\in grad(cost, wrt, consider_constant, disconnected_inputs, add_names, known_grads, return_disconnected, null_gradients)
    428             raise AssertionError("cost and known_grads can't both be None.")
    429 
--> 430     if cost is not None and isinstance(cost.type, NullType):
    431         raise ValueError("Can't differentiate a NaN cost."
    432                          "cost is NaN because " +

AttributeError: 'Function' object has no attribute 'type'

1 个答案:

答案 0 :(得分:1)

在行grad_err = function([W,TS,E],grad(e,W))中,您想要计算错误的梯度' def_err' w.r.t' W'但你传递的功能是' e'没有输入列表的grad(..),这将永远不会工作。 另请注意,TS,W,E,O等是张量/符号变量,它们是一般表达式,需要提供额外的输入来确定它们的值。

我建议您浏览following tutorial进行逻辑回归,如果您刚开始Theano,那么these tutorials肯定会帮助您开始。

这应该有效:

from theano import tensor, function, grad

TS = tensor.matrix('training-set')
W = tensor.matrix('weights')
E = tensor.matrix('expected')
O = tensor.dot(TS,W)
def_err = ((E-O)**2).sum()
e = function([W,TS,E],def_err)
grad_err = function([W,TS,E],grad(def_err,W))