提交 1a1ea829 authored 作者: Frederic's avatar Frederic

Added the infer_shape and grad method in the example Op implementation.

上级 2d1e2a9c
...@@ -127,17 +127,41 @@ Op example ...@@ -127,17 +127,41 @@ Op example
z = output_storage[0] z = output_storage[0]
z[0] = x * 2 z[0] = x * 2
def infer_shape(self, node, i0_shapes):
return i0_shapes
def grad(self, inputs, output_grads):
return [output_grads[0]*2]
Test it! Test it!
>>> x = theano.tensor.matrix() .. code-block:: python
>>> f = theano.function([x],DoubleOp()(x))
>>> import numpy x = theano.tensor.matrix()
>>> inp = numpy.random.rand(5,5) f = theano.function([x],DoubleOp()(x))
>>> out = f(inp) import numpy
>>> assert numpy.allclose(inp*2, out) inp = numpy.random.rand(5,5)
>>> print inp out = f(inp)
>>> print out assert numpy.allclose(inp*2, out)
print inp
print out
Testing the gradient
--------------------
The function :ref:`verify_grad
<validating_grad>`
verify the gradient of an Op or Theano graph. It compare the
analytic(symbolicaly computed) gradient and the numeric
gradient(computed through the Finite Difference Method).
To verify the grad method of the DoubleOp, you can use this:
.. code-block:: python
import numpy
import theano.tests
theano.tests.unittest_tools.verify_grad(DoubleOp(), [numpy.random.rand(5,7,2)])
Exercises 8 Exercises 8
----------- -----------
......
...@@ -374,6 +374,8 @@ A few tools have been developed to help automate the development of ...@@ -374,6 +374,8 @@ A few tools have been developed to help automate the development of
unitests for Theano Ops. unitests for Theano Ops.
.. _validating_grad:
Validating the Gradient Validating the Gradient
----------------------- -----------------------
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论