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

testcode for doc/extending/optimization.txt

上级 ee43bd11
...@@ -113,7 +113,6 @@ We will implement it in three ways: using a global optimization, a ...@@ -113,7 +113,6 @@ We will implement it in three ways: using a global optimization, a
local optimization with a Navigator and then using the PatternSub local optimization with a Navigator and then using the PatternSub
facility. facility.
Global optimization Global optimization
------------------- -------------------
...@@ -123,8 +122,10 @@ simplification described above: ...@@ -123,8 +122,10 @@ simplification described above:
.. If you modify this code, also change : .. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2 .. theano/tests/test_tutorial.py:T_extending.test_extending_2
.. code-block:: python .. testcode::
import theano
from theano import gof
from theano.gof import toolbox from theano.gof import toolbox
class Simplify(gof.Optimizer): class Simplify(gof.Optimizer):
...@@ -132,7 +133,7 @@ simplification described above: ...@@ -132,7 +133,7 @@ simplification described above:
fgraph.attach_feature(toolbox.ReplaceValidate()) fgraph.attach_feature(toolbox.ReplaceValidate())
def apply(self, fgraph): def apply(self, fgraph):
for node in fgraph.toposort(): for node in fgraph.toposort():
if node.op == div: if node.op == true_div:
x, y = node.inputs x, y = node.inputs
z = node.outputs[0] z = node.outputs[0]
if x.owner and x.owner.op == mul: if x.owner and x.owner.op == mul:
...@@ -186,32 +187,33 @@ for the simplification (``x``, ``y``, ``z``, ``a``, ``b``, etc.). ...@@ -186,32 +187,33 @@ for the simplification (``x``, ``y``, ``z``, ``a``, ``b``, etc.).
Test time: Test time:
>>> x = double('x') >>> from theano.scalar import float64, add, mul, true_div
>>> y = double('y') >>> x = float64('x')
>>> z = double('z') >>> y = float64('y')
>>> a = add(z, mul(div(mul(y, x), y), div(z, x))) >>> z = float64('z')
>>> a = add(z, mul(true_div(mul(y, x), y), true_div(z, x)))
>>> e = gof.FunctionGraph([x, y, z], [a]) >>> e = gof.FunctionGraph([x, y, z], [a])
>>> e >>> e
[add(z, mul(div(mul(y, x), y), div(z, x)))] [add(z, mul(true_div(mul(y, x), y), true_div(z, x)))]
>>> simplify.optimize(e) >>> simplify.optimize(e)
>>> e >>> e
[add(z, mul(x, div(z, x)))] [add(z, mul(x, true_div(z, x)))]
Cool! It seems to work. You can check what happens if you put many Cool! It seems to work. You can check what happens if you put many
instances of :math:`\frac{xy}{y}` in the graph. Note that it sometimes instances of :math:`\frac{xy}{y}` in the graph. Note that it sometimes
won't work for reasons that have nothing to do with the quality of the won't work for reasons that have nothing to do with the quality of the
optimization you wrote. For example, consider the following: optimization you wrote. For example, consider the following:
>>> x = double('x') >>> x = float64('x')
>>> y = double('y') >>> y = float64('y')
>>> z = double('z') >>> z = float64('z')
>>> a = div(mul(add(y, z), x), add(y, z)) >>> a = true_div(mul(add(y, z), x), add(y, z))
>>> e = gof.FunctionGraph([x, y, z], [a]) >>> e = gof.FunctionGraph([x, y, z], [a])
>>> e >>> e
[div(mul(add(y, z), x), add(y, z))] [true_div(mul(add(y, z), x), add(y, z))]
>>> simplify.optimize(e) >>> simplify.optimize(e)
>>> e >>> e
[div(mul(add(y, z), x), add(y, z))] [true_div(mul(add(y, z), x), add(y, z))]
Nothing happened here. The reason is: ``add(y, z) != add(y, Nothing happened here. The reason is: ``add(y, z) != add(y,
z)``. That is the case for efficiency reasons. To fix this problem we z)``. That is the case for efficiency reasons. To fix this problem we
...@@ -221,8 +223,9 @@ computation, using the ``merge_optimizer`` defined in ...@@ -221,8 +223,9 @@ computation, using the ``merge_optimizer`` defined in
>>> from theano.gof.opt import merge_optimizer >>> from theano.gof.opt import merge_optimizer
>>> merge_optimizer.optimize(e) >>> merge_optimizer.optimize(e)
(0, 0.0001430511474609375, None, None, {}, 1, 0)
>>> e >>> e
[div(mul(*1 -> add(y, z), x), *1)] [true_div(mul(*1 -> add(y, z), x), *1)]
>>> simplify.optimize(e) >>> simplify.optimize(e)
>>> e >>> e
[x] [x]
...@@ -255,11 +258,11 @@ The local version of the above code would be the following: ...@@ -255,11 +258,11 @@ The local version of the above code would be the following:
.. theano/tests/test_tutorial.py:T_extending.test_extending_2 .. theano/tests/test_tutorial.py:T_extending.test_extending_2
.. code-block:: python .. testcode::
class LocalSimplify(gof.LocalOptimizer): class LocalSimplify(gof.LocalOptimizer):
def transform(self, node): def transform(self, node):
if node.op == div: if node.op == true_div:
x, y = node.inputs x, y = node.inputs
if x.owner and x.owner.op == mul: if x.owner and x.owner.op == mul:
a, b = x.owner.inputs a, b = x.owner.inputs
...@@ -295,18 +298,18 @@ subset of them) and applies one or several local optimizers on them. ...@@ -295,18 +298,18 @@ subset of them) and applies one or several local optimizers on them.
.. If you modify this code, also change : .. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_extending.test_extending_2 .. theano/tests/test_tutorial.py:T_extending.test_extending_2
>>> x = double('x') >>> x = float64('x')
>>> y = double('y') >>> y = float64('y')
>>> z = double('z') >>> z = float64('z')
>>> a = add(z, mul(div(mul(y, x), y), div(z, x))) >>> a = add(z, mul(true_div(mul(y, x), y), true_div(z, x)))
>>> e = gof.FunctionGraph([x, y, z], [a]) >>> e = gof.FunctionGraph([x, y, z], [a])
>>> e >>> e
[add(z, mul(div(mul(y, x), y), div(z, x)))] [add(z, mul(true_div(mul(y, x), y), true_div(z, x)))]
>>> simplify = gof.TopoOptimizer(local_simplify) >>> simplify = gof.TopoOptimizer(local_simplify)
>>> simplify.optimize(e) >>> simplify.optimize(e)
(<theano.gof.opt.TopoOptimizer object at 0x7f3219787f90>, 1, 5, 3, 0.00017309188842773438, 0.00020599365234375, 6.4849853515625e-05)
>>> e >>> e
[add(z, mul(x, div(z, x)))] [add(z, mul(x, true_div(z, x)))]
OpSub, OpRemove, PatternSub OpSub, OpRemove, PatternSub
+++++++++++++++++++++++++++ +++++++++++++++++++++++++++
...@@ -332,7 +335,7 @@ Theano defines some shortcuts to make LocalOptimizers: ...@@ -332,7 +335,7 @@ Theano defines some shortcuts to make LocalOptimizers:
See :class:`PatternSub`. See :class:`PatternSub`.
.. code-block:: python .. testcode::
from theano.gof.opt import OpSub, OpRemove, PatternSub from theano.gof.opt import OpSub, OpRemove, PatternSub
...@@ -346,9 +349,9 @@ Theano defines some shortcuts to make LocalOptimizers: ...@@ -346,9 +349,9 @@ Theano defines some shortcuts to make LocalOptimizers:
# The "simplify" operation we've been defining in the past few # The "simplify" operation we've been defining in the past few
# sections. Note that we need two patterns to account for the # sections. Note that we need two patterns to account for the
# permutations of the arguments to mul. # permutations of the arguments to mul.
local_simplify_1 = PatternSub((div, (mul, 'x', 'y'), 'y'), local_simplify_1 = PatternSub((true_div, (mul, 'x', 'y'), 'y'),
'x') 'x')
local_simplify_2 = PatternSub((div, (mul, 'x', 'y'), 'x'), local_simplify_2 = PatternSub((true_div, (mul, 'x', 'y'), 'x'),
'y') 'y')
.. note:: .. note::
...@@ -435,7 +438,7 @@ Query ...@@ -435,7 +438,7 @@ Query
A Query is built by the following call: A Query is built by the following call:
.. code-block:: python .. testcode::
theano.gof.Query(include, require = None, exclude = None, subquery = None) theano.gof.Query(include, require = None, exclude = None, subquery = None)
...@@ -476,7 +479,7 @@ Examples ...@@ -476,7 +479,7 @@ Examples
Here are a few examples of how to use a Query on optdb to produce an Here are a few examples of how to use a Query on optdb to produce an
Optimizer: Optimizer:
.. code-block:: python .. testcode::
from theano.compile import optdb from theano.compile import optdb
...@@ -500,7 +503,7 @@ Registering an Optimizer ...@@ -500,7 +503,7 @@ Registering an Optimizer
Let's say we have a global optimizer called ``simplify``. We can add Let's say we have a global optimizer called ``simplify``. We can add
it to ``optdb`` as follows: it to ``optdb`` as follows:
.. code-block:: python .. testcode::
# optdb.register(name, optimizer, order, *tags) # optdb.register(name, optimizer, order, *tags)
optdb.register('simplify', simplify, 0.5, 'fast_run') optdb.register('simplify', simplify, 0.5, 'fast_run')
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论