Search code examples
pythontheano

Error using grad in theano


I am following a tutorial that shows how to implement logistic regression using Theano. The listed line is giving me an error. I don't know how to fix it.

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))

This is the error I am getting:

\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'

Solution

  • In line grad_err = function([W,TS,E],grad(e,W)) you want to compute gradient of error 'def_err' w.r.t 'W', but you are passing a function 'e' to grad(..) without the list of inputs, this will never work. Also please note that TS, W, E, O etc are tensor/symbolic variables which are general expressions and need to be provided with extra input to determine their value.

    I would recommend going through the following tutorial for logistic regression, If you have just started Theano then these tutorials will definitely help you to get started.

    This should work:

    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))