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

pep8

上级 039f7b5c
...@@ -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
...@@ -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
...@@ -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)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论