提交 c4147fa5 authored 作者: Iban Harlouchet's avatar Iban Harlouchet 提交者: Arnaud Bergeron

testcode for doc/tutorial/debug_faq.txt

上级 762cb0d1
......@@ -23,7 +23,7 @@ Interpreting Error Messages
Even in its default configuration, Theano tries to display useful error
messages. Consider the following faulty code.
.. code-block:: python
.. testcode::
import numpy as np
import theano
......@@ -38,7 +38,7 @@ messages. Consider the following faulty code.
Running the code above we see:
.. code-block:: bash
.. testoutput::
Traceback (most recent call last):
File "test0.py", line 10, in <module>
......@@ -71,7 +71,7 @@ the faulty line, while ``exception_verbosity=high`` will display a
debugprint of the apply node. Using these hints, the end of the error
message becomes :
.. code-block:: bash
.. testoutput::
Backtrace when the node is created:
File "test0.py", line 8, in <module>
......@@ -101,7 +101,7 @@ following example. Here, we use ``exception_verbosity=high`` and
``optimizer=None`` would and it could therefore be used instead of test values.
.. code-block:: python
.. testcode:: testvalues
import numpy
import theano
......@@ -137,7 +137,7 @@ following example. Here, we use ``exception_verbosity=high`` and
Running the above code generates the following error message:
.. code-block:: bash
.. testoutput:: testvalues
Traceback (most recent call last):
File "test1.py", line 31, in <module>
......@@ -166,7 +166,7 @@ Running the above code generates the following error message:
If the above is not informative enough, by instrumenting the code ever
so slightly, we can get Theano to reveal the exact source of the error.
.. code-block:: python
.. testcode:: testvalues
# enable on-the-fly graph computations
theano.config.compute_test_value = 'warn'
......@@ -185,7 +185,7 @@ of error can thus be identified with much more precision and much earlier in
the compilation pipeline. For example, running the above code yields the
following error message, which properly identifies *line 24* as the culprit.
.. code-block:: bash
.. testoutput:: testvalues
Traceback (most recent call last):
File "test2.py", line 24, in <module>
......@@ -228,7 +228,10 @@ The ``compute_test_value`` mechanism works as follows:
Theano provides a 'Print' op to do this.
.. code-block:: python
.. testcode::
import numpy
import theano
x = theano.tensor.dvector('x')
......@@ -243,6 +246,9 @@ Theano provides a 'Print' op to do this.
#this runs the graph with the message, and value printed
assert numpy.all( f_with_print([1, 2, 3]) == [5, 10, 15])
.. testoutput::
this is a very important value __str__ = [ 1. 2. 3.]
Since Theano runs your program in a topological order, you won't have precise
control over the order in which multiple ``Print()`` ops are evaluted. For a more
......@@ -324,7 +330,7 @@ You can use ``MonitorMode`` to inspect the inputs and outputs of each
node being executed when the function is called. The code snipped below
shows how to print all inputs and outputs:
.. code-block:: python
.. testcode::
import theano
......@@ -341,8 +347,9 @@ shows how to print all inputs and outputs:
post_func=inspect_outputs))
f(3)
# The code will print the following:
# 0 Elemwise{mul,no_inplace}(TensorConstant{5.0}, x) input(s) value(s): [array(5.0), array(3.0)] output(s) value(s): [array(15.0)]
.. testoutput::
0 Elemwise{mul,no_inplace}(TensorConstant{5.0}, x) input(s) value(s): [array(5.0), array(3.0)] output(s) value(s): [array(15.0)]
When using these ``inspect_inputs`` and ``inspect_outputs`` functions
with ``MonitorMode``, you should see [potentially a lot of] printed output.
......@@ -357,7 +364,7 @@ position, or only if a particular value showed up in one of the inputs or output
A typical example is to detect when NaN values are added into computations, which
can be achieved as follows:
.. code-block:: python
.. testcode:: compiled
import numpy
......@@ -385,12 +392,13 @@ can be achieved as follows:
post_func=detect_nan))
f(0) # log(0) * 0 = -inf * 0 = NaN
# The code above will print:
# *** NaN detected ***
# Elemwise{Composite{[mul(log(i0), i0)]}} [@A] ''
# |x [@B]
# Inputs : [array(0.0)]
# Outputs: [array(nan)]
.. testoutput:: compiled
*** NaN detected ***
Elemwise{Composite{(log(i0) * i0)}} [@A] ''
|x [@B]
Inputs : [array(0.0)]
Outputs: [array(nan)]
To help understand what is happening in your graph, you can
disable the ``local_elemwise_fusion`` and all ``inplace``
......@@ -402,12 +410,12 @@ will not be able to see the input that was overwriten in the ``post_func``
function. To disable those optimizations (with a Theano version after
0.6rc3), define the MonitorMode like this:
.. code-block:: python
.. testcode:: compiled
mode = theano.compile.MonitorMode(post_func=detect_nan).excluding(
'local_elemwise_fusion', 'inplace)
f = theano.function([x], [theano.tensor.log(x) * x],
mode=mode)
'local_elemwise_fusion', 'inplace')
f = theano.function([x], [theano.tensor.log(x) * x],
mode=mode)
.. note::
......@@ -422,7 +430,7 @@ the execution of the node can garbage collect its inputs that aren't
needed anymore by the Theano function. This can be done with the Theano
flag:
.. code-block:: cfg
.. testcode:: compiled
allow_gc=False
......@@ -443,7 +451,7 @@ functions.
Consider this example script ("ex.py"):
.. code-block:: python
.. testcode::
import theano
import numpy
......@@ -464,7 +472,7 @@ This is actually so simple the debugging could be done easily, but it's for
illustrative purposes. As the matrices can't be multiplied element-wise
(unsuitable shapes), we get the following exception:
.. code-block:: text
.. testoutput::
File "ex.py", line 14, in <module>
f(mat1, mat2)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论