提交 5659f1e0 authored 作者: Dustin Webb's avatar Dustin Webb

Optimized new code and improved the tests.

上级 6b7ac951
......@@ -2314,12 +2314,12 @@ class GpuReshape(tensor.Reshape, GpuOp):
' has incorrect length %i'
', should be %i' % (len(shp), self.ndim), shp)
m1_idx = -1
if shp.prod() != x.size:
# We need to do check here to raise the same error as NumPy.
# We should make pygpu do the same.
ss = 1
nb_m1 = 0
m1_idx = -1
for idx, i in enumerate(shp):
if i == -1:
nb_m1 += 1
......@@ -2331,12 +2331,13 @@ class GpuReshape(tensor.Reshape, GpuOp):
elif nb_m1 == 1:
if (x.size % ss) != 0:
raise ValueError("When using -1 in new shape, the computed new shape must be an multiple of the original shape.")
shp_new = numpy.copy(shp)
shp_new[m1_idx] = x.size/ss
shp = shp_new
else:
raise ValueError("total size of new array must be unchanged")
if m1_idx != -1:
shp[m1_idx] = x.size/(-1*shp.prod())
out[0] = x.reshape(tuple(shp))
......
......@@ -339,19 +339,22 @@ def test_reshape():
# Test for appropriate handling of -1 indices
x = T.tensor3('x')
f_reshp = theano.function([x], x.reshape((-1, 1, 1)), mode=mode_with_gpu)
y = f_reshp(
numpy.array([[[1, 0], [0, 1]], [[0, 1], [1, 0]]], dtype='float32')
)
dim = T.scalar('dim_val', dtype='int32')
reshp_val = numpy.array([[[1, 0], [0, 1]], [[0, 1], [1, 0]]], dtype='float32')
f_reshp = theano.function([x, dim], x.reshape((dim, 1, 1)), mode=mode_with_gpu)
y = f_reshp(reshp_val, -1)
assert y.shape == (8, 1, 1)
assert_raises(ValueError,
f_reshp=theano.function(
[x],
x.reshape((-1, -1, 1)),
mode=mode_with_gpu
)
f_reshp=theano.function(
[x, dim],
x.reshape((dim, dim, 1)),
mode=mode_with_gpu
)
try:
f_reshp(reshp_val, 4)
raise('Only one -1 is accepted in the new shape')
except ValueError:
pass
def test_elemwise_empty():
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论