Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7d41214e
提交
7d41214e
authored
3月 02, 2012
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pep8
上级
0f4e6f57
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
42 行增加
和
30 行删除
+42
-30
sharedvalue.py
theano/compile/sharedvalue.py
+42
-30
没有找到文件。
theano/compile/sharedvalue.py
浏览文件 @
7d41214e
...
@@ -16,14 +16,16 @@ _logger = logging.getLogger('theano.compile.sharedvalue')
...
@@ -16,14 +16,16 @@ _logger = logging.getLogger('theano.compile.sharedvalue')
class
SharedVariable
(
Variable
):
class
SharedVariable
(
Variable
):
"""
"""
Variable that is (defaults to being) shared between functions that it appears in.
Variable that is (defaults to being) shared between functions that
it appears in.
"""
"""
#Container object
#Container object
container
=
None
container
=
None
"""
"""
A container to use for this SharedVariable when it is an implicit function parameter.
A container to use for this SharedVariable when it is an implicit
function parameter.
:type: `Container`
:type: `Container`
"""
"""
...
@@ -33,39 +35,44 @@ class SharedVariable(Variable):
...
@@ -33,39 +35,44 @@ class SharedVariable(Variable):
# this Variable, unless another update value has been passed to "function",
# this Variable, unless another update value has been passed to "function",
# or the "no_default_updates" list passed to "function" contains it.
# or the "no_default_updates" list passed to "function" contains it.
def
__init__
(
self
,
name
,
type
,
value
,
strict
,
allow_downcast
=
None
,
container
=
None
):
def
__init__
(
self
,
name
,
type
,
value
,
strict
,
allow_downcast
=
None
,
container
=
None
):
"""
"""
:param name: The name for this variable (see `Variable`).
:param name: The name for this variable (see `Variable`).
:param type: The type for this variable (see `Variable`).
:param type: The type for this variable (see `Variable`).
:param value: A value to associate with this variable (a new container will be created).
:param value: A value to associate with this variable (a new
container will be created).
:param strict: True -> assignments to .value will not be cast
or copied, so they must
:param strict: True -> assignments to .value will not be cast
have the correct type.
or copied, so they must
have the correct type.
:param allow_downcast: Only applies if `strict` is False.
:param allow_downcast: Only applies if `strict` is False.
True -> allow assigned value to lose precision when cast during assignment.
True -> allow assigned value to lose precision when cast during assignment.
False -> never allow precision loss.
False -> never allow precision loss.
None -> only allow downcasting of a Python float to a scalar floatX.
None -> only allow downcasting of a Python float to a scalar floatX.
:param container: The container to use for this
variable. Illegal to pass this as well
:param container: The container to use for this
as a value.
variable. Illegal to pass this as well
as a value.
:note: For more user-friendly constructor, see `shared`
:note: For more user-friendly constructor, see `shared`
"""
"""
super
(
SharedVariable
,
self
)
.
__init__
(
type
=
type
,
name
=
name
,
owner
=
None
,
index
=
None
)
super
(
SharedVariable
,
self
)
.
__init__
(
type
=
type
,
name
=
name
,
owner
=
None
,
index
=
None
)
if
container
is
not
None
:
if
container
is
not
None
:
self
.
container
=
container
self
.
container
=
container
if
(
value
is
not
None
)
or
(
strict
is
not
None
):
if
(
value
is
not
None
)
or
(
strict
is
not
None
):
raise
TypeError
(
'value and strict are ignored if you pass a container here'
)
raise
TypeError
(
'value and strict are ignored if you pass a container here'
)
else
:
else
:
if
container
is
not
None
:
if
container
is
not
None
:
raise
TypeError
(
'Error to specify both value and container'
)
raise
TypeError
(
'Error to specify both value and container'
)
self
.
container
=
Container
(
self
,
self
.
container
=
Container
(
self
,
storage
=
[
type
.
filter
(
value
,
strict
=
strict
,
allow_downcast
=
allow_downcast
)],
storage
=
[
type
.
filter
(
value
,
strict
=
strict
,
allow_downcast
=
allow_downcast
)],
readonly
=
False
,
readonly
=
False
,
strict
=
strict
,
strict
=
strict
,
allow_downcast
=
allow_downcast
)
allow_downcast
=
allow_downcast
)
...
@@ -73,15 +80,16 @@ class SharedVariable(Variable):
...
@@ -73,15 +80,16 @@ class SharedVariable(Variable):
def
get_value
(
self
,
borrow
=
False
,
return_internal_type
=
False
):
def
get_value
(
self
,
borrow
=
False
,
return_internal_type
=
False
):
"""Get the non-symbolic value associated with this SharedVariable.
"""Get the non-symbolic value associated with this SharedVariable.
:param borrow:
:param borrow:
True to permit returning of an object aliased
True to permit returning of an object aliased
to internal memory.
to internal memory.
:param return_internal_type:
:param return_internal_type:
True to permit the returning of
True to permit the returning of an arbitrary type object used internally to stor
e
an arbitrary type object used internally to store th
e
the
shared variable.
shared variable.
Only with borrow=False and return_internal_type=True does this function guarantee that
Only with borrow=False and return_internal_type=True does this
you actually get the internal object. But in that case, you may get different return
function guarantee that you actually get the internal object.
types when using different compute devices.
But in that case, you may get different return types when
using different compute devices.
"""
"""
if
borrow
:
if
borrow
:
...
@@ -89,14 +97,15 @@ class SharedVariable(Variable):
...
@@ -89,14 +97,15 @@ class SharedVariable(Variable):
else
:
else
:
return
copy
.
deepcopy
(
self
.
container
.
value
)
return
copy
.
deepcopy
(
self
.
container
.
value
)
def
set_value
(
self
,
new_value
,
borrow
=
False
):
def
set_value
(
self
,
new_value
,
borrow
=
False
):
"""Set the non-symbolic value associated with this SharedVariable.
"""Set the non-symbolic value associated with this SharedVariable.
:param borrow:
:param borrow:
True to use the new_value directly, potentially creating problems
True to use the new_value directly, potentially creating problems
related to aliased memory.
related to aliased memory.
Changes to this value will be visible to all functions using this SharedVariable.
Changes to this value will be visible to all functions using
this SharedVariable.
"""
"""
if
borrow
:
if
borrow
:
self
.
container
.
value
=
new_value
self
.
container
.
value
=
new_value
...
@@ -157,14 +166,15 @@ def shared_constructor(ctor, remove=False):
...
@@ -157,14 +166,15 @@ def shared_constructor(ctor, remove=False):
def
shared
(
value
,
name
=
None
,
strict
=
False
,
allow_downcast
=
None
,
**
kwargs
):
def
shared
(
value
,
name
=
None
,
strict
=
False
,
allow_downcast
=
None
,
**
kwargs
):
"""Return a SharedVariable Variable, initialized with a copy or reference of `value`.
"""Return a SharedVariable Variable, initialized with a copy or
reference of `value`.
This function iterates over constructor functions (see
`shared_constructor`) to find a suitable SharedVariable subclass.
This function iterates over constructor functions (see `shared_constructor`) to find a
:note: By passing kwargs, you effectively limit the set of
suitable SharedVariable subclas
s.
potential constructors to those that can accept those kwarg
s.
:note:
By passing kwargs, you effectively limit the set of potential constructors to those that
can accept those kwargs.
:note: Some shared variable have 'borrow' as extra kwargs.
:note: Some shared variable have 'borrow' as extra kwargs.
`See <http://deeplearning.net/software/theano/tutorial/aliasing.html#borrowing-when-creating-shared-variables>`_ for detail.
`See <http://deeplearning.net/software/theano/tutorial/aliasing.html#borrowing-when-creating-shared-variables>`_ for detail.
...
@@ -178,11 +188,13 @@ def shared(value, name=None, strict=False, allow_downcast=None, **kwargs):
...
@@ -178,11 +188,13 @@ def shared(value, name=None, strict=False, allow_downcast=None, **kwargs):
# This may happen when kwargs were supplied
# This may happen when kwargs were supplied
# if kwargs were given, the generic_constructor won't be callable.
# if kwargs were given, the generic_constructor won't be callable.
#
#
# This was done on purpose, the rationale being that if kwargs were supplied,
# This was done on purpose, the rationale being that if kwargs
# the user didn't want them to be ignored.
# were supplied, the user didn't want them to be ignored.
raise
TypeError
(
'No suitable SharedVariable constructor could be found'
,
(
value
,
kwargs
))
raise
TypeError
(
'No suitable SharedVariable constructor could be found'
,
(
value
,
kwargs
))
shared
.
constructors
=
[]
shared
.
constructors
=
[]
@shared_constructor
@shared_constructor
def
generic_constructor
(
value
,
name
=
None
,
strict
=
False
,
allow_downcast
=
None
):
def
generic_constructor
(
value
,
name
=
None
,
strict
=
False
,
allow_downcast
=
None
):
"""SharedVariable Constructor"""
"""SharedVariable Constructor"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论