提交 6131797e authored 作者: Frederic's avatar Frederic

pep8

上级 039f7b5c
...@@ -149,7 +149,7 @@ class test_GpuCAReduceCuda(test_GpuCAReduceCPY): ...@@ -149,7 +149,7 @@ class test_GpuCAReduceCuda(test_GpuCAReduceCPY):
# ((4100,4,3,2),[3]),((4,4100,3,2),[3]),((4,3,4100,2),[3]),((4,3,2,4100),[3]),#0001 # ((4100,4,3,2),[3]),((4,4100,3,2),[3]),((4,3,4100,2),[3]),((4,3,2,4100),[3]),#0001
# ((1100,2,3,4,5),[0,1,2,3,4]),((2,1100,3,4,5),[0,1,2,3,4]),((2,3,1100,4,5),[0,1,2,3,4]),((2,3,4,1100,5),[0,1,2,3,4]),((2,3,4,5,1100),[0,1,2,3,4]),#11111 # ((1100,2,3,4,5),[0,1,2,3,4]),((2,1100,3,4,5),[0,1,2,3,4]),((2,3,1100,4,5),[0,1,2,3,4]),((2,3,4,1100,5),[0,1,2,3,4]),((2,3,4,5,1100),[0,1,2,3,4]),#11111
# ((5,4,3,10,11),[1,2]), # ((5,4,3,10,11),[1,2]),
] ]
op = GpuCAReduceCuda op = GpuCAReduceCuda
reds = [scalar.add, scalar.mul, reds = [scalar.add, scalar.mul,
scalar.maximum, scalar.minimum] scalar.maximum, scalar.minimum]
......
...@@ -19,6 +19,7 @@ from theano.tensor.elemwise import (CAReduce, Elemwise, DimShuffle, ...@@ -19,6 +19,7 @@ from theano.tensor.elemwise import (CAReduce, Elemwise, DimShuffle,
from theano.tests import unittest_tools from theano.tests import unittest_tools
import math import math
def FunctionGraph(i, o): def FunctionGraph(i, o):
e = gof.FunctionGraph(i, o) e = gof.FunctionGraph(i, o)
return e return e
...@@ -46,8 +47,8 @@ class test_DimShuffle(unittest_tools.InferShapeTester): ...@@ -46,8 +47,8 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
#test that DimShuffle.infer_shape work correctly #test that DimShuffle.infer_shape work correctly
x = TensorType('float64', ib)('x') x = TensorType('float64', ib)('x')
e = self.op(ib, shuffle)(x) e = self.op(ib, shuffle)(x)
f = copy(linker).accept(FunctionGraph([x], [e. f = copy(linker).accept(FunctionGraph([x],
shape])).make_function() [e.shape])).make_function()
assert all(f(numpy.ones(xsh))) == all(zsh) assert all(f(numpy.ones(xsh))) == all(zsh)
# Test when we drop a axis that is not broadcastable # Test when we drop a axis that is not broadcastable
...@@ -100,44 +101,52 @@ class test_DimShuffle(unittest_tools.InferShapeTester): ...@@ -100,44 +101,52 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
y = x.dimshuffle(('x',) * (numpy.MAXDIMS + 1)) y = x.dimshuffle(('x',) * (numpy.MAXDIMS + 1))
self.assertRaises(ValueError, y.eval, {x: 0}) self.assertRaises(ValueError, y.eval, {x: 0})
class test_reduce_axes(unittest.TestCase): class test_reduce_axes(unittest.TestCase):
def test_sum_axes(self): def test_sum_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1), [numpy.array(0), numpy.array(1)]] axes = [None, 0, 1, [0, 1], numpy.array(1),
[numpy.array(0), numpy.array(1)]]
for a in axes: for a in axes:
x = tensor.matrix() x = tensor.matrix()
m = x.sum(a) m = x.sum(a)
def test_mean_axes(self): def test_mean_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1), [numpy.array(0), numpy.array(1)]] axes = [None, 0, 1, [0, 1], numpy.array(1),
[numpy.array(0), numpy.array(1)]]
for a in axes: for a in axes:
x = tensor.matrix() x = tensor.matrix()
m = x.mean(a) m = x.mean(a)
def test_max_axes(self): def test_max_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1), [numpy.array(0), numpy.array(1)]] axes = [None, 0, 1, [0, 1], numpy.array(1),
[numpy.array(0), numpy.array(1)]]
for a in axes: for a in axes:
x = tensor.matrix() x = tensor.matrix()
m = x.max(a) m = x.max(a)
def test_min_axes(self): def test_min_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1), [numpy.array(0), numpy.array(1)]] axes = [None, 0, 1, [0, 1], numpy.array(1),
[numpy.array(0), numpy.array(1)]]
for a in axes: for a in axes:
x = tensor.matrix() x = tensor.matrix()
m = x.min(a) m = x.min(a)
def test_argmax_axes(self): def test_argmax_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1), [numpy.array(0), numpy.array(1)]] axes = [None, 0, 1, [0, 1], numpy.array(1),
[numpy.array(0), numpy.array(1)]]
for a in axes: for a in axes:
x = tensor.matrix() x = tensor.matrix()
m = x.argmax(a) m = x.argmax(a)
def test_var_axes(self): def test_var_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1), [numpy.array(0), numpy.array(1)]] axes = [None, 0, 1, [0, 1], numpy.array(1),
[numpy.array(0), numpy.array(1)]]
for a in axes: for a in axes:
x = tensor.matrix() x = tensor.matrix()
m = x.var(a) m = x.var(a)
class test_Broadcast(unittest.TestCase): class test_Broadcast(unittest.TestCase):
# this is to allow other types to reuse this class to test their ops # this is to allow other types to reuse this class to test their ops
type = TensorType type = TensorType
...@@ -165,7 +174,10 @@ class test_Broadcast(unittest.TestCase): ...@@ -165,7 +174,10 @@ class test_Broadcast(unittest.TestCase):
((1, 5), (5, 1)), ((1, 5), (5, 1)),
((1, 1), (1, 1)), ((1, 1), (1, 1)),
((self.openmp_minsize,), (self.openmp_minsize,)), ((self.openmp_minsize,), (self.openmp_minsize,)),
((self.openmp_minsize_sqrt, self.openmp_minsize_sqrt), (self.openmp_minsize_sqrt, self.openmp_minsize_sqrt)), ((self.openmp_minsize_sqrt,
self.openmp_minsize_sqrt),
(self.openmp_minsize_sqrt,
self.openmp_minsize_sqrt)),
((2, 3, 4, 5), (2, 3, 4, 5)), ((2, 3, 4, 5), (2, 3, 4, 5)),
((2, 3, 4, 5), (1, 3, 1, 5)), ((2, 3, 4, 5), (1, 3, 1, 5)),
((2, 3, 4, 5), (1, 1, 1, 1)), ((2, 3, 4, 5), (1, 1, 1, 1)),
...@@ -186,8 +198,8 @@ class test_Broadcast(unittest.TestCase): ...@@ -186,8 +198,8 @@ class test_Broadcast(unittest.TestCase):
x = type('float64', [(entry == 1) for entry in xsh])('x') x = type('float64', [(entry == 1) for entry in xsh])('x')
y = type('float64', [(entry == 1) for entry in ysh])('y') y = type('float64', [(entry == 1) for entry in ysh])('y')
e = op(scalar.add)(x, y) e = op(scalar.add)(x, y)
f = copy(linker).accept(FunctionGraph([x, f = copy(linker).accept(FunctionGraph(
y], [e.shape])).make_function() [x, y], [e.shape])).make_function()
assert tuple(f(xv, yv)) == tuple(zv.shape) assert tuple(f(xv, yv)) == tuple(zv.shape)
def with_linker_inplace(self, linker, op, type, rand_val): def with_linker_inplace(self, linker, op, type, rand_val):
...@@ -216,8 +228,8 @@ class test_Broadcast(unittest.TestCase): ...@@ -216,8 +228,8 @@ class test_Broadcast(unittest.TestCase):
x = type('float64', [(entry == 1) for entry in xsh])('x') x = type('float64', [(entry == 1) for entry in xsh])('x')
y = type('float64', [(entry == 1) for entry in ysh])('y') y = type('float64', [(entry == 1) for entry in ysh])('y')
e = op(scalar.Add(scalar.transfer_type(0)), {0: 0})(x, y) e = op(scalar.Add(scalar.transfer_type(0)), {0: 0})(x, y)
f = copy(linker).accept(FunctionGraph([x, f = copy(linker).accept(FunctionGraph(
y], [e.shape])).make_function() [x, y], [e.shape])).make_function()
xv = rand_val(xsh) xv = rand_val(xsh)
yv = rand_val(ysh) yv = rand_val(ysh)
zv = xv + yv zv = xv + yv
...@@ -309,7 +321,7 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -309,7 +321,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
((5, 0), ()), ((5, 0), ()),
((), None), ((), None),
((), ()) ((), ())
] ]
def with_linker(self, linker, scalar_op=scalar.add, dtype="floatX", def with_linker(self, linker, scalar_op=scalar.add, dtype="floatX",
pre_scalar_op=None, pre_scalar_op=None,
...@@ -429,7 +441,8 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -429,7 +441,8 @@ class test_CAReduce(unittest_tools.InferShapeTester):
try: try:
f_xv = f(xv) f_xv = f(xv)
self.assertTrue((f_xv.shape == zv.shape), (f_xv, zv)) self.assertTrue((f_xv.shape == zv.shape), (f_xv, zv))
self.assertTrue(numpy.allclose(f_xv, zv), (f_xv, zv, xsh, tosum)) self.assertTrue(numpy.allclose(f_xv, zv),
(f_xv, zv, xsh, tosum))
except NotImplementedError: except NotImplementedError:
# GpuCAReduce don't implement all cases when size is 0 # GpuCAReduce don't implement all cases when size is 0
assert xv.size == 0 assert xv.size == 0
...@@ -553,7 +566,7 @@ class test_Prod(unittest.TestCase): ...@@ -553,7 +566,7 @@ class test_Prod(unittest.TestCase):
# including zeros, as the case with zeros is important # including zeros, as the case with zeros is important
# (and special cases: 1 zero in the row, more than 1 zero in the row) # (and special cases: 1 zero in the row, more than 1 zero in the row)
x_val = numpy.asarray([[1, 2, 3], [4, 5, 6], [7, 8, 9]], x_val = numpy.asarray([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
dtype='float32') dtype='float32')
# now with verify_grad # now with verify_grad
unittest_tools.verify_grad(Prod(axis=1), [x_val], mode=self.mode) unittest_tools.verify_grad(Prod(axis=1), [x_val], mode=self.mode)
...@@ -568,7 +581,7 @@ class test_Prod(unittest.TestCase): ...@@ -568,7 +581,7 @@ class test_Prod(unittest.TestCase):
# including zeros, as the case with zeros is important # including zeros, as the case with zeros is important
# (and special cases: 1 zero in the row, more than 1 zero in the row) # (and special cases: 1 zero in the row, more than 1 zero in the row)
x_val = numpy.asarray([[1., 2., 3.], [0., 5., 6.], [0., 0., 9.]], x_val = numpy.asarray([[1., 2., 3.], [0., 5., 6.], [0., 0., 9.]],
dtype='float32') dtype='float32')
x = theano.tensor.dmatrix() x = theano.tensor.dmatrix()
# sanity check # sanity check
...@@ -760,7 +773,8 @@ class T_reduce_dtype(unittest.TestCase): ...@@ -760,7 +773,8 @@ class T_reduce_dtype(unittest.TestCase):
).get(dtype, dtype) ).get(dtype, dtype)
f = theano.function([x], s, mode=self.mode) f = theano.function([x], s, mode=self.mode)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert [n for n in topo if isinstance(n.op, self.op)], (topo, dtype) assert [n for n in topo if isinstance(n.op, self.op)], (topo,
dtype)
data = numpy.random.rand(3, 4) * 10 data = numpy.random.rand(3, 4) * 10
data = data.astype(dtype) data = data.astype(dtype)
f(data) f(data)
...@@ -785,7 +799,8 @@ class T_reduce_dtype(unittest.TestCase): ...@@ -785,7 +799,8 @@ class T_reduce_dtype(unittest.TestCase):
).get(dtype, dtype) ).get(dtype, dtype)
f = theano.function([x], s, mode=self.mode) f = theano.function([x], s, mode=self.mode)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert [n for n in topo if isinstance(n.op, self.op)], (topo, dtype) assert [n for n in topo if isinstance(n.op, self.op)], (topo,
dtype)
data = numpy.random.rand(3, 4) * 10 data = numpy.random.rand(3, 4) * 10
data = data.astype(dtype) data = data.astype(dtype)
f(data) f(data)
...@@ -814,7 +829,8 @@ class T_reduce_dtype(unittest.TestCase): ...@@ -814,7 +829,8 @@ class T_reduce_dtype(unittest.TestCase):
f = theano.function([x], var, mode=self.mode) f = theano.function([x], var, mode=self.mode)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert [n for n in topo if isinstance(n.op, self.op)], (topo, dtype) assert [n for n in topo if isinstance(n.op, self.op)], (topo,
dtype)
data = numpy.random.rand(3, 4) * 10 data = numpy.random.rand(3, 4) * 10
data = data.astype(input_dtype) data = data.astype(input_dtype)
f(data) f(data)
...@@ -850,7 +866,8 @@ class T_reduce_dtype(unittest.TestCase): ...@@ -850,7 +866,8 @@ class T_reduce_dtype(unittest.TestCase):
(input_dtype in tensor.discrete_dtypes and (input_dtype in tensor.discrete_dtypes and
acc_dtype in tensor.continuous_dtypes) acc_dtype in tensor.continuous_dtypes)
): ):
var = getattr(x, method)(acc_dtype=acc_dtype, axis=axis) var = getattr(x, method)(acc_dtype=acc_dtype,
axis=axis)
assert var.owner.op.acc_dtype == acc_dtype assert var.owner.op.acc_dtype == acc_dtype
if "complex" in input_dtype: if "complex" in input_dtype:
...@@ -873,10 +890,12 @@ class T_reduce_dtype(unittest.TestCase): ...@@ -873,10 +890,12 @@ class T_reduce_dtype(unittest.TestCase):
s = getattr(x, method)() s = getattr(x, method)()
f = theano.function([], s, mode=self.mode) f = theano.function([], s, mode=self.mode)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert [n for n in topo if isinstance(n.op, self.op)], (topo, dtype) assert [n for n in topo if isinstance(n.op, self.op)], (topo,
dtype)
s_val = f() s_val = f()
# Use extra precision in NumPy to compute the good answer. # Use extra precision in NumPy to compute the good answer.
ret = getattr(numpy.asarray([1e8, 1, -1e8], dtype='float64'), method)() ret = getattr(numpy.asarray([1e8, 1, -1e8], dtype='float64'),
method)()
assert numpy.allclose(s_val, ret), (s_val, ret) assert numpy.allclose(s_val, ret), (s_val, ret)
...@@ -922,10 +941,10 @@ class T_mean_dtype(unittest.TestCase): ...@@ -922,10 +941,10 @@ class T_mean_dtype(unittest.TestCase):
# Executed if no TypeError was raised # Executed if no TypeError was raised
if sum_dtype in tensor.discrete_dtypes and axis != []: if sum_dtype in tensor.discrete_dtypes and axis != []:
assert mean_var.dtype == 'float64', ( assert mean_var.dtype == 'float64', (
(mean_var.dtype, sum_dtype)) (mean_var.dtype, sum_dtype))
else: else:
assert mean_var.dtype == sum_dtype, ( assert mean_var.dtype == sum_dtype, (
(mean_var.dtype, sum_dtype)) (mean_var.dtype, sum_dtype))
if (('complex' in input_dtype or if (('complex' in input_dtype or
'complex' in sum_dtype) and 'complex' in sum_dtype) and
input_dtype != sum_dtype): input_dtype != sum_dtype):
...@@ -970,13 +989,13 @@ class T_prod_without_zeros_dtype(unittest.TestCase): ...@@ -970,13 +989,13 @@ class T_prod_without_zeros_dtype(unittest.TestCase):
axis = axes[idx % len(axes)] axis = axes[idx % len(axes)]
x = ProdWithoutZeros(axis=axis)(tensor.matrix(dtype=dtype)) x = ProdWithoutZeros(axis=axis)(tensor.matrix(dtype=dtype))
assert x.dtype == dict( assert x.dtype == dict(
int8='int64', int8='int64',
int16='int64', int16='int64',
int32='int64', int32='int64',
uint8='uint64', uint8='uint64',
uint16='uint64', uint16='uint64',
uint32='uint64', uint32='uint64',
).get(dtype, dtype) ).get(dtype, dtype)
def test_prod_without_zeros_default_acc_dtype(self): def test_prod_without_zeros_default_acc_dtype(self):
""" """
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论