Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6b9267e8
提交
6b9267e8
authored
12月 19, 2014
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Sum gradient over dimensions where the increment was broadcasted
上级
a4cb2200
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
36 行增加
和
23 行删除
+36
-23
subtensor.py
theano/tensor/subtensor.py
+36
-23
没有找到文件。
theano/tensor/subtensor.py
浏览文件 @
6b9267e8
...
...
@@ -1543,32 +1543,42 @@ class IncSubtensor(Op):
else
:
gx
=
g_output
gy
=
Subtensor
(
idx_list
=
self
.
idx_list
)(
g_output
,
*
idx_list
)
if
gy
.
broadcastable
!=
y
.
broadcastable
:
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
gy
=
_sum_grad_over_bcasted_dims
(
y
,
gy
)
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
...
...
@@ -2183,6 +2193,9 @@ class AdvancedIncSubtensor(Op):
else
:
gx
=
outgrad
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
]
+
\
[
DisconnectedType
()()
for
_
in
idxs
]
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论