Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
924e9d03
提交
924e9d03
authored
11月 04, 2013
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix grad of Alloc when we unbroadcast an input.
In some cases it was causing an error in the grad related to broadcasting. In the test it case bad shape.
上级
8bd900f8
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
80 行增加
和
5 行删除
+80
-5
basic.py
theano/tensor/basic.py
+22
-1
test_basic.py
theano/tensor/tests/test_basic.py
+58
-4
没有找到文件。
theano/tensor/basic.py
浏览文件 @
924e9d03
...
...
@@ -2547,7 +2547,28 @@ class Alloc(gof.Op):
x
=
inputs
[
0
]
gz
=
grads
[
0
]
n_axes_to_sum
=
gz
.
ndim
-
x
.
ndim
gx
=
gz
.
sum
(
axis
=
range
(
n_axes_to_sum
))
#The number of dimensions added
axis
=
range
(
n_axes_to_sum
)
#The broadcasted dimensions
axis_broadcasted
=
[]
for
i
,
(
ib
,
gb
)
in
enumerate
(
zip
(
inputs
[
0
]
.
broadcastable
,
#We need the dimensions corresponding to x
grads
[
0
]
.
broadcastable
[
-
inputs
[
0
]
.
ndim
:])):
if
ib
and
not
gb
:
axis_broadcasted
.
append
(
i
+
n_axes_to_sum
)
gx
=
gz
.
sum
(
axis
=
axis
+
axis_broadcasted
)
if
axis_broadcasted
:
new_order
=
list
(
x
.
broadcastable
)
idx
=
0
for
i
in
range
(
x
.
ndim
):
if
not
new_order
[
i
]:
new_order
[
i
]
=
idx
idx
+=
1
else
:
new_order
[
i
]
=
'x'
gx
=
gx
.
dimshuffle
(
new_order
)
#Dimshuffle to add back the broadcasted dims
#The *elements* of the output are not connected to
#the inputs that specify the shape. If you grow the
#shape by epsilon, the existing elements do not
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
924e9d03
...
...
@@ -1787,10 +1787,16 @@ AllocTester = makeBroadcastTester(
correct01_bcast
=
(
rand
(
1
),
numpy
.
int32
(
7
)),
correct02
=
(
rand
(),
numpy
.
int32
(
4
),
numpy
.
int32
(
7
)),
correct12
=
(
rand
(
7
),
numpy
.
int32
(
4
),
numpy
.
int32
(
7
)),
correct13
=
(
rand
(
7
),
numpy
.
int32
(
2
),
numpy
.
int32
(
4
),
numpy
.
int32
(
7
)),
correct23
=
(
rand
(
4
,
7
),
numpy
.
int32
(
2
),
numpy
.
int32
(
4
),
numpy
.
int32
(
7
)),
correct13
=
(
rand
(
7
),
numpy
.
int32
(
2
),
numpy
.
int32
(
4
),
numpy
.
int32
(
7
)),
correct23
=
(
rand
(
4
,
7
),
numpy
.
int32
(
2
),
numpy
.
int32
(
4
),
numpy
.
int32
(
7
)),
correctb1
=
(
rand
(
1
,
7
),
numpy
.
int32
(
4
),
numpy
.
int32
(
7
)),
correctb2
=
(
rand
(
1
,
7
),
numpy
.
int32
(
2
),
numpy
.
int32
(
4
),
numpy
.
int32
(
7
)),
correctb3
=
(
rand
(
7
,
1
),
numpy
.
int32
(
7
),
numpy
.
int32
(
4
)),
correctb4
=
(
rand
(
7
,
1
),
numpy
.
int32
(
2
),
numpy
.
int32
(
7
),
numpy
.
int32
(
4
)),
),
bad_runtime
=
dict
(
bad_shape12
=
(
rand
(
7
),
numpy
.
int32
(
7
),
numpy
.
int32
(
5
)),
...
...
@@ -1839,6 +1845,54 @@ Alloc13GradTester = makeBroadcastTester(
),
)
# unbroadcast a row to a matrix
Allocb1GradTester
=
makeBroadcastTester
(
name
=
'Allocb1GradTester'
,
op
=
lambda
x
:
alloc
(
x
,
s1
,
s2
),
expected
=
(
lambda
x
:
numpy
.
zeros
((
s1
,
s2
),
dtype
=
x
.
dtype
)
+
x
),
grad
=
dict
(
x1
=
(
rand
(
1
,
s2
),),
x2
=
(
rand
(
1
,
s2
),),
x3
=
(
rand
(
1
,
s2
),),
),
)
# unbroadcast a row to a tensor3
Allocb2GradTester
=
makeBroadcastTester
(
name
=
'Allocb2GradTester'
,
op
=
lambda
x
:
alloc
(
x
,
s1
,
s2
,
s3
),
expected
=
(
lambda
x
:
numpy
.
zeros
((
s1
,
s2
,
s3
),
dtype
=
x
.
dtype
)
+
x
),
grad
=
dict
(
x1
=
(
rand
(
1
,
s3
),),
x2
=
(
rand
(
1
,
s3
),),
x3
=
(
rand
(
1
,
s3
),),
),
)
# unbroadcast a col to a matrix
Allocb3GradTester
=
makeBroadcastTester
(
name
=
'Allocb3GradTester'
,
op
=
lambda
x
:
alloc
(
x
,
s1
,
s2
),
expected
=
(
lambda
x
:
numpy
.
zeros
((
s1
,
s2
),
dtype
=
x
.
dtype
)
+
x
),
grad
=
dict
(
x1
=
(
rand
(
s1
,
1
),),
x2
=
(
rand
(
s1
,
1
),),
x3
=
(
rand
(
s1
,
1
),),
),
)
# unbroadcast a col to a tensor3
Allocb4GradTester
=
makeBroadcastTester
(
name
=
'Allocb4GradTester'
,
op
=
lambda
x
:
alloc
(
x
,
s1
,
s2
,
s3
),
expected
=
(
lambda
x
:
numpy
.
zeros
((
s1
,
s2
,
s3
),
dtype
=
x
.
dtype
)
+
x
),
grad
=
dict
(
x1
=
(
rand
(
s2
,
1
),),
x2
=
(
rand
(
s2
,
1
),),
x3
=
(
rand
(
s2
,
1
),),
),
)
class
TestAlloc
(
unittest
.
TestCase
):
dtype
=
config
.
floatX
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论