2016-03-05 89 views
0

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

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

在行grad_err = function([W,TS,E],grad(e,W))要计算错误“def_err”的梯度WRT“W”,但你传递一个函数“E”来没有输入列表的毕业生(..),这将永远不会工作。 另请注意,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))