Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a663afde
提交
a663afde
authored
8月 10, 2015
作者:
Iban Harlouchet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
numpydoc for theano/sandbox/theano_object.py
上级
47ea0388
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
112 行增加
和
74 行删除
+112
-74
theano_object.py
theano/sandbox/theano_object.py
+112
-74
没有找到文件。
theano/sandbox/theano_object.py
浏览文件 @
a663afde
"""DRAFT: TheanoObject
"""
DRAFT: TheanoObject
N.B. the gotcha with this design is listed in the documentation of `TheanoObject`
N.B. the gotcha with this design is listed in the documentation of
`TheanoObject`.
"""
from
__future__
import
print_function
...
...
@@ -10,7 +12,10 @@ import numpy
def
theano_type
(
x
):
"""Return a theano Type instance suitable for containing value `x`."""
"""
Return a theano Type instance suitable for containing value `x`.
"""
if
type
(
x
)
is
int
:
return
tensor
.
lscalar
else
:
...
...
@@ -18,37 +23,47 @@ def theano_type(x):
class
symbolic_fn_callable
(
object
):
"""This is the class whose instance you get when you access a symbolic function in a
`TheanoObject`.
When you call a symbolic function (`symbolic_fn`) of a TheanoObject the `__call__` of this
class handles your request.
You can also access the symbolic outputs and updates of a symbolic function though this
class.
.. code-block:: python
class T(TheanoObject):
@symbolic_fn
def add(self, x):
...
add_outputs = ...
add_updates = ...
return RVal(add_outputs, add_updates)
t = T()
t.add.outputs(5) # returns `add_outputs` from when `x=theano_type(5)`
t.add.updates(5) # returns `add_updates` from when `x=theano_type(5)`
t.add.theano_function(5) # returns the `Function` compiled when `x=theano_type(5)`
t.add(5) # runs the `Function` compiled when `x=theano_type(5)`
# with arguments `(5,)`
"""
This is the class whose instance you get when you access a symbolic function
in a `TheanoObject`.
When you call a symbolic function (`symbolic_fn`) of a TheanoObject,
the `__call__` of this class handles your request.
You can also access the symbolic outputs and updates of a symbolic function
through this class.
Examples
--------
class T(TheanoObject):
@symbolic_fn
def add(self, x):
...
add_outputs = ...
add_updates = ...
return RVal(add_outputs, add_updates)
t = T()
t.add.outputs(5) # returns `add_outputs` from when `x=theano_type(5)`
t.add.updates(5) # returns `add_updates` from when `x=theano_type(5)`
t.add.theano_function(5) # returns the `Function` compiled when
# `x=theano_type(5)`
t.add(5) # runs the `Function` compiled when `x=theano_type(5)`
# with arguments `(5,)`
"""
def
__init__
(
self
,
fn
,
mode
):
self
.
fn
=
fn
self
.
mode
=
mode
def
on
(
self
,
o_self
):
"""Silly method to work with symbolic_fn.__get__"""
"""
Silly method to work with symbolic_fn.__get__.
"""
self
.
o_self
=
o_self
return
self
...
...
@@ -69,7 +84,9 @@ class symbolic_fn_callable(object):
class
symbolic_fn
(
object
):
"""A property-like class for decorating symbolic functions in `TheanoObject`
"""
A property-like class for decorating symbolic functions in `TheanoObject`.
"""
def
__init__
(
self
,
fn
,
mode
=
None
):
self
.
fn
=
fn
...
...
@@ -84,9 +101,11 @@ class symbolic_fn(object):
def
symbolic_fn_opts
(
**
kwargs
):
"""Return a decorator for symbolic_functions in a `TheanoObject`
"""
Return a decorator for symbolic_functions in a `TheanoObject`.
`kwargs` passed here are passed to `theano.function` via `symbolic_fn`.
`kwargs` passed here are passed to `theano.function` via `symbolic_fn`
"""
def
deco
(
f
):
return
symbolic_fn
(
f
,
**
kwargs
)
...
...
@@ -94,16 +113,22 @@ def symbolic_fn_opts(**kwargs):
class
RVal
(
object
):
"""A Return-Value object for a `symbolic_fn` """
"""
A Return-Value object for a `symbolic_fn`.
"""
outputs
=
[]
"""The method will compute values for the variables in this list"""
"""
The method will compute values for the variables in this list.
"""
updates
=
{}
"""The method will update module variables in this dictionary
"""The method will update module variables in this dictionary
.
For items ``(k,v)`` in this dictionary, ``k`` must be a `symbolic_member` of some module.
On each call to this compiled function, the value of ``k`` will be replaced with the
computed value of the Variable ``v``.
For items ``(k,v)`` in this dictionary, ``k`` must be a `symbolic_member`
of some module.
On each call to this compiled function, the value of ``k`` will be replaced
with the computed value of the Variable ``v``.
"""
def
__init__
(
self
,
outputs
,
updates
=
None
):
...
...
@@ -115,54 +140,63 @@ class RVal(object):
class
TheanoObject
(
object
):
"""Base for Theano-supported classes
"""
Base for Theano-supported classes.
This class provides support for symbolic_fn class attributes.
These will be compiled on demand so that they can be used just like normal (non-symbolic)
methods.
The symbolic functions in a TheanoObject can share member variables that have been created
using the `symbolic_member` method.
These will be compiled on demand so that they can be used just like normal
(non-symbolic) methods.
:note: Other variables (ones not created using ``self.symbolic_member``) referred to in the
body of a symbolic function will *not* be shared between symbolic functions, or between
symbolic functions and this class. These other variables will be locked away in the
closure of a symbolic function when that function is compiled.
The symbolic functions in a TheanoObject can share member variables that
have been created using the `symbolic_member` method.
Notes
-----
Other variables (ones not created using ``self.symbolic_member``) referred
to in the body of a symbolic function will *not* be shared between symbolic
functions, or between symbolic functions and this class. These other
variables will be locked away in the closure of a symbolic function when
that function is compiled.
:warning
: It is not recommended for code to interleave
.. warning:
: It is not recommended for code to interleave
(a) changes to non-symbolic instance variables with
(b) calls to symbolic functions that use those instance variables.
A symbolic function may be
compiled multiple times because it must be compiled for each set of argument types.
Each time the function is compiled, the values of non-symbolic variables will be locked
into the compiled function. Subsequent changes to those non-symbolic instance variables
will not have any effect on the behaviour of the already-compiled symbolic function.
A symbolic function may be compiled multiple times because it must be
compiled for each set of argument types.
Each time the function is compiled, the values of non-symbolic variables
will be locked into the compiled function. Subsequent changes to those
non-symbolic instance variables will not have any effect on the behaviour
of the already-compiled symbolic function.
:todo: Is there an efficient way of recognizing when a compiled symbolic
function is stale,
wrt the current values of the class's instance variables?
:todo: Is there an efficient way of recognizing when a compiled symbolic
function is stale,
wrt the current values of the class's instance variables?
- One option is to re-evaluate symbolic functions symbolically and see if
the graph can be
completely merged with the original graph. This is not fast enough to do all the time by
default though.
- One option is to re-evaluate symbolic functions symbolically and see if
the graph can be completely merged with the original graph. This is not
fast enough to do all the time by
default though.
"""
def
__init__
(
self
):
self
.
module_method_cache
=
{}
def
_get_method_impl
(
self
,
fn
,
o_self
,
args
,
kwargs
,
mode
):
"""Retrieve information about the symbolic function (`fn`) in TheanoObject instance
`o_self`, being evaluated on arguments `args` and `kwargs`.
:rtype: dict with entries 'theano_function', 'outputs', 'updates'
"""
Retrieve information about the symbolic function (`fn`) in TheanoObject
instance `o_self`, being evaluated on arguments `args` and `kwargs`.
:return: the theano function compiled for these arguments, the symbolic outputs of that
function, and the symbolic updates performed by that function.
Returns
-------
dict with entries 'theano_function', 'outputs', 'updates'
The theano function compiled for these arguments, the symbolic
outputs of that function, and the symbolic updates performed by
that function.
:note: This function caches return values in self.`module_method_cache`.
Notes
-----
This function caches return values in self.`module_method_cache`.
:todo: This may at some point become a class-level cache rather than an
instance-level
cache.
:todo: This may at some point become a class-level cache rather than an
instance-level
cache.
"""
if
kwargs
:
...
...
@@ -203,15 +237,19 @@ class TheanoObject(object):
return
cache
[
key
]
def
symbolic_member
(
self
,
ival
,
name
=
None
):
"""Create a Variable instance to hold value `ival`.
"""
Create a Variable instance to hold value `ival`.
This function also immediately creates a Container object for ival.
When the returned Variable is used as input to a `TheanoObject` `symbolic_fn`, (but
does not appear as an argument to that symbolic_fn), then this Container will be used to
retrieve (and store) values for the Variable.
This Variable's Container's contents can be retrieved by its `get()` method.
This Variable's Container's contents can be written using its `set(newval)` method.
When the returned Variable is used as input to a `TheanoObject`
`symbolic_fn`, (but does not appear as an argument to that symbolic_fn),
then this Container will be used to retrieve (and store) values for the
Variable.
This Variable's Container's contents can be retrieved by its `get()`
method.
This Variable's Container's contents can be written using its
`set(newval)` method.
"""
if
type
(
ival
)
is
not
int
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论