在 Theano 中使用 Grad 时出错
Error using grad in 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'
在行 grad_err = function([W,TS,E],grad(e,W))
中,你想计算误差的梯度 'def_err' w.r.t 'W',但你正在将函数 'e' 传递给 grad(。 .) 如果没有输入列表,这将永远行不通。
另请注意,TS、W、E、O 等是 tensor/symbolic 变量,它们是一般表达式,需要提供额外的输入来确定它们的值。
我建议通读 following tutorial for logistic regression, If you have just started Theano then 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))
我正在学习介绍如何使用 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'
在行 grad_err = function([W,TS,E],grad(e,W))
中,你想计算误差的梯度 'def_err' w.r.t 'W',但你正在将函数 'e' 传递给 grad(。 .) 如果没有输入列表,这将永远行不通。
另请注意,TS、W、E、O 等是 tensor/symbolic 变量,它们是一般表达式,需要提供额外的输入来确定它们的值。
我建议通读 following tutorial for logistic regression, If you have just started Theano then 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))