Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
cee71c3b
提交
cee71c3b
authored
7月 23, 2015
作者:
Iban Harlouchet
提交者:
Arnaud Bergeron
9月 08, 2015
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
testcode for doc/extending/optimization.txt
上级
ee43bd11
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
34 行增加
和
31 行删除
+34
-31
optimization.txt
doc/extending/optimization.txt
+34
-31
没有找到文件。
doc/extending/optimization.txt
浏览文件 @
cee71c3b
...
...
@@ -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
facility.
Global optimization
-------------------
...
...
@@ -123,8 +122,10 @@ simplification described above:
.. If you modify this code, also change :
.. 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
class Simplify(gof.Optimizer):
...
...
@@ -132,7 +133,7 @@ simplification described above:
fgraph.attach_feature(toolbox.ReplaceValidate())
def apply(self, fgraph):
for node in fgraph.toposort():
if node.op == div:
if node.op ==
true_
div:
x, y = node.inputs
z = node.outputs[0]
if x.owner and x.owner.op == mul:
...
...
@@ -186,32 +187,33 @@ for the simplification (``x``, ``y``, ``z``, ``a``, ``b``, etc.).
Test time:
>>> x = double('x')
>>> y = double('y')
>>> z = double('z')
>>> a = add(z, mul(div(mul(y, x), y), div(z, x)))
>>> from theano.scalar import float64, add, mul, true_div
>>> x = float64('x')
>>> y = float64('y')
>>> 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
[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)
>>> 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
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
optimization you wrote. For example, consider the following:
>>> x =
double
('x')
>>> y =
double
('y')
>>> z =
double
('z')
>>> a = div(mul(add(y, z), x), add(y, z))
>>> x =
float64
('x')
>>> y =
float64
('y')
>>> z =
float64
('z')
>>> a =
true_
div(mul(add(y, z), x), add(y, z))
>>> e = gof.FunctionGraph([x, y, z], [a])
>>> e
[div(mul(add(y, z), x), add(y, z))]
[
true_
div(mul(add(y, z), x), add(y, z))]
>>> simplify.optimize(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,
z)``. That is the case for efficiency reasons. To fix this problem we
...
...
@@ -221,8 +223,9 @@ computation, using the ``merge_optimizer`` defined in
>>> from theano.gof.opt import merge_optimizer
>>> merge_optimizer.optimize(e)
(0, 0.0001430511474609375, None, None, {}, 1, 0)
>>> e
[div(mul(*1 -> add(y, z), x), *1)]
[
true_
div(mul(*1 -> add(y, z), x), *1)]
>>> simplify.optimize(e)
>>> e
[x]
...
...
@@ -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
..
code-block:: python
..
testcode::
class LocalSimplify(gof.LocalOptimizer):
def transform(self, node):
if node.op == div:
if node.op ==
true_
div:
x, y = node.inputs
if x.owner and x.owner.op == mul:
a, b = x.owner.inputs
...
...
@@ -295,18 +298,18 @@ 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')
>>> a = add(z, mul(
div(mul(y, x), y),
div(z, x)))
>>> x =
float64
('x')
>>> y =
float64
('y')
>>> 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
[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.optimize(e)
(<theano.gof.opt.TopoOptimizer object at 0x7f3219787f90>, 1, 5, 3, 0.00017309188842773438, 0.00020599365234375, 6.4849853515625e-05)
>>> e
[add(z, mul(x, div(z, x)))]
[add(z, mul(x, true_div(z, x)))]
OpSub, OpRemove, PatternSub
+++++++++++++++++++++++++++
...
...
@@ -332,7 +335,7 @@ Theano defines some shortcuts to make LocalOptimizers:
See :class:`PatternSub`.
..
code-block:: python
..
testcode::
from theano.gof.opt import OpSub, OpRemove, PatternSub
...
...
@@ -346,9 +349,9 @@ Theano defines some shortcuts to make LocalOptimizers:
# The "simplify" operation we've been defining in the past few
# sections. Note that we need two patterns to account for the
# 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')
local_simplify_2 = PatternSub((div, (mul, 'x', 'y'), 'x'),
local_simplify_2 = PatternSub((
true_
div, (mul, 'x', 'y'), 'x'),
'y')
.. note::
...
...
@@ -435,7 +438,7 @@ Query
A Query is built by the following call:
..
code-block:: python
..
testcode::
theano.gof.Query(include, require = None, exclude = None, subquery = None)
...
...
@@ -476,7 +479,7 @@ Examples
Here are a few examples of how to use a Query on optdb to produce an
Optimizer:
..
code-block:: python
..
testcode::
from theano.compile import optdb
...
...
@@ -500,7 +503,7 @@ Registering an Optimizer
Let's say we have a global optimizer called ``simplify``. We can add
it to ``optdb`` as follows:
..
code-block:: python
..
testcode::
# optdb.register(name, optimizer, order, *tags)
optdb.register('simplify', simplify, 0.5, 'fast_run')
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论