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