Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e2e65f54
提交
e2e65f54
authored
2月 07, 2017
作者:
Frédéric Bastien
提交者:
GitHub
2月 07, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5443 from Thrandis/ccw
Added broadcast check in scan.
上级
47548c0a
e5b05f47
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
51 行增加
和
0 行删除
+51
-0
scan_op.py
theano/scan_module/scan_op.py
+31
-0
test_scan.py
theano/scan_module/tests/test_scan.py
+20
-0
没有找到文件。
theano/scan_module/scan_op.py
浏览文件 @
e2e65f54
...
...
@@ -403,6 +403,36 @@ class Scan(PureOp):
'by using dimshuffle or shape_padleft. '
)
def
check_broadcast
(
v1
,
v2
):
""" Checks that the broadcast pattern of v1 and v2.
Controls that the broadcast pattern of the variable provided as
input to `scan` matches the broadcast pattern provided in
`output_info`. It raises an error when they don't match. The
typical case is when the user provides either the input or the
`output_info` (but not both) with a dimension fixed to 1,
which may wrongly be interpreted as broadcastable.
"""
if
(
not
hasattr
(
v1
,
'broadcastable'
)
and
not
hasattr
(
v2
,
'broadcastable'
)):
return
msg
=
(
"The broadcast pattern of the output of scan (
%
s) is "
"inconsistent with the one provided in `output_info` "
"(
%
s). The output on axis
%
d is `
%
r`, but it is `
%
r` on "
"axis
%
d in `output_info`. This can happen if one of the "
"dimension is fixed to 1 in the input, while it is still "
"variable in the output, or vice-verca. You have to make "
"them consistent, e.g. using theano.tensor."
"{patternbroadcast,unbroadcast,addbroadcast}."
)
size
=
min
(
len
(
v1
.
broadcastable
),
len
(
v2
.
broadcastable
))
for
n
,
(
b1
,
b2
)
in
enumerate
(
zip
(
v1
.
broadcastable
[
-
size
:],
v2
.
broadcastable
[
-
size
:])):
if
b1
!=
b2
:
a1
=
n
+
size
-
len
(
v1
.
broadcastable
)
+
1
a2
=
n
+
size
-
len
(
v2
.
broadcastable
)
+
1
raise
TypeError
(
msg
%
(
v1
.
type
,
v2
.
type
,
a1
,
b1
,
b2
,
a2
))
def
format
(
var
,
as_var
):
"""
This functions ensures that ``out`` has the same dtype as
...
...
@@ -430,6 +460,7 @@ class Scan(PureOp):
argoffset
=
0
for
inner_seq
,
outer_seq
in
zip
(
self
.
inner_seqs
(
self
.
inputs
),
self
.
outer_seqs
(
inputs
)):
check_broadcast
(
outer_seq
,
inner_seq
)
new_inputs
.
append
(
format
(
outer_seq
,
as_var
=
inner_seq
))
argoffset
+=
len
(
self
.
outer_seqs
(
inputs
))
...
...
theano/scan_module/tests/test_scan.py
浏览文件 @
e2e65f54
...
...
@@ -5498,3 +5498,23 @@ def test_default_value_broadcasted():
updates
=
[(
W_x
,
W_x
-
0.1
*
gW_x
)]
f
=
theano
.
function
([
X
],
outputs
=
cost
,
updates
=
updates
)
f
(
numpy
.
random
.
rand
(
10
,
in_size
)
.
astype
(
X
.
dtype
))
class
TestInconsistentBroadcast
(
unittest
.
TestCase
):
def
test_raise_error
(
self
):
x
=
tensor
.
tensor3
()
initial_x
=
tensor
.
constant
(
numpy
.
zeros
((
1
,
10
)))
y
,
updates
=
theano
.
scan
(
fn
=
lambda
x
,
prev_x
:
x
+
prev_x
,
sequences
=
x
,
outputs_info
=
[
dict
(
initial
=
initial_x
)])
# Error, because the broadcast patterns are inconsistent.
with
self
.
assertRaises
(
TypeError
):
gs
=
tensor
.
grad
(
y
.
sum
(),
x
)
# No error here, because the broadcast patterns are consistent.
initial_x
=
tensor
.
unbroadcast
(
initial_x
,
0
,
1
)
y
,
updates
=
theano
.
scan
(
fn
=
lambda
x
,
prev_x
:
x
+
prev_x
,
sequences
=
x
,
outputs_info
=
[
dict
(
initial
=
initial_x
)])
gs
=
tensor
.
grad
(
y
.
sum
(),
x
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论