提交 84d55a8f authored 作者: Frederic's avatar Frederic

pep8

上级 cd50d5ef
...@@ -10,7 +10,7 @@ import theano.sandbox.cuda as cuda_ndarray ...@@ -10,7 +10,7 @@ import theano.sandbox.cuda as cuda_ndarray
from theano.tensor.basic import _allclose from theano.tensor.basic import _allclose
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
if cuda_ndarray.cuda_available == False: if not cuda_ndarray.cuda_available:
raise SkipTest('Optional package cuda disabled') raise SkipTest('Optional package cuda disabled')
...@@ -29,14 +29,16 @@ def advantage(cpu_dt, gpu_dt): ...@@ -29,14 +29,16 @@ def advantage(cpu_dt, gpu_dt):
else: else:
return cpu_dt / gpu_dt return cpu_dt / gpu_dt
def test_host_to_device(): def test_host_to_device():
#print >>sys.stdout, 'starting test_host_to_dev' #print >>sys.stdout, 'starting test_host_to_dev'
for shape in ((), (3,), (2,3), (3,4,5,6)): for shape in ((), (3,), (2, 3), (3, 4, 5, 6)):
a = theano._asarray(numpy.random.rand(*shape), dtype='float32') a = theano._asarray(numpy.random.rand(*shape), dtype='float32')
b = cuda_ndarray.CudaNdarray(a) b = cuda_ndarray.CudaNdarray(a)
c = numpy.asarray(b) c = numpy.asarray(b)
assert numpy.all(a == c) assert numpy.all(a == c)
def test_add_iadd_idiv(): def test_add_iadd_idiv():
for shapes in ( for shapes in (
[(5,5),(5,1)], [(5,5),(5,1)],
...@@ -52,7 +54,7 @@ def test_add_iadd_idiv(): ...@@ -52,7 +54,7 @@ def test_add_iadd_idiv():
(33,34,3,36,37), (33,34,3,36,37),
(33,34,35,36,3), (33,34,35,36,3),
): ):
if isinstance(shapes,tuple): if isinstance(shapes, tuple):
shape = shapes shape = shapes
shape2 = shapes shape2 = shapes
a0 = theano._asarray(numpy.random.rand(*shape), dtype='float32') a0 = theano._asarray(numpy.random.rand(*shape), dtype='float32')
...@@ -91,7 +93,7 @@ def test_add_iadd_idiv(): ...@@ -91,7 +93,7 @@ def test_add_iadd_idiv():
#should raise not implemented. #should raise not implemented.
a0 = a0_orig.copy() a0 = a0_orig.copy()
b0 = cuda_ndarray.CudaNdarray(a0) b0 = cuda_ndarray.CudaNdarray(a0)
if len(shape)==0: if len(shape) == 0:
continue continue
elif len(shape) == 1: elif len(shape) == 1:
_b = b1[::-1] _b = b1[::-1]
...@@ -141,11 +143,15 @@ def test_add_iadd_idiv(): ...@@ -141,11 +143,15 @@ def test_add_iadd_idiv():
b0 /= _b b0 /= _b
a0 /= a1[..., ::-1] a0 /= a1[..., ::-1]
assert numpy.allclose(a0, numpy.asarray(b0)) assert numpy.allclose(a0, numpy.asarray(b0))
assert numpy.allclose(a0, ((a0_orig+a1)/a1+a1[..., ::-1])/a1[..., ::-1]) assert numpy.allclose(a0, ((a0_orig + a1) / a1 +
a1[..., ::-1]) / a1[..., ::-1])
def test_exp(): def test_exp():
#print >>sys.stdout, 'starting test_exp' #print >>sys.stdout, 'starting test_exp'
for shape in ((), (3,), (2,3), (1,10000000),(10,1000000), (100,100000),(1000,10000),(10000,1000)): for shape in ((), (3,), (2, 3),
(1, 10000000), (10, 1000000),
(100, 100000), (1000, 10000), (10000, 1000)):
a0 = theano._asarray(numpy.random.rand(*shape), dtype='float32') a0 = theano._asarray(numpy.random.rand(*shape), dtype='float32')
a1 = a0.copy() a1 = a0.copy()
b0 = cuda_ndarray.CudaNdarray(a0) b0 = cuda_ndarray.CudaNdarray(a0)
...@@ -181,90 +187,108 @@ def test_copy(): ...@@ -181,90 +187,108 @@ def test_copy():
assert numpy.allclose(a, numpy.asarray(b)) assert numpy.allclose(a, numpy.asarray(b))
assert numpy.allclose(a, numpy.asarray(c)) assert numpy.allclose(a, numpy.asarray(c))
assert numpy.allclose(a, numpy.asarray(d)) assert numpy.allclose(a, numpy.asarray(d))
b+=b b += b
assert numpy.allclose(a+a, numpy.asarray(b)) assert numpy.allclose(a+a, numpy.asarray(b))
assert numpy.allclose(a+a, numpy.asarray(c)) assert numpy.allclose(a+a, numpy.asarray(c))
assert numpy.allclose(a, numpy.asarray(d)) assert numpy.allclose(a, numpy.asarray(d))
def test_nvcc_bug(): def test_nvcc_bug():
""" """
The fct k_elemwise_unary_rowmajor_copy(used by cuda.copy()) in cuda_ndarray.cu The fct k_elemwise_unary_rowmajor_copy(used by cuda.copy()) in cuda_ndarray.cu
is not well compiled with nvcc 3.0 and 3.1 beta. We found a workaround, so it is not well compiled with nvcc 3.0 and 3.1 beta. We found a workaround, so it
sould work correctly. Without the workaround, this test fail. sould work correctly. Without the workaround, this test fail.
""" """
shape = (5,4) shape = (5, 4)
aa = theano._asarray(numpy.random.rand(*shape), dtype='float32') aa = theano._asarray(numpy.random.rand(*shape), dtype='float32')
a = aa[::,::-1] a = aa[::, ::-1]
b = cuda_ndarray.CudaNdarray(aa)[::,::-1] b = cuda_ndarray.CudaNdarray(aa)[::, ::-1]
c = copy.copy(b) c = copy.copy(b)
d = copy.deepcopy(b) d = copy.deepcopy(b)
assert numpy.allclose(a, numpy.asarray(b)) assert numpy.allclose(a, numpy.asarray(b))
assert numpy.allclose(a, numpy.asarray(c)) assert numpy.allclose(a, numpy.asarray(c))
assert numpy.allclose(a, numpy.asarray(d)) assert numpy.allclose(a, numpy.asarray(d))
b+=b b += b
assert numpy.allclose(a+a, numpy.asarray(b)) assert numpy.allclose(a+a, numpy.asarray(b))
assert numpy.allclose(a+a, numpy.asarray(c)) assert numpy.allclose(a+a, numpy.asarray(c))
assert numpy.allclose(a, numpy.asarray(d)) assert numpy.allclose(a, numpy.asarray(d))
class test_DimShuffle(unittest.TestCase): class test_DimShuffle(unittest.TestCase):
def test_dimshuffle(self): def test_dimshuffle(self):
utt.seed_rng() utt.seed_rng()
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
# 2d -> 0d # 2d -> 0d
a = theano._asarray(rng.randn(1,1), dtype='float32') a = theano._asarray(rng.randn(1, 1), dtype='float32')
b = cuda_ndarray.CudaNdarray(a) b = cuda_ndarray.CudaNdarray(a)
assert numpy.allclose(numpy.transpose(a), cuda_ndarray.dimshuffle(b,())) assert numpy.allclose(numpy.transpose(a),
cuda_ndarray.dimshuffle(b, ()))
# Test when we drop a axis that don't have shape 1 # Test when we drop a axis that don't have shape 1
a = theano._asarray(rng.randn(2,1), dtype='float32') a = theano._asarray(rng.randn(2, 1), dtype='float32')
b = cuda_ndarray.CudaNdarray(a) b = cuda_ndarray.CudaNdarray(a)
self.assertRaises(ValueError, cuda_ndarray.dimshuffle, b,()) self.assertRaises(ValueError, cuda_ndarray.dimshuffle, b, ())
# Test that we can't take a dimensions multiple time # Test that we can't take a dimensions multiple time
a = theano._asarray(rng.randn(2,1), dtype='float32') a = theano._asarray(rng.randn(2, 1), dtype='float32')
b = cuda_ndarray.CudaNdarray(a) b = cuda_ndarray.CudaNdarray(a)
self.assertRaises(ValueError, cuda_ndarray.dimshuffle, b,(1,1)) self.assertRaises(ValueError, cuda_ndarray.dimshuffle, b, (1, 1))
# 1d # 1d
a = theano._asarray(rng.randn(3,), dtype='float32') a = theano._asarray(rng.randn(3,), dtype='float32')
b = cuda_ndarray.CudaNdarray(a) b = cuda_ndarray.CudaNdarray(a)
assert numpy.allclose(numpy.transpose(a), cuda_ndarray.dimshuffle(b,(0,))) assert numpy.allclose(numpy.transpose(a),
assert numpy.allclose(a[None,:,None], cuda_ndarray.dimshuffle(b,(-1,0,-1))) cuda_ndarray.dimshuffle(b, (0,)))
assert numpy.allclose(a[None, :, None],
cuda_ndarray.dimshuffle(b, (-1, 0, -1)))
# 2d # 2d
a = theano._asarray(rng.randn(3,11), dtype='float32') a = theano._asarray(rng.randn(3, 11), dtype='float32')
b = cuda_ndarray.CudaNdarray(a) b = cuda_ndarray.CudaNdarray(a)
assert numpy.allclose(numpy.transpose(a), cuda_ndarray.dimshuffle(b,(1,0))) assert numpy.allclose(numpy.transpose(a),
assert numpy.allclose(numpy.transpose(a)[None,:,None,:,None], cuda_ndarray.dimshuffle(b,(-1,1,-1,0,-1))) cuda_ndarray.dimshuffle(b, (1, 0)))
assert numpy.allclose(numpy.transpose(a)[None, :, None, :, None],
cuda_ndarray.dimshuffle(b, (-1, 1, -1, 0, -1)))
# 2d -> 1d # 2d -> 1d
a = theano._asarray(rng.randn(1,11), dtype='float32') a = theano._asarray(rng.randn(1, 11), dtype='float32')
b = cuda_ndarray.CudaNdarray(a) b = cuda_ndarray.CudaNdarray(a)
assert numpy.allclose(a[:,], cuda_ndarray.dimshuffle(b,(1,))) assert numpy.allclose(a[:],
a = theano._asarray(rng.randn(11,1), dtype='float32') cuda_ndarray.dimshuffle(b, (1,)))
a = theano._asarray(rng.randn(11, 1), dtype='float32')
b = cuda_ndarray.CudaNdarray(a) b = cuda_ndarray.CudaNdarray(a)
assert numpy.allclose(a.reshape((11,)), cuda_ndarray.dimshuffle(b,(0,))) assert numpy.allclose(a.reshape((11,)),
cuda_ndarray.dimshuffle(b, (0,)))
# 3d # 3d
a = theano._asarray(rng.randn(3,4,5), dtype='float32') a = theano._asarray(rng.randn(3, 4, 5), dtype='float32')
b = cuda_ndarray.CudaNdarray(a) b = cuda_ndarray.CudaNdarray(a)
assert numpy.allclose(a, cuda_ndarray.dimshuffle(b,(0,1,2))) assert numpy.allclose(a, cuda_ndarray.dimshuffle(b, (0, 1, 2)))
assert numpy.allclose(numpy.swapaxes(a,0,1), cuda_ndarray.dimshuffle(b,(1,0,2))) assert numpy.allclose(numpy.swapaxes(a, 0, 1),
assert numpy.allclose(numpy.swapaxes(a,0,2), cuda_ndarray.dimshuffle(b,(2,1,0))) cuda_ndarray.dimshuffle(b, (1, 0, 2)))
assert numpy.allclose(numpy.swapaxes(a,1,2), cuda_ndarray.dimshuffle(b,(0,2,1))) assert numpy.allclose(numpy.swapaxes(a, 0, 2),
assert numpy.allclose(numpy.swapaxes(a,1,2)[None,:,None,:,:,None], cuda_ndarray.dimshuffle(b,(-1,0,-1,2,1,-1))) cuda_ndarray.dimshuffle(b, (2, 1, 0)))
assert numpy.allclose(numpy.swapaxes(a, 1, 2),
cuda_ndarray.dimshuffle(b, (0, 2, 1)))
assert numpy.allclose(numpy.swapaxes(a, 1, 2)[None, :, None, :, :, None],
cuda_ndarray.dimshuffle(b, (-1, 0, -1, 2, 1, -1)))
# 4d # 4d
a = theano._asarray(rng.randn(3,11,4,5), dtype='float32') a = theano._asarray(rng.randn(3, 11, 4, 5), dtype='float32')
b = cuda_ndarray.CudaNdarray(a) b = cuda_ndarray.CudaNdarray(a)
assert numpy.allclose(numpy.swapaxes(a,0,1), cuda_ndarray.dimshuffle(b,(1,0,2,3))) assert numpy.allclose(numpy.swapaxes(a, 0, 1),
assert numpy.allclose(numpy.swapaxes(a,0,2), cuda_ndarray.dimshuffle(b,(2,1,0,3))) cuda_ndarray.dimshuffle(b, (1, 0, 2, 3)))
assert numpy.allclose(numpy.swapaxes(a,0,3), cuda_ndarray.dimshuffle(b,(3,1,2,0))) assert numpy.allclose(numpy.swapaxes(a, 0, 2),
assert numpy.allclose(numpy.swapaxes(a,0,3), cuda_ndarray.dimshuffle(b,(3,1,2,0))) cuda_ndarray.dimshuffle(b, (2, 1, 0, 3)))
assert numpy.allclose(numpy.swapaxes(a,0,3)[None,:,None,:,:,:], cuda_ndarray.dimshuffle(b,(-1,3,-1,1,2,0))) assert numpy.allclose(numpy.swapaxes(a, 0, 3),
cuda_ndarray.dimshuffle(b, (3, 1, 2, 0)))
assert numpy.allclose(numpy.swapaxes(a, 0, 3),
cuda_ndarray.dimshuffle(b, (3, 1, 2, 0)))
assert numpy.allclose(numpy.swapaxes(a, 0, 3)[None, :, None, :, :, :],
cuda_ndarray.dimshuffle(b, (-1, 3, -1, 1, 2, 0)))
def test_dot(): def test_dot():
...@@ -281,12 +305,11 @@ def test_dot(): ...@@ -281,12 +305,11 @@ def test_dot():
assert _allclose(numpy.dot(a0, a1), cuda_ndarray.dot(b0, b1)) assert _allclose(numpy.dot(a0, a1), cuda_ndarray.dot(b0, b1))
a1 = theano._asarray(rng.randn(6, 7), dtype='float32') a1 = theano._asarray(rng.randn(6, 7), dtype='float32')
b1 = cuda_ndarray.CudaNdarray(a1) b1 = cuda_ndarray.CudaNdarray(a1)
numpy_version = numpy.dot(a0, a1.T) numpy_version = numpy.dot(a0, a1.T)
transposed = cuda_ndarray.dimshuffle(b1,(1,0)) transposed = cuda_ndarray.dimshuffle(b1, (1, 0))
cuda_version = cuda_ndarray.dot(b0, transposed) cuda_version = cuda_ndarray.dot(b0, transposed)
assert _allclose(numpy_version, cuda_version) assert _allclose(numpy_version, cuda_version)
...@@ -294,53 +317,60 @@ def test_dot(): ...@@ -294,53 +317,60 @@ def test_dot():
a1 = theano._asarray(rng.randn(7, 6), dtype='float32') a1 = theano._asarray(rng.randn(7, 6), dtype='float32')
b1 = cuda_ndarray.CudaNdarray(a1) b1 = cuda_ndarray.CudaNdarray(a1)
a0 = theano._asarray(rng.randn(7, 4), dtype='float32') a0 = theano._asarray(rng.randn(7, 4), dtype='float32')
b0 = cuda_ndarray.CudaNdarray(a0) b0 = cuda_ndarray.CudaNdarray(a0)
assert _allclose(numpy.dot(a0.T, a1), assert _allclose(numpy.dot(a0.T, a1),
cuda_ndarray.dot(cuda_ndarray.dimshuffle(b0,(1,0)), b1)) cuda_ndarray.dot(cuda_ndarray.dimshuffle(b0, (1, 0)), b1))
a1 = theano._asarray(rng.randn(6, 7), dtype='float32') a1 = theano._asarray(rng.randn(6, 7), dtype='float32')
b1 = cuda_ndarray.CudaNdarray(a1) b1 = cuda_ndarray.CudaNdarray(a1)
assert _allclose(numpy.dot(a0.T, a1.T), assert _allclose(numpy.dot(a0.T, a1.T),
cuda_ndarray.dot(cuda_ndarray.dimshuffle(b0,(1,0)), cuda_ndarray.dot(cuda_ndarray.dimshuffle(b0, (1, 0)),
cuda_ndarray.dimshuffle(b1,(1,0)))) cuda_ndarray.dimshuffle(b1, (1, 0))))
def test_sum(): def test_sum():
shape = (2,3) shape = (2, 3)
a0 = theano._asarray(numpy.arange(shape[0]*shape[1]).reshape(shape), dtype='float32') a0 = theano._asarray(numpy.arange(shape[0] * shape[1]).reshape(shape),
dtype='float32')
b0 = cuda_ndarray.CudaNdarray(a0) b0 = cuda_ndarray.CudaNdarray(a0)
assert numpy.allclose(a0.sum(), numpy.asarray(b0.reduce_sum([1,1]))) assert numpy.allclose(a0.sum(),
numpy.asarray(b0.reduce_sum([1, 1])))
a0sum = a0.sum(axis=0) a0sum = a0.sum(axis=0)
b0sum = b0.reduce_sum([1,0]) b0sum = b0.reduce_sum([1, 0])
#print 'asum\n',a0sum #print 'asum\n',a0sum
#print 'bsum\n',numpy.asarray(b0sum) #print 'bsum\n',numpy.asarray(b0sum)
assert numpy.allclose(a0.sum(axis=0), numpy.asarray(b0.reduce_sum([1,0]))) assert numpy.allclose(a0.sum(axis=0),
assert numpy.allclose(a0.sum(axis=1), numpy.asarray(b0.reduce_sum([0,1]))) numpy.asarray(b0.reduce_sum([1, 0])))
assert numpy.allclose(a0, numpy.asarray(b0.reduce_sum([0,0]))) assert numpy.allclose(a0.sum(axis=1),
numpy.asarray(b0.reduce_sum([0, 1])))
assert numpy.allclose(a0, numpy.asarray(b0.reduce_sum([0, 0])))
shape = (3,4,5,6,7,8) shape = (3, 4, 5, 6, 7, 8)
a0 = theano._asarray(numpy.arange(3*4*5*6*7*8).reshape(shape), dtype='float32') a0 = theano._asarray(numpy.arange(3 * 4 * 5 * 6 * 7 * 8).reshape(shape),
dtype='float32')
b0 = cuda_ndarray.CudaNdarray(a0) b0 = cuda_ndarray.CudaNdarray(a0)
assert numpy.allclose(a0.sum(axis=5).sum(axis=3).sum(axis=0), numpy.asarray(b0.reduce_sum([1,0,0,1,0,1]))) assert numpy.allclose(a0.sum(axis=5).sum(axis=3).sum(axis=0),
numpy.asarray(b0.reduce_sum([1, 0, 0, 1, 0, 1])))
shape = (16,2048) shape = (16, 2048)
a0 = theano._asarray(numpy.arange(16*2048).reshape(shape), dtype='float32') a0 = theano._asarray(numpy.arange(16 * 2048).reshape(shape),
dtype='float32')
b0 = cuda_ndarray.CudaNdarray(a0) b0 = cuda_ndarray.CudaNdarray(a0)
assert numpy.allclose(a0.sum(axis=0), numpy.asarray(b0.reduce_sum([1,0]))) assert numpy.allclose(a0.sum(axis=0), numpy.asarray(b0.reduce_sum([1, 0])))
shape = (16,10) shape = (16, 10)
a0 = theano._asarray(numpy.arange(160).reshape(shape), dtype='float32') a0 = theano._asarray(numpy.arange(160).reshape(shape), dtype='float32')
b0 = cuda_ndarray.CudaNdarray(a0) b0 = cuda_ndarray.CudaNdarray(a0)
assert numpy.allclose(a0.sum(), numpy.asarray(b0.reduce_sum([1,1]))) assert numpy.allclose(a0.sum(), numpy.asarray(b0.reduce_sum([1, 1])))
def test_reshape(): def test_reshape():
shapelist = [ shapelist = [
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论