Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e67a5ba0
提交
e67a5ba0
authored
2月 26, 2015
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2363 from lamblin/fix_advincsubtensor_grad
Fix advincsubtensor grad crash
上级
6be35ca3
0c67f88f
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
48 行增加
和
31 行删除
+48
-31
null_type.py
theano/gof/null_type.py
+1
-1
subtensor.py
theano/tensor/subtensor.py
+36
-23
test_subtensor.py
theano/tensor/tests/test_subtensor.py
+11
-7
没有找到文件。
theano/gof/null_type.py
浏览文件 @
e67a5ba0
...
@@ -33,7 +33,7 @@ class NullType(Type):
...
@@ -33,7 +33,7 @@ class NullType(Type):
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
return
type
(
self
)
==
type
(
other
)
def
__hash__
(
self
,
other
):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
return
hash
(
type
(
self
))
def
__str__
(
self
):
def
__str__
(
self
):
...
...
theano/tensor/subtensor.py
浏览文件 @
e67a5ba0
...
@@ -1544,32 +1544,42 @@ class IncSubtensor(Op):
...
@@ -1544,32 +1544,42 @@ class IncSubtensor(Op):
else
:
else
:
gx
=
g_output
gx
=
g_output
gy
=
Subtensor
(
idx_list
=
self
.
idx_list
)(
g_output
,
*
idx_list
)
gy
=
Subtensor
(
idx_list
=
self
.
idx_list
)(
g_output
,
*
idx_list
)
if
gy
.
broadcastable
!=
y
.
broadcastable
:
gy
=
_sum_grad_over_bcasted_dims
(
y
,
gy
)
y_dim_added
=
gy
.
ndim
-
y
.
ndim
y_broad
=
(
True
,)
*
y_dim_added
+
y
.
broadcastable
assert
sum
(
gy
.
broadcastable
)
<
sum
(
y_broad
)
axis_to_sum
=
[]
for
i
in
range
(
gy
.
ndim
):
if
gy
.
broadcastable
[
i
]
is
False
and
y_broad
[
i
]
is
True
:
axis_to_sum
.
append
(
i
)
elif
(
gy
.
broadcastable
[
i
]
is
True
and
y_broad
[
i
]
is
False
):
# This mean that Theano where able to infer that
# gy.shape[i] is 1, so y.shape[i] is 1, but we
# didn't know it. It is fine.
pass
else
:
assert
gy
.
broadcastable
[
i
]
==
y_broad
[
i
]
gy
=
gy
.
sum
(
axis
=
axis_to_sum
,
keepdims
=
True
)
if
gy
.
ndim
!=
y
.
ndim
:
assert
gy
.
ndim
>
y
.
ndim
for
i
in
range
(
y_dim_added
):
assert
gy
.
broadcastable
[
i
]
gy
=
gy
.
dimshuffle
(
*
range
(
y_dim_added
,
gy
.
ndim
))
assert
gy
.
broadcastable
==
y
.
broadcastable
return
[
gx
,
gy
]
+
[
DisconnectedType
()()]
*
len
(
idx_list
)
return
[
gx
,
gy
]
+
[
DisconnectedType
()()]
*
len
(
idx_list
)
def
_sum_grad_over_bcasted_dims
(
x
,
gx
):
"""Sum of gx over dimensions to reproduce x.broadcastable.
This is useful to sum gradients over certain dimensions when
x has been broadcasted, and we need to sum the gradient contributions
over all duplications.
"""
if
gx
.
broadcastable
!=
x
.
broadcastable
:
x_dim_added
=
gx
.
ndim
-
x
.
ndim
x_broad
=
(
True
,)
*
x_dim_added
+
x
.
broadcastable
assert
sum
(
gx
.
broadcastable
)
<
sum
(
x_broad
)
axis_to_sum
=
[]
for
i
in
range
(
gx
.
ndim
):
if
gx
.
broadcastable
[
i
]
is
False
and
x_broad
[
i
]
is
True
:
axis_to_sum
.
append
(
i
)
elif
(
gx
.
broadcastable
[
i
]
is
True
and
x_broad
[
i
]
is
False
):
# This means that Theano was able to infer that
# gx.shape[i] is 1, so x.shape[i] is 1, but we
# didn't know it. It is fine.
pass
else
:
assert
gx
.
broadcastable
[
i
]
==
x_broad
[
i
]
gx
=
gx
.
sum
(
axis
=
axis_to_sum
,
keepdims
=
True
)
if
gx
.
ndim
!=
x
.
ndim
:
assert
gx
.
ndim
>
x
.
ndim
for
i
in
range
(
x_dim_added
):
assert
gx
.
broadcastable
[
i
]
gx
=
gx
.
dimshuffle
(
*
range
(
x_dim_added
,
gx
.
ndim
))
assert
gx
.
broadcastable
==
x
.
broadcastable
return
gx
#########################
#########################
# Advanced indexing
# Advanced indexing
...
@@ -2247,6 +2257,9 @@ class AdvancedIncSubtensor(Op):
...
@@ -2247,6 +2257,9 @@ class AdvancedIncSubtensor(Op):
else
:
else
:
gx
=
outgrad
gx
=
outgrad
gy
=
advanced_subtensor
(
outgrad
,
*
idxs
)
gy
=
advanced_subtensor
(
outgrad
,
*
idxs
)
# Make sure to sum gy over the dimensions of y that have been
# added or broadcasted
gy
=
_sum_grad_over_bcasted_dims
(
y
,
gy
)
return
[
gx
,
gy
]
+
\
return
[
gx
,
gy
]
+
\
[
DisconnectedType
()()
for
_
in
idxs
]
[
DisconnectedType
()()
for
_
in
idxs
]
...
...
theano/tensor/tests/test_subtensor.py
浏览文件 @
e67a5ba0
...
@@ -1328,20 +1328,24 @@ class TestAdvancedSubtensor(unittest.TestCase):
...
@@ -1328,20 +1328,24 @@ class TestAdvancedSubtensor(unittest.TestCase):
if
inplace_increment
is
None
:
if
inplace_increment
is
None
:
raise
inplace_increment_missing
raise
inplace_increment_missing
a
=
inc_subtensor
(
self
.
m
[
self
.
ix1
,
self
.
ix12
],
2.1
)
inc
=
dscalar
()
a
=
inc_subtensor
(
self
.
m
[
self
.
ix1
,
self
.
ix12
],
inc
)
g_inc
=
tensor
.
grad
(
a
.
sum
(),
inc
)
assert
a
.
type
==
self
.
m
.
type
,
(
a
.
type
,
self
.
m
.
type
)
assert
a
.
type
==
self
.
m
.
type
,
(
a
.
type
,
self
.
m
.
type
)
f
=
theano
.
function
([
self
.
m
,
self
.
ix1
,
self
.
ix12
],
a
,
f
=
theano
.
function
([
self
.
m
,
self
.
ix1
,
self
.
ix12
,
inc
],
[
a
,
g_inc
]
,
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
aval
=
f
([[
.
4
,
.
9
,
.
1
],
aval
,
gval
=
f
([[
.
4
,
.
9
,
.
1
],
[
5
,
6
,
7
],
[
5
,
6
,
7
],
[
.
5
,
.
3
,
.
15
]],
[
.
5
,
.
3
,
.
15
]],
[
1
,
2
,
1
],
[
1
,
2
,
1
],
[
0
,
1
,
0
])
[
0
,
1
,
0
],
2.1
)
assert
numpy
.
allclose
(
aval
,
assert
numpy
.
allclose
(
aval
,
[[
.
4
,
.
9
,
.
1
],
[[
.
4
,
.
9
,
.
1
],
[
5
+
2.1
*
2
,
6
,
7
],
[
5
+
2.1
*
2
,
6
,
7
],
[
.
5
,
.
3
+
2.1
,
.
15
]]),
aval
[
.
5
,
.
3
+
2.1
,
.
15
]]),
aval
assert
numpy
.
allclose
(
gval
,
3.0
),
gval
def
test_inc_adv_subtensor_with_index_broadcasting
(
self
):
def
test_inc_adv_subtensor_with_index_broadcasting
(
self
):
if
inplace_increment
is
None
:
if
inplace_increment
is
None
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论