提交 879faed6 authored 作者: Frederic's avatar Frederic

New crash fix suggested by @rizar with corresponding test

上级 d73923e5
...@@ -2512,22 +2512,20 @@ class Alloc(gof.Op): ...@@ -2512,22 +2512,20 @@ class Alloc(gof.Op):
axis = range(n_axes_to_sum) axis = range(n_axes_to_sum)
# The broadcasted dimensions # The broadcasted dimensions
axis_broadcasted = [] axis_broadcasted = []
axis_kept = []
for i, (ib, gb) in enumerate( for i, (ib, gb) in enumerate(
zip(inputs[0].broadcastable, zip(inputs[0].broadcastable,
# We need the dimensions corresponding to x # We need the dimensions corresponding to x
grads[0].broadcastable[-inputs[0].ndim:])): grads[0].broadcastable[-inputs[0].ndim:])):
if ib and not gb: if ib and not gb:
axis_broadcasted.append(i + n_axes_to_sum) axis_broadcasted.append(i + n_axes_to_sum)
else:
axis_kept.append(i)
gx = gz.sum(axis=axis + axis_broadcasted) gx = gz.sum(axis=axis + axis_broadcasted)
if axis_broadcasted: if axis_broadcasted:
new_order = list(x.broadcastable) new_order = ['x'] * x.ndim
idx = 0 for idx, axis in enumerate(axis_kept):
for i in range(x.ndim): new_order[axis] = idx
if i not in axis_broadcasted:
new_order[i] = idx
idx += 1
else:
new_order[i] = 'x'
gx = gx.dimshuffle(new_order) gx = gx.dimshuffle(new_order)
# Dimshuffle to add back the broadcasted dims # Dimshuffle to add back the broadcasted dims
# The *elements* of the output are not connected to # The *elements* of the output are not connected to
......
...@@ -2073,6 +2073,16 @@ AllocDimshuffleGradTester = makeBroadcastTester( ...@@ -2073,6 +2073,16 @@ AllocDimshuffleGradTester = makeBroadcastTester(
x3=(rand(s3),), x3=(rand(s3),),
), ),
) )
AllocDimshuffleGradTester2 = makeBroadcastTester(
name='Allocb4GradTester',
op=lambda x: alloc(x.dimshuffle('x', 0), 1, s2, s3),
expected=(lambda x: numpy.zeros((1, s2, s3), dtype=x.dtype) + x),
grad=dict(
x1=(rand(s3),),
x2=(rand(s3),),
x3=(rand(s3),),
),
)
class ApplyDefaultTestOp(theano.Op): class ApplyDefaultTestOp(theano.Op):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论