Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
bb5a1127
提交
bb5a1127
authored
5月 05, 2014
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make a register for SpecifyShape.c_code
上级
c3d3ea87
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
72 行增加
和
37 行删除
+72
-37
__init__.py
theano/compile/__init__.py
+1
-1
ops.py
theano/compile/ops.py
+41
-35
type.py
theano/tensor/type.py
+30
-1
没有找到文件。
theano/compile/__init__.py
浏览文件 @
bb5a1127
...
@@ -4,7 +4,7 @@ from theano.compile.ops import (
...
@@ -4,7 +4,7 @@ from theano.compile.ops import (
Shape_i
,
register_shape_i_c_code
,
Shape_i
,
register_shape_i_c_code
,
ViewOp
,
view_op
,
register_view_op_c_code
,
FromFunctionOp
,
ViewOp
,
view_op
,
register_view_op_c_code
,
FromFunctionOp
,
as_op
,
Rebroadcast
,
register_rebroadcast_c_code
,
as_op
,
Rebroadcast
,
register_rebroadcast_c_code
,
SpecifyShape
,
specify_shape
)
SpecifyShape
,
specify_shape
,
register_specify_shape_c_code
)
from
theano.compile.function_module
import
*
from
theano.compile.function_module
import
*
...
...
theano/compile/ops.py
浏览文件 @
bb5a1127
...
@@ -613,6 +613,20 @@ class Rebroadcast(gof.Op):
...
@@ -613,6 +613,20 @@ class Rebroadcast(gof.Op):
return
tuple
(
version
)
return
tuple
(
version
)
def
register_specify_shape_c_code
(
typ
,
code
,
version
=
()):
""" Tell SpecifyShape how to generate C code for a Theano Type
:param typ: A Theano type. It must be the Theano class itself and not an
instance of the class.
:param code: C code that deep copies the Theano type 'typ'.
Use
%(iname)
s and
%(oname)
s for the input and output C
variable names respectively.
%(axis)
s for the axis that we need to check.
:param version: A number indicating the version of the code, for cache.
"""
SpecifyShape
.
c_code_and_version
[
typ
]
=
(
code
,
version
)
class
SpecifyShape
(
gof
.
Op
):
class
SpecifyShape
(
gof
.
Op
):
"""
"""
L{Op} that puts into the graph the user-provided shape.
L{Op} that puts into the graph the user-provided shape.
...
@@ -629,6 +643,10 @@ class SpecifyShape(gof.Op):
...
@@ -629,6 +643,10 @@ class SpecifyShape(gof.Op):
Do C code for them too.
Do C code for them too.
"""
"""
view_map
=
{
0
:
[
0
]}
view_map
=
{
0
:
[
0
]}
# Mapping from Type to C code (and version) to use.
# In the C code, the name of the input variable is %(iname)s,
# the output variable is %(oname)s.
c_code_and_version
=
{}
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
return
hash
(
type
(
self
))
...
@@ -692,45 +710,33 @@ class SpecifyShape(gof.Op):
...
@@ -692,45 +710,33 @@ class SpecifyShape(gof.Op):
return
[
None
]
return
[
None
]
return
self
.
make_node
(
eval_points
[
0
],
*
inputs
[
1
:])
.
outputs
return
self
.
make_node
(
eval_points
[
0
],
*
inputs
[
1
:])
.
outputs
def
c_code
(
self
,
node
,
nodename
,
inp
,
out
,
sub
):
def
c_code
(
self
,
node
,
name
,
inames
,
onames
,
sub
):
if
not
isinstance
(
node
.
inputs
[
0
],
theano
.
tensor
.
TensorVariable
):
iname
,
shape
=
inames
# The C code below supports only Tensor. super.c_code
oname
,
=
onames
# will raise an exception to tell that there is no C code
# for the other cases.
return
super
(
SpecifyShape
,
self
)
.
c_code
(
node
,
nodename
,
inp
,
out
,
sub
)
iname
,
shape
=
inp
oname
,
=
out
fail
=
sub
[
'fail'
]
fail
=
sub
[
'fail'
]
return
"""
itype
=
node
.
inputs
[
0
]
.
type
.
__class__
if (PyArray_NDIM(
%(iname)
s) != PyArray_DIMS(
%(shape)
s)[0]) {
if
itype
in
self
.
c_code_and_version
:
PyErr_Format(PyExc_AssertionError,
code
,
version
=
self
.
c_code_and_version
[
itype
]
"SpecifyShape: vector of shape has
%%
d elements,"
return
code
%
locals
()
" but the input has
%%
d dimensions.",
PyArray_NDIM(
%(iname)
s),
return
super
(
SpecifyShape
,
self
)
.
c_code
(
node
,
node
,
inames
,
onames
,
sub
)
PyArray_DIMS(
%(shape)
s)[0]);
%(fail)
s;
}
for(int i = 0; i < PyArray_NDIM(
%(iname)
s); i++){
dtype_
%(shape)
s shp = ((dtype_
%(shape)
s*)PyArray_GETPTR1(
%(shape)
s,
i))[0];
if (PyArray_DIMS(
%(iname)
s)[i] != shp) {
PyErr_Format(PyExc_AssertionError,
"SpecifyShape: dim
%%
d of input has shape
%%
d,"
" expected
%%
d.",
i, PyArray_DIMS(
%(iname)
s)[i],
shp);
%(fail)
s;
}
}
Py_XDECREF(
%(oname)
s);
%(oname)
s =
%(iname)
s;
Py_XINCREF(
%(oname)
s);
"""
%
locals
()
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
1
,)
version
=
[]
# If any of the c code is unversionned, we have to return ()
# Else, we will return a list of (type name, version) pairs.
for
t
,
(
c
,
v
)
in
sorted
(
self
.
c_code_and_version
.
items
(),
key
=
lambda
pair
:
str
(
pair
[
0
])):
if
not
v
:
warnings
.
warn
(
"Type
%
s has C code for SpecifyShape, but it has "
"no version. You should add a 'version' keyword arg "
"when calling register_specify_shape_c_code."
%
t
,
stacklevel
=
2
)
return
()
version
.
append
((
str
(
t
),
v
))
return
tuple
(
version
)
specify_shape
=
SpecifyShape
()
specify_shape
=
SpecifyShape
()
theano/tensor/type.py
浏览文件 @
bb5a1127
...
@@ -673,7 +673,6 @@ theano.compile.register_deep_copy_op_c_code(
...
@@ -673,7 +673,6 @@ theano.compile.register_deep_copy_op_c_code(
version
=
2
)
version
=
2
)
# Register TensorType C code for ViewOp.
theano
.
compile
.
register_rebroadcast_c_code
(
theano
.
compile
.
register_rebroadcast_c_code
(
TensorType
,
TensorType
,
"""
"""
...
@@ -686,3 +685,33 @@ theano.compile.register_rebroadcast_c_code(
...
@@ -686,3 +685,33 @@ theano.compile.register_rebroadcast_c_code(
}
}
"""
,
"""
,
version
=
1
)
version
=
1
)
theano
.
compile
.
register_specify_shape_c_code
(
TensorType
,
"""
if (PyArray_NDIM(
%(iname)
s) != PyArray_DIMS(
%(shape)
s)[0]) {
PyErr_Format(PyExc_AssertionError,
"SpecifyShape: vector of shape has
%%
d elements,"
" but the input has
%%
d dimensions.",
PyArray_NDIM(
%(iname)
s),
PyArray_DIMS(
%(shape)
s)[0]);
%(fail)
s;
}
for(int i = 0; i < PyArray_NDIM(
%(iname)
s); i++){
dtype_
%(shape)
s shp = ((dtype_
%(shape)
s*)PyArray_GETPTR1(
%(shape)
s,
i))[0];
if (PyArray_DIMS(
%(iname)
s)[i] != shp) {
PyErr_Format(PyExc_AssertionError,
"SpecifyShape: dim
%%
d of input has shape
%%
d,"
" expected
%%
d.",
i, PyArray_DIMS(
%(iname)
s)[i],
shp);
%(fail)
s;
}
}
Py_XDECREF(
%(oname)
s);
%(oname)
s =
%(iname)
s;
Py_XINCREF(
%(oname)
s);
"""
,
version
=
1
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论