提交 f601a0ff authored 作者: Razvan Pascanu's avatar Razvan Pascanu

Added comments pointing to the file/class/function where this code snippets

are tested.
上级 24ccfb93
......@@ -91,6 +91,9 @@ We will be defining C code for the multiplication Op on doubles.
**c_code**
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2
.. code-block:: python
def c_code(node, name, input_names, output_names, sub):
......@@ -134,6 +137,9 @@ repetition. You can check that mul produces the same C code in this
version that it produces in the code I gave above.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2
.. code-block:: python
from theano import gof
......
......@@ -123,6 +123,8 @@ out:
Defining the methods
====================
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2
**c_declare**
.. code-block:: python
......@@ -153,6 +155,9 @@ your Type. If you wish people to develop operations that make use of
it, it's best to publish it somewhere.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2
**c_init**
.. code-block:: python
......@@ -179,6 +184,9 @@ you should only assume that either ``c_init`` or ``c_extract`` has been
called, without knowing for sure which of the two.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2
**c_extract**
.. code-block:: python
......@@ -215,6 +223,8 @@ using the ``PyFloat_AsDouble`` function (yet again provided by CPython's C
API) and we put it in our double variable that we declared previously.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2
**c_sync**
.. code-block:: python
......@@ -274,6 +284,9 @@ than sorry.
do *NOT* decrease its reference count!
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2
**c_cleanup**
.. code-block:: python
......@@ -322,6 +335,9 @@ depends on the the relationship between Python and C with respect to
that Variable. For instance, imagine you define the following function
and call it:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2
.. code-block:: python
x, y, z = double('x'), double('y'), double('z')
......@@ -405,6 +421,9 @@ multiplication block.
Final version
=============
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2
.. code-block:: python
from theano import gof
......
......@@ -26,6 +26,9 @@ clarity. For example, when you write C code that assumes memory is contiguous,
you should check the strides and alignment.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_fibby.test_fibby_1
.. code-block:: python
class Fibby(theano.Op):
......@@ -191,6 +194,9 @@ for the old output.
TODO: talk about OPTIMIZATION STAGES
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_fibby.test_fibby_1
.. code-block:: python
from theano.tensor.opt import get_constant_value
......
......@@ -69,6 +69,9 @@ without any shortcuts, that will make the graph construction very explicit.
This is what you would normally type:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_graphstructures.test_graphstructures_1
.. code-block:: python
from theano.tensor import *
......@@ -87,6 +90,9 @@ This is what you would normally type:
This is what you would type to build the graph explicitly:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_graphstructures.test_graphstructures_1
.. code-block:: python
from theano.tensor import *
......
......@@ -170,6 +170,9 @@ multiplication Op could take an arbitrary number of arguments.
First, we'll instantiate a ``mul`` Op:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_1
.. code-block:: python
from theano import gof
......@@ -187,6 +190,9 @@ Since multiplying two doubles yields a double,
this function makes an Apply node with an output Variable of type
``double``.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_1
.. code-block:: python
def make_node(x, y):
......@@ -219,6 +225,8 @@ built-in type ``float`` because this is the type that ``double.filter()``
will always return, per our own definition. ``output_storage`` will
contain a single storage cell for the multiplication's variable.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_1
.. code-block:: python
def perform(node, inputs, output_storage):
......@@ -247,6 +255,8 @@ Here, ``z`` is a list of one element. By default, ``z == [None]``.
Trying out our new Op
=====================
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_1
In the following code, we use our new Op:
>>> x, y = double('x'), double('y')
......@@ -276,6 +286,8 @@ Well, OK. We'd like our Op to be a bit more flexible. This can be done
by modifying ``make_node`` to accept Python ``int`` or ``float`` as
``x`` and/or ``y``:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_1
.. code-block:: python
def make_node(x, y):
......@@ -292,6 +304,9 @@ Whenever we pass a Python int or float instead of a Variable as ``x`` or
``y``, ``make_node`` will convert it to :ref:`constant` for us. ``gof.Constant``
is a :ref:`variable` we statically know the value of.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_op.test_op_1
>>> x = double('x')
>>> z = mul(x, 2)
>>> f = theano.function([x], z)
......@@ -319,6 +334,9 @@ operations ``add``, ``sub`` and ``div``, code for ``make_node`` can be
shared between these Ops. Here is revised implementation of these four
arithmetic operators:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_1
.. code-block:: python
from theano import gof
......
......@@ -115,6 +115,9 @@ Global optimization
Here is the code for a global optimization implementing the
simplification described above:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2
.. code-block:: python
from theano.gof import toolbox
......@@ -173,6 +176,9 @@ pointer-following game you need to get ahold of the nodes of interest
for the simplification (``x``, ``y``, ``z``, ``a``, ``b``, etc.).
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2
Test time:
>>> x = double('x')
......@@ -239,6 +245,9 @@ Local optimization
The local version of the above code would be the following:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2
.. code-block:: python
......@@ -275,6 +284,9 @@ with a :ref:`navigator`. Basically, a :ref:`navigator` is a global
optimizer that loops through all nodes in the graph (or a well-defined
subset of them) and applies one or several local optimizers on them.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2
>>> x = double('x')
>>> y = double('y')
>>> z = double('z')
......
......@@ -107,6 +107,8 @@ must define ``filter`` and shall override ``values_eq_approx``.
**filter**
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_1
.. code-block:: python
# Note that we shadow Python's function ``filter`` with this
......@@ -160,6 +162,8 @@ contract. Recall that Type defines default implementations for all
required methods of the interface, except ``filter``. One way to make
the Type is to instantiate a plain Type and set the needed fields:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_1
.. code-block:: python
from theano import gof
......@@ -172,6 +176,8 @@ the Type is to instantiate a plain Type and set the needed fields:
Another way to make this Type is to make a subclass of ``gof.Type``
and define ``filter`` and ``values_eq_approx`` in the subclass:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_1
.. code-block:: python
from theano import gof
......@@ -209,6 +215,8 @@ There are several ways to make sure that equality testing works properly:
#. Define ``Double.__eq__`` so that instances of type Double
are equal. For example:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_1
.. code-block:: python
def __eq__(self, other):
......@@ -262,6 +270,8 @@ attempt to clear up the confusion:
Final version
=============
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_1
.. code-block:: python
from theano import gof
......
......@@ -49,6 +49,10 @@ Sneak peek
Here is an example of how to use Theano. It doesn't show off many of
Theano's features, but it illustrates concretely what Theano is.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_introduction.test_introduction_1
.. code-block:: python
import theano
......
......@@ -12,6 +12,11 @@ So, to get us started with Theano and get a feel of what we're working with,
let's make a simple function: add two numbers together. Here is how you do
it:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_adding.test_adding_1
>>> import theano.tensor as T
>>> from theano import function
>>> x = T.dscalar('x')
>>> y = T.dscalar('y')
>>> z = x + y
......@@ -125,6 +130,9 @@ You might already have guessed how to do this. Indeed, the only change
from the previous example is that you need to instantiate ``x`` and
``y`` using the matrix Types:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_adding.test_adding_2
>>> x = T.dmatrix('x')
>>> y = T.dmatrix('y')
>>> z = x + y
......
......@@ -56,6 +56,10 @@ Borrowing when creating shared variables
A ``borrow`` argument can be provided to the shared-variable constructor.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_aliasing.test_aliasing_1
.. code-block:: python
import numpy, theano
......@@ -112,6 +116,10 @@ Retrieving
A ``borrow`` argument can also be used to control how a shared variable's value is retrieved.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_aliasing.test_aliasing_2
.. code-block:: python
s = theano.shared(np_array)
......@@ -201,6 +209,9 @@ Borrowing when constructing Function objects
A ``borrow`` argument can also be provided to the ``In`` and ``Out`` objects
that control how ``theano.function`` handles its arguments and return value[s].
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_aliasing.test_aliasing_3
.. code-block:: python
import theano, theano.tensor
......
......@@ -28,6 +28,10 @@ individual element of the matrix.
Well, what you do is this:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_examples.test_examples_1
>>> x = T.dmatrix('x')
>>> s = 1 / (1 + T.exp(-x))
>>> logistic = function([x], s)
......@@ -47,6 +51,8 @@ It is also the case that:
We can verify that this alternate form produces the same values:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_examples.test_examples_2
>>> s2 = (1 + T.tanh(x / 2)) / 2
>>> logistic2 = function([x], s2)
>>> logistic2([[0, 1], [-1, -2]])
......@@ -61,6 +67,8 @@ Theano supports functions with multiple outputs. For example, we can
compute the :ref:`elementwise <libdoc_tensor_elementwise>` difference, absolute difference, and
squared difference between two matrices ``a`` and ``b`` at the same time:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_examples.test_examples_3
>>> a, b = T.dmatrices('a', 'b')
>>> diff = a - b
>>> abs_diff = abs(diff)
......@@ -95,6 +103,10 @@ gradient of :math:`x^2` with respect to :math:`x`. Note that:
Here is code to compute this gradient:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_examples.test_examples_4
>>> from theano import pp
>>> x = T.dscalar('x')
>>> y = x**2
>>> gy = T.grad(y, x)
......@@ -132,6 +144,10 @@ logistic is: :math:`ds(x)/dx = s(x) \cdot (1 - s(x))`.
A plot of the gradient of the logistic function, with x on the x-axis
and :math:`ds(x)/dx` on the y-axis.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_examples.test_examples_5
>>> x = T.dmatrix('x')
>>> s = 1 / (1 + T.exp(-x))
>>> gs = T.grad(s, x)
......@@ -165,6 +181,10 @@ Let's say you want to define a function that adds two numbers, except
that if you only provide one number, the other input is assumed to be
one. You can do it like this:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_examples.test_examples_6
>>> from theano import Param
>>> x, y = T.dscalars('x', 'y')
>>> z = x + y
>>> f = function([x, Param(y, default=1)], z)
......@@ -182,6 +202,10 @@ Inputs with default values must follow inputs without default
values (like python's functions). There can be multiple inputs with default values. These parameters can
be set positionally or by name, as in standard Python:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_examples.test_examples_7
>>> x, y, w = T.dscalars('x', 'y', 'w')
>>> z = (x + y) * w
>>> f = function([x, Param(y, default=1), Param(w, default=2, name='w_by_name')], z)
......@@ -219,6 +243,10 @@ is incremented by the function's argument.
First let's define the ``accumulator`` function. It adds its argument to the
internal state, and returns the old state value.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_examples.test_examples_8
>>> from theano import shared
>>> state = shared(0)
>>> inc = T.iscalar('inc')
>>> accumulator = function([inc], state, updates=[(state, state+inc)])
......@@ -241,6 +269,9 @@ of the state and the increment amount.
Anyway, let's try it out!
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_examples.test_examples_8
>>> state.value
array(0)
>>> accumulator(1)
......@@ -263,6 +294,9 @@ array(2)
As we mentioned above, you can define more than one function to use the same
shared variable. These functions can both update the value.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_examples.test_examples_8
>>> decrementor = function([inc], state, updates=[(state, state-inc)])
>>> decrementor(2)
array(2)
......@@ -283,6 +317,9 @@ you do *not* want to use its value. In this case, you can use the
``givens`` parameter of ``function`` which replaces a particular node in a graph
for the purpose of one particular function.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_examples.test_examples_8
>>> fn_of_state = state * 2 + inc
>>> foo = lscalar() # the type (lscalar) must match the shared variable we
>>> # are replacing with the ``givens`` list
......@@ -326,6 +363,9 @@ Brief example
Here's a brief example. The setup code is:
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_examples.test_examples_9
.. code-block:: python
from theano.tensor.shared_randomstreams import RandomStreams
......
......@@ -30,6 +30,9 @@ The basics of pickling
The two modules ``pickle`` and ``cPickle`` have the same functionalities, but
``cPickle``, coded in C, is much faster.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_loading_and_saving.test_loading_and_saving_3
>>> import cPickle
You can serialize (or *save*, or *pickle*) objects to a file with
......
......@@ -54,6 +54,8 @@ use it only during development (not when you launch 1000 process on a
cluster!).
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_modes.test_modes_1
DebugMode is used as follows:
.. code-block:: python
......
......@@ -67,6 +67,9 @@ Putting it all Together
To see if your GPU is being used, cut and paste the following program into a
file and run it.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_using_gpu.test_using_gpu_1
.. code-block:: python
from theano import function, config, shared, sandbox
......@@ -120,6 +123,9 @@ the graph to express a computation with a GPU-stored result. The gpu_from_host
Op means "copy the input from the host to the gpu" and it is optimized away
after the T.exp(x) is replaced by a GPU version of exp().
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_using_gpu.test_using_gpu_2
.. code-block:: python
from theano import function, config, shared, sandbox
......@@ -171,6 +177,8 @@ that it has the un-wanted side-effect of really slowing things down.
The story here about copying and working buffers is misleading and potentially not correct
... why exactly does borrow=True cut 75% of the runtime ???
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_using_gpu.test_using_gpu_3
.. code-block:: python
from theano import function, config, shared, sandbox, Out
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论