Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f13ab8b7
提交
f13ab8b7
authored
1月 18, 2012
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add a "filter_variable" mechanism in Type.
It should work similarly to filter(), but on symbolic variables, returning equivalent variables of the current Type if they are compatible.
上级
7dd0276b
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
82 行增加
和
1 行删除
+82
-1
type.py
theano/gof/type.py
+27
-1
type.py
theano/sandbox/cuda/type.py
+26
-0
basic.py
theano/tensor/basic.py
+29
-0
没有找到文件。
theano/gof/type.py
浏览文件 @
f13ab8b7
...
...
@@ -228,9 +228,35 @@ class PureType(object):
# filter() This is to allow reusing the old allocated memory. As
# of this writing this is used only when we transfer new data to a
# shared variable on the gpu.
#def filter_inplace(value, storage, strict=False, allow_downcast=None)
def
filter_variable
(
self
,
other
):
"""Convert a symbolic variable into this Type, if compatible.
For the moment, the only Types compatible with one another are
TensorType and CudaNdarrayType, provided they have the same
number of dimensions, same broadcasting pattern, and same dtype.
If Types are not compatible, a TypeError should be raised.
"""
if
not
isinstance
(
other
,
graph
.
Variable
):
# The value is not a Variable: we cast it into
# a Constant of the appropriate Type.
other
=
self
.
Constant
(
type
=
self
,
data
=
other
)
if
other
.
type
!=
self
:
raise
TypeError
(
'Cannot convert Type
%(othertype)
s '
'(of Variable
%(other)
s) into Type
%(self)
s. '
'You can try to manually convert
%(other)
s into a
%(self)
s.'
%
dict
(
othertype
=
other
.
type
,
other
=
other
,
self
=
self
)
)
return
other
def
is_valid_value
(
self
,
a
):
"""Required: Return True for any python object `a` that would be a legal value for a Variable of this Type"""
try
:
...
...
theano/sandbox/cuda/type.py
浏览文件 @
f13ab8b7
...
...
@@ -97,6 +97,32 @@ class CudaNdarrayType(Type):
%
(
self
,
self
.
dtype
,
data
,
converted_data
,
self
.
dtype
),
data
)
def
filter_variable
(
self
,
other
):
"""Convert a Variable into a CudaNdarrayType, if compatible.
This Variable should either already be a CudaNdarrayType, or be
a TensorType. It has to have the right number of dimensions,
broadcastable pattern, and dtype.
"""
if
hasattr
(
other
,
'_as_CudaNdarrayVariable'
):
other
=
other
.
_as_CudaNdarrayVariable
()
if
not
isinstance
(
other
,
Variable
):
# The value is not a Variable: we cast it into
# a Constant of the appropriate Type.
other
=
self
.
Constant
(
type
=
self
,
data
=
other
)
if
other
.
type
==
self
:
return
other
if
not
isinstance
(
other
.
type
,
tensor
.
TensorType
):
raise
TypeError
(
'Incompatible type'
,
(
self
,
other
.
type
))
if
(
other
.
type
.
dtype
!=
self
.
dtype
):
raise
TypeError
(
'Incompatible dtype'
,
(
self
.
dtype
,
other
.
type
.
dtype
))
if
(
other
.
type
.
broadcastable
!=
self
.
broadcastable
):
raise
TypeError
(
'Incompatible broadcastable'
,
(
self
.
broadcastable
,
other
.
type
.
broadcastable
))
return
theano
.
sandbox
.
cuda
.
basic_ops
.
GpuFromHost
()(
other
)
@staticmethod
def
bound
(
a
):
...
...
theano/tensor/basic.py
浏览文件 @
f13ab8b7
...
...
@@ -633,6 +633,35 @@ class TensorType(Type):
raise
ValueError
(
"non-finite elements not allowed"
)
return
data
def
filter_variable
(
self
,
other
):
"""Convert a symbolic Variable into a TensorType, if compatible.
For the moment, only a TensorType or CudaNdarrayType will be
converted, provided they have the same number of dimensions,
broadcastable pattern, and dtype.
"""
if
hasattr
(
other
,
'_as_TensorVariable'
):
other
=
other
.
_as_TensorVariable
()
if
not
isinstance
(
other
,
Variable
):
# The value is not a Variable: we cast it into
# a Constant of the appropriate Type.
other
=
self
.
Constant
(
type
=
self
,
data
=
other
)
if
other
.
type
==
self
:
return
other
raise
TypeError
(
'Cannot convert Type
%(othertype)
s '
'(of Variable
%(other)
s) into Type
%(self)
s. '
'You can try to manually convert
%(other)
s into a
%(self)
s.'
%
dict
(
othertype
=
other
.
type
,
other
=
other
,
self
=
self
)
)
def
value_validity_msg
(
self
,
a
):
try
:
self
.
filter
(
a
,
strict
=
True
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论