提交 74253a36 authored 作者: Frederic's avatar Frederic

fix and better explaination in the doc by code review.

上级 ecdbcd92
...@@ -158,17 +158,17 @@ Try it! ...@@ -158,17 +158,17 @@ Try it!
How to test it How to test it
-------------- --------------
Theano has some functions to simplify testing. Those help test the Theano has some functions to simplify testing. These help test the
infer_shape, grad and R_op method. Put the following code in a file ``infer_shape``, ``grad`` and ``R_op`` methods. Put the following code
and execute with the `nosetests` program to run it. in a file and execute it with the ``nosetests`` program.
Basic tests Basic tests
=========== ===========
Basic tests are done by you just by using the Op and checking it Basic tests are done by you just by using the Op and checking that it
return the right answer. If you detect an error, you must raise an return the right answer. If you detect an error, you must raise an
exception. You can use the `assert` keyword to raise automatically an exception. You can use the `assert` keyword to automatically raise an
AssertionError. ``AssertionError``.
.. code-block:: python .. code-block:: python
...@@ -185,25 +185,29 @@ AssertionError. ...@@ -185,25 +185,29 @@ AssertionError.
f = theano.function([x], self.op(x)) f = theano.function([x], self.op(x))
inp = numpy.asarray(numpy.random.rand(5, 4), dtype=config.floatX) inp = numpy.asarray(numpy.random.rand(5, 4), dtype=config.floatX)
out = f(inp) out = f(inp)
# Compare to result computed to the expected value. # Compare the result computed to the expected value.
assert numpy.allclose(inp * 2, out) assert numpy.allclose(inp * 2, out)
Testing the infer_shape Testing the infer_shape
======================= =======================
When a class inherits from the InferShapeTester class, it gets the When a class inherits from the ``InferShapeTester`` class, it gets the
`self._compile_and_check` method that tests the Op infer_shape `self._compile_and_check` method that tests the Op ``infer_shape``
method. It checks if the optimized graph obtained gives the correct method. It tests that the Op gets optimized out of the graph if only
values. It also tests that the Op gets removed from the the shape of the output is needed and not the output
graph. `self._compile_and_check` compiles a Theano function. So it itself. Additionally, it checks that such an optimized graph computes
takes as parameter the list of inputs and outputs Theano variable the correct shape, by comparing it to the actual shape of the computed
that is passed to `theano.function`. Then output.
with a list of real values to pass the the compiled function (don't
use symmetric shape!). It also takes the class op to verify that `self._compile_and_check` compiles a Theano function. It takes as
it get removed from the graph. parameters the lists of input and output Theano variables, as would be
provided to theano.function, and a list of real values to pass to the
compiled function (don't use shapes that are symmetric, e.g. (3, 3),
as they can easily to hide errors). It also takes the Op class to
verify that no Ops of that type appear in the shape-optimized graph.
If there is an error, the function raises an exception. If you want to If there is an error, the function raises an exception. If you want to
see it fail, you can implement a wrong infer_shape. see it fail, you can implement an incorrect ``infer_shape``.
.. code-block:: python .. code-block:: python
...@@ -227,7 +231,7 @@ analytic (symbolically computed) gradient and the numeric ...@@ -227,7 +231,7 @@ analytic (symbolically computed) gradient and the numeric
gradient (computed through the Finite Difference Method). gradient (computed through the Finite Difference Method).
If there is an error, the function raises an exception. If you want to If there is an error, the function raises an exception. If you want to
see it fail, you can implement a wrong gradient (for instance removing see it fail, you can implement an incorrect gradient (for instance, by removing
the multiplication by 2). the multiplication by 2).
.. code-block:: python .. code-block:: python
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论