Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
d71e2fce
提交
d71e2fce
authored
3月 05, 2009
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
more documentation on creating ops
上级
8a15ca52
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
78 行增加
和
0 行删除
+78
-0
op.txt
doc/tutorials/advanced/ex1/op.txt
+78
-0
没有找到文件。
doc/tutorials/advanced/ex1/op.txt
浏览文件 @
d71e2fce
...
@@ -229,7 +229,85 @@ is basically a :ref:`result` we statically know the value of.
...
@@ -229,7 +229,85 @@ is basically a :ref:`result` we statically know the value of.
And now it works the way we want it to.
And now it works the way we want it to.
Final version
=============
While I would call the above definitions appropriately pedagogical, it
is not necessarily the best way to do things, especially when you need
to define the other basic arithmetic operations ``add``, ``sub`` and
``div``. It appears that the code for ``make_node`` can be shared
between these Ops. Here is the final version of the four arithmetic
operators (well, pending revision of this tutorial, I guess):
.. code-block:: python
from theano import gof
class BinaryDoubleOp(gof.Op):
def __init__(self, name, fn, gradfnx, gradfny):
self.name = name
self.fn = fn
self.gradfnx = gradfnx
self.gradfny = gradfny
def make_node(self, x, y):
if isinstance(x, (int, float)):
x = gof.Constant(double, x)
if isinstance(y, (int, float)):
y = gof.Constant(double, y)
if x.type != double or y.type != double:
raise TypeError('%s only works on doubles' % self.name)
return gof.Apply(self, [x, y], [double()])
def perform(self, node, (x, y), (z, )):
z[0] = self.fn(x, y)
def __str__(self):
return self.name
add = BinaryDoubleOp(name = 'add',
fn = lambda x, y: x + y)
sub = BinaryDoubleOp(name = 'sub',
fn = lambda x, y: x - y)
mul = BinaryDoubleOp(name = 'mul',
fn = lambda x, y: x * y)
div = BinaryDoubleOp(name = 'div',
fn = lambda x, y: x / y)
Can you see how the definition of ``mul`` here does exactly the same
thing as the definition we had earlier?
Instead of working directly on an instance of Op, we create a subclass
of Op that we can parametrize. First, all the operations we define are
binary, they all work on inputs with type ``double`` and they all
return a single Result of type ``double``. Therefore, ``make_node``
basically does the same thing for all these operations, except for the
fact that the Op reference passed as first argument to Apply must be
themselves. Therefore we can abstract out most of the logic and pass
self to Apply, which seems natural. We can also easily define
``perform`` as depending on a function or lambda expression passed in
the constructor.
This design therefore appears to be a flexible way to define our four
basic operations (and possibly many more!) without duplicating
code. The same way a Type subclass represents a set of structurally
similar types (see previous section), an Op subclass represents a set
of structurally similar operations: operations that have the same
input/output types, operations that only differ in one small detail,
etc. If you see common patterns in several Ops that you want to
define, it can be a good idea to abstract out what you can, as I did
here. Remember that an Op is just an object which satisfies the
contract described above on this page and that you should use all the
tools at your disposal to create these objects as efficiently as
possible.
While I could have made a generic DoubleOp where the number of
arguments can also be given as a parameter, I decided it was not
necessary here.
**Next:** `Implementing double in C`_
**Next:** `Implementing double in C`_
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论