提交 6b7ac951 authored 作者: Dustin Webb's avatar Dustin Webb

Added code to calculate new shape when '-1' is provided as a value for a dimension.

上级 52ea2d0c
...@@ -2313,6 +2313,30 @@ class GpuReshape(tensor.Reshape, GpuOp): ...@@ -2313,6 +2313,30 @@ class GpuReshape(tensor.Reshape, GpuOp):
raise ValueError('shape argument to Reshape.perform' raise ValueError('shape argument to Reshape.perform'
' has incorrect length %i' ' has incorrect length %i'
', should be %i' % (len(shp), self.ndim), shp) ', 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
for idx, i in enumerate(shp):
if i == -1:
nb_m1 += 1
m1_idx = idx
else:
ss *= i
if nb_m1 > 1:
raise ValueError("Only one -1 is accepted in the new shape")
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.")
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)) out[0] = x.reshape(tuple(shp))
......
...@@ -10,6 +10,7 @@ import theano.tensor as T ...@@ -10,6 +10,7 @@ import theano.tensor as T
# Skip test if cuda_ndarray is not available. # Skip test if cuda_ndarray is not available.
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from nose.tools import assert_raises
import theano.sandbox.cuda as cuda_ndarray import theano.sandbox.cuda as cuda_ndarray
if cuda_ndarray.cuda_available == False: if cuda_ndarray.cuda_available == False:
raise SkipTest('Optional package cuda disabled') raise SkipTest('Optional package cuda disabled')
...@@ -336,6 +337,22 @@ def test_reshape(): ...@@ -336,6 +337,22 @@ def test_reshape():
return T.Reshape(2)(v, theano._asarray([2, 3], dtype='int32')) return T.Reshape(2)(v, theano._asarray([2, 3], dtype='int32'))
utt.verify_grad(just_vals, [a_val]) utt.verify_grad(just_vals, [a_val])
# 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')
)
assert y.shape == (8, 1, 1)
assert_raises(ValueError,
f_reshp=theano.function(
[x],
x.reshape((-1, -1, 1)),
mode=mode_with_gpu
)
)
def test_elemwise_empty(): def test_elemwise_empty():
#test with 0 element #test with 0 element
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论