2016-03-05 87 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))