Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5b5b87b2
提交
5b5b87b2
authored
11月 13, 2020
作者:
Brandon T. Willard
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use np.shape in Shape/Shape_i Python implementations
This change allows `Shape`/`Shape_i` to handle non-standard `Op` values, like the `None` and `list` values produced by `NoneType` and `MakeList` `Op`s.
上级
5bc0592c
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
39 行增加
和
11 行删除
+39
-11
test_ops.py
tests/compile/test_ops.py
+35
-4
ops.py
theano/compile/ops.py
+4
-7
没有找到文件。
tests/compile/test_ops.py
浏览文件 @
5b5b87b2
...
...
@@ -5,8 +5,9 @@ import pytest
import
theano
from
tests
import
unittest_tools
as
utt
from
theano
import
config
,
function
from
theano.compile.ops
import
Rebroadcast
,
SpecifyShape
,
as_op
from
theano
import
change_flags
,
config
,
function
from
theano.compile.ops
import
Rebroadcast
,
SpecifyShape
,
as_op
,
shape
,
shape_i
from
theano.gof.fg
import
FunctionGraph
from
theano.tensor.basic
import
(
TensorType
,
dmatrix
,
...
...
@@ -14,8 +15,13 @@ from theano.tensor.basic import (
dvector
,
ivector
,
matrix
,
tensor3
,
vector
,
)
from
theano.tensor.opt
import
ShapeFeature
from
theano.tensor.subtensor
import
Subtensor
from
theano.tensor.type_other
import
NoneConst
from
theano.typed_list
import
make_list
@as_op
([
dmatrix
,
dmatrix
],
dmatrix
)
...
...
@@ -202,7 +208,7 @@ class TestRebroadcast(utt.InferShapeTester):
# Rebroadcast
adtens4
=
dtensor4
()
adict
=
[(
0
,
False
),
(
1
,
True
),
(
2
,
False
),
(
3
,
True
)]
adtens4_val
=
rng
.
rand
(
2
,
1
,
3
,
1
)
adtens4_val
=
rng
.
rand
(
2
,
1
,
3
,
1
)
.
astype
(
config
.
floatX
)
self
.
_compile_and_check
(
[
adtens4
],
[
Rebroadcast
(
*
adict
)(
adtens4
)],
...
...
@@ -213,10 +219,35 @@ class TestRebroadcast(utt.InferShapeTester):
adtens4_bro
=
TensorType
(
"float64"
,
(
True
,
True
,
True
,
False
))()
bdict
=
[(
0
,
True
),
(
1
,
False
),
(
2
,
False
),
(
3
,
False
)]
adtens4_bro_val
=
rng
.
rand
(
1
,
1
,
1
,
3
)
adtens4_bro_val
=
rng
.
rand
(
1
,
1
,
1
,
3
)
.
astype
(
config
.
floatX
)
self
.
_compile_and_check
(
[
adtens4_bro
],
[
Rebroadcast
(
*
bdict
)(
adtens4_bro
)],
[
adtens4_bro_val
],
Rebroadcast
,
)
@change_flags
(
compute_test_value
=
"raise"
)
def
test_nonstandard_shapes
():
a
=
tensor3
(
config
.
floatX
)
a
.
tag
.
test_value
=
np
.
random
.
random
((
2
,
3
,
4
))
.
astype
(
config
.
floatX
)
b
=
tensor3
(
theano
.
config
.
floatX
)
b
.
tag
.
test_value
=
np
.
random
.
random
((
2
,
3
,
4
))
.
astype
(
config
.
floatX
)
tl
=
make_list
([
a
,
b
])
tl_shape
=
shape
(
tl
)
assert
np
.
array_equal
(
tl_shape
.
get_test_value
(),
(
2
,
2
,
3
,
4
))
# There's no `FunctionGraph`, so it should return a `Subtensor`
tl_shape_i
=
shape_i
(
tl
,
0
)
assert
isinstance
(
tl_shape_i
.
owner
.
op
,
Subtensor
)
assert
tl_shape_i
.
get_test_value
()
==
2
tl_fg
=
FunctionGraph
([
a
,
b
],
[
tl
],
features
=
[
ShapeFeature
()])
tl_shape_i
=
shape_i
(
tl
,
0
,
fgraph
=
tl_fg
)
assert
not
isinstance
(
tl_shape_i
.
owner
.
op
,
Subtensor
)
assert
tl_shape_i
.
get_test_value
()
==
2
none_shape
=
shape
(
NoneConst
)
assert
np
.
array_equal
(
none_shape
.
get_test_value
(),
[])
theano/compile/ops.py
浏览文件 @
5b5b87b2
...
...
@@ -260,7 +260,7 @@ class Shape(Op):
def
perform
(
self
,
node
,
inp
,
out_
):
(
x
,)
=
inp
(
out
,)
=
out_
out
[
0
]
=
theano
.
_asarray
(
x
.
shape
,
dtype
=
"int64"
)
out
[
0
]
=
theano
.
_asarray
(
np
.
shape
(
x
)
,
dtype
=
"int64"
)
def
infer_shape
(
self
,
node
,
in_shapes
):
return
[[
len
(
in_shapes
[
0
])]]
...
...
@@ -370,9 +370,6 @@ class Shape_i(Op):
return
"
%
s{
%
i}"
%
(
self
.
__class__
.
__name__
,
self
.
i
)
def
make_node
(
self
,
x
):
# x could be one of a number of types
# the only thing we require is that the variable have a .ndim,
# and that the value have a .shape
if
not
isinstance
(
x
,
theano
.
Variable
):
raise
TypeError
(
"x must be Variable with ndim attribute"
,
x
)
if
x
.
ndim
<=
self
.
i
:
...
...
@@ -383,9 +380,9 @@ class Shape_i(Op):
(
x
,)
=
inp
(
out
,)
=
out_
if
out
[
0
]
is
None
:
out
[
0
]
=
theano
.
_asarray
(
x
.
shape
[
self
.
i
],
dtype
=
"int64"
)
out
[
0
]
=
theano
.
_asarray
(
np
.
shape
(
x
)
[
self
.
i
],
dtype
=
"int64"
)
else
:
out
[
0
][
...
]
=
x
.
shape
[
self
.
i
]
out
[
0
][
...
]
=
np
.
shape
(
x
)
[
self
.
i
]
def
c_code_cache_version
(
self
):
version
=
[]
...
...
@@ -489,7 +486,7 @@ def shape_i(var, i, fgraph=None):
# If we are not able to use the shape feature, we should not put
# Shape_i in the graph. Otherwise, the shape feature optimization
# won't get applied.
return
var
.
shape
[
i
]
return
shape
(
var
)
[
i
]
def
shape_i_op
(
i
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论