Basic question from Theano Tutorials

26 views
Skip to first unread message

Satyan Sharma

unread,
Nov 18, 2019, 7:00:07 AM11/18/19
to theano-users
Hello everyone,

I am going through Theano Tutorials (http://deeplearning.net/software/theano/tutorial/gradients.html). I have a basic doubt:
why the foloowing return an error :
In [28]: x = T.dmatrix('x')
...: y = x**2
...: gy = T.grad(y, x)
...: dy = theano.function([x], gy)
...:
...:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-28-74c00692575f> in <module>()
1 x = T.dmatrix('x')
2 y = x**2
----> 3 gy = T.grad(y, x)
4 dy = theano.function([x], gy)
5

py27_venvs/venv/local/lib/python2.7/site-packages/theano/gradient.pyc in grad(cost, wrt, consider_constant, disconnected_inputs, add_names, known_grads, return_disconnected, null_gradients)
485
486 if cost is not None and cost.ndim != 0:
--> 487 raise TypeError("cost must be a scalar.")
488
489 if isinstance(wrt, set):

while writing y = T.sum(x**2) gives me elementwise grad succesfully.
In [29]: x = T.dmatrix('x')
...: y = T.sum(x**2)
...: gy = T.grad(y, x)
...: dy = theano.function([x], gy)
...:

In [30]: dy([[0, 1], [-1, -2]])
Out[30]:
array([[ 0., 2.],
[-2., -4.]])

with regards,
Satti

Reply all
Reply to author
Forward
0 new messages