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 个修改的文件
包含
33 行增加
和
20 行删除
+33
-20
subtensor.py
theano/tensor/subtensor.py
+33
-20
没有找到文件。
theano/tensor/subtensor.py
浏览文件 @
6b9267e8
...
@@ -1543,31 +1543,41 @@ class IncSubtensor(Op):
...
@@ -1543,31 +1543,41 @@ 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
return
[
gx
,
gy
]
+
[
DisconnectedType
()()]
*
len
(
idx_list
)
assert
sum
(
gy
.
broadcastable
)
<
sum
(
y_broad
)
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
=
[]
axis_to_sum
=
[]
for
i
in
range
(
gy
.
ndim
):
for
i
in
range
(
gx
.
ndim
):
if
gy
.
broadcastable
[
i
]
is
False
and
y
_broad
[
i
]
is
True
:
if
gx
.
broadcastable
[
i
]
is
False
and
x
_broad
[
i
]
is
True
:
axis_to_sum
.
append
(
i
)
axis_to_sum
.
append
(
i
)
elif
(
gy
.
broadcastable
[
i
]
is
True
and
elif
(
gx
.
broadcastable
[
i
]
is
True
and
y
_broad
[
i
]
is
False
):
x
_broad
[
i
]
is
False
):
# This mean that Theano where
able to infer that
# This means that Theano was
able to infer that
# gy.shape[i] is 1, so y
.shape[i] is 1, but we
# gx.shape[i] is 1, so x
.shape[i] is 1, but we
# didn't know it. It is fine.
# didn't know it. It is fine.
pass
pass
else
:
else
:
assert
gy
.
broadcastable
[
i
]
==
y_broad
[
i
]
assert
gx
.
broadcastable
[
i
]
==
x_broad
[
i
]
gy
=
gy
.
sum
(
axis
=
axis_to_sum
,
keepdims
=
True
)
gx
=
gx
.
sum
(
axis
=
axis_to_sum
,
keepdims
=
True
)
if
gy
.
ndim
!=
y
.
ndim
:
if
gx
.
ndim
!=
x
.
ndim
:
assert
gy
.
ndim
>
y
.
ndim
assert
gx
.
ndim
>
x
.
ndim
for
i
in
range
(
y_dim_added
):
for
i
in
range
(
x_dim_added
):
assert
gy
.
broadcastable
[
i
]
assert
gx
.
broadcastable
[
i
]
gy
=
gy
.
dimshuffle
(
*
range
(
y_dim_added
,
gy
.
ndim
))
gx
=
gx
.
dimshuffle
(
*
range
(
x_dim_added
,
gx
.
ndim
))
assert
gy
.
broadcastable
==
y
.
broadcastable
assert
gx
.
broadcastable
==
x
.
broadcastable
return
gx
return
[
gx
,
gy
]
+
[
DisconnectedType
()()]
*
len
(
idx_list
)
#########################
#########################
...
@@ -2183,6 +2193,9 @@ class AdvancedIncSubtensor(Op):
...
@@ -2183,6 +2193,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
]
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论