提交 026e8116 authored 作者: Frederic's avatar Frederic

remove "import *"

上级 158c4b90
import cPickle, time, unittest import cPickle, time, unittest
from itertools import imap from itertools import imap
from copy import copy
import numpy
from numpy.testing import dec from numpy.testing import dec
import theano
from theano.gof import Variable, Op from theano.gof import Variable, Op
from theano import gof from theano import gof, scalar, config
from theano.scalar import *
from theano import tensor from theano import tensor
from theano.tensor import TensorType
from theano.compile.mode import get_default_mode from theano.compile.mode import get_default_mode
from theano.tensor.elemwise import * from theano.tensor.elemwise import CAReduce, Elemwise, DimShuffle, Prod, ProdWithoutZeros
from theano.tests import unittest_tools from theano.tests import unittest_tools
...@@ -83,7 +85,7 @@ class test_Broadcast(unittest.TestCase): ...@@ -83,7 +85,7 @@ class test_Broadcast(unittest.TestCase):
((), ())]: ((), ())]:
x = TensorType('float64', [(entry == 1) for entry in xsh])('x') x = TensorType('float64', [(entry == 1) for entry in xsh])('x')
y = TensorType('float64', [(entry == 1) for entry in ysh])('y') y = TensorType('float64', [(entry == 1) for entry in ysh])('y')
e = Elemwise(add)(x, y) e = Elemwise(scalar.add)(x, y)
f = copy(linker).accept(Env([x, y], [e])).make_function() f = copy(linker).accept(Env([x, y], [e])).make_function()
xv = numpy.asarray(numpy.random.rand(*xsh)) xv = numpy.asarray(numpy.random.rand(*xsh))
yv = numpy.asarray(numpy.random.rand(*ysh)) yv = numpy.asarray(numpy.random.rand(*ysh))
...@@ -96,7 +98,7 @@ class test_Broadcast(unittest.TestCase): ...@@ -96,7 +98,7 @@ class test_Broadcast(unittest.TestCase):
if isinstance(linker,gof.PerformLinker): if isinstance(linker,gof.PerformLinker):
x = TensorType('float64', [(entry == 1) for entry in xsh])('x') x = TensorType('float64', [(entry == 1) for entry in xsh])('x')
y = TensorType('float64', [(entry == 1) for entry in ysh])('y') y = TensorType('float64', [(entry == 1) for entry in ysh])('y')
e = Elemwise(add)(x, y) e = Elemwise(scalar.add)(x, y)
f = copy(linker).accept(Env([x, y], [e.shape])).make_function() f = copy(linker).accept(Env([x, y], [e.shape])).make_function()
assert tuple(f(xv, yv))==tuple(zv.shape) assert tuple(f(xv, yv))==tuple(zv.shape)
...@@ -111,7 +113,7 @@ class test_Broadcast(unittest.TestCase): ...@@ -111,7 +113,7 @@ class test_Broadcast(unittest.TestCase):
((), ())]: ((), ())]:
x = TensorType('float64', [(entry == 1) for entry in xsh])('x') x = TensorType('float64', [(entry == 1) for entry in xsh])('x')
y = TensorType('float64', [(entry == 1) for entry in ysh])('y') y = TensorType('float64', [(entry == 1) for entry in ysh])('y')
e = Elemwise(Add(transfer_type(0)), {0:0})(x, y) e = Elemwise(scalar.Add(scalar.transfer_type(0)), {0:0})(x, y)
f = copy(linker).accept(Env([x, y], [e])).make_function() f = copy(linker).accept(Env([x, y], [e])).make_function()
xv = numpy.asarray(numpy.random.rand(*xsh)) xv = numpy.asarray(numpy.random.rand(*xsh))
yv = numpy.asarray(numpy.random.rand(*ysh)) yv = numpy.asarray(numpy.random.rand(*ysh))
...@@ -125,7 +127,7 @@ class test_Broadcast(unittest.TestCase): ...@@ -125,7 +127,7 @@ class test_Broadcast(unittest.TestCase):
if isinstance(linker,gof.PerformLinker): if isinstance(linker,gof.PerformLinker):
x = TensorType('float64', [(entry == 1) for entry in xsh])('x') x = TensorType('float64', [(entry == 1) for entry in xsh])('x')
y = TensorType('float64', [(entry == 1) for entry in ysh])('y') y = TensorType('float64', [(entry == 1) for entry in ysh])('y')
e = Elemwise(Add(transfer_type(0)), {0:0})(x, y) e = Elemwise(scalar.Add(scalar.transfer_type(0)), {0:0})(x, y)
f = copy(linker).accept(Env([x, y], [e.shape])).make_function() f = copy(linker).accept(Env([x, y], [e.shape])).make_function()
xv = numpy.asarray(numpy.random.rand(*xsh)) xv = numpy.asarray(numpy.random.rand(*xsh))
yv = numpy.asarray(numpy.random.rand(*ysh)) yv = numpy.asarray(numpy.random.rand(*ysh))
...@@ -150,7 +152,7 @@ class test_Broadcast(unittest.TestCase): ...@@ -150,7 +152,7 @@ class test_Broadcast(unittest.TestCase):
def test_fill(self): def test_fill(self):
x = TensorType('float64', [0, 0])('x') x = TensorType('float64', [0, 0])('x')
y = TensorType('float64', [1, 1])('y') y = TensorType('float64', [1, 1])('y')
e = Elemwise(Second(transfer_type(0)), {0:0})(x, y) e = Elemwise(scalar.Second(scalar.transfer_type(0)), {0:0})(x, y)
f = gof.CLinker().accept(Env([x, y], [e])).make_function() f = gof.CLinker().accept(Env([x, y], [e])).make_function()
xv = numpy.ones((5, 5)) xv = numpy.ones((5, 5))
yv = numpy.random.rand(1, 1) yv = numpy.random.rand(1, 1)
...@@ -160,7 +162,7 @@ class test_Broadcast(unittest.TestCase): ...@@ -160,7 +162,7 @@ class test_Broadcast(unittest.TestCase):
def test_weird_strides(self): def test_weird_strides(self):
x = TensorType('float64', [0, 0, 0, 0, 0])('x') x = TensorType('float64', [0, 0, 0, 0, 0])('x')
y = TensorType('float64', [0, 0, 0, 0, 0])('y') y = TensorType('float64', [0, 0, 0, 0, 0])('y')
e = Elemwise(add)(x, y) e = Elemwise(scalar.add)(x, y)
f = gof.CLinker().accept(Env([x, y], [e])).make_function() f = gof.CLinker().accept(Env([x, y], [e])).make_function()
xv = numpy.random.rand(2, 2, 2, 2, 2) xv = numpy.random.rand(2, 2, 2, 2, 2)
yv = numpy.random.rand(2, 2, 2, 2, 2).transpose(4, 0, 3, 1, 2) yv = numpy.random.rand(2, 2, 2, 2, 2).transpose(4, 0, 3, 1, 2)
...@@ -169,7 +171,7 @@ class test_Broadcast(unittest.TestCase): ...@@ -169,7 +171,7 @@ class test_Broadcast(unittest.TestCase):
def test_same_inputs(self): def test_same_inputs(self):
x = TensorType('float64', [0, 0])('x') x = TensorType('float64', [0, 0])('x')
e = Elemwise(add)(x, x) e = Elemwise(scalar.add)(x, x)
f = gof.CLinker().accept(Env([x], [e])).make_function() f = gof.CLinker().accept(Env([x], [e])).make_function()
xv = numpy.random.rand(2, 2) xv = numpy.random.rand(2, 2)
zv = xv + xv zv = xv + xv
...@@ -180,7 +182,7 @@ class test_CAReduce(unittest.TestCase): ...@@ -180,7 +182,7 @@ class test_CAReduce(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() unittest_tools.seed_rng()
def with_linker(self, linker, scalar_op = add, dtype="floatX", def with_linker(self, linker, scalar_op=scalar.add, dtype="floatX",
test_nan=False, tensor_op=None): test_nan=False, tensor_op=None):
for xsh, tosum in [((5, 6), None), for xsh, tosum in [((5, 6), None),
((5, 6), (0, 1)), ((5, 6), (0, 1)),
...@@ -241,31 +243,31 @@ class test_CAReduce(unittest.TestCase): ...@@ -241,31 +243,31 @@ class test_CAReduce(unittest.TestCase):
zv = numpy.any(zv, axis) zv = numpy.any(zv, axis)
if len(tosum) == 0: if len(tosum) == 0:
zv = zv != 0 zv = zv != 0
elif scalar_op == add: elif scalar_op == scalar.add:
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = numpy.add.reduce(zv, axis) zv = numpy.add.reduce(zv, axis)
elif scalar_op == mul: elif scalar_op == scalar.mul:
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = numpy.multiply.reduce(zv, axis) zv = numpy.multiply.reduce(zv, axis)
elif scalar_op == maximum: elif scalar_op == scalar.maximum:
try: try:
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = numpy.maximum.reduce(zv, axis) zv = numpy.maximum.reduce(zv, axis)
except ValueError: except ValueError:
numpy_raised=True numpy_raised=True
elif scalar_op == minimum: elif scalar_op == scalar.minimum:
try: try:
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = numpy.minimum.reduce(zv, axis) zv = numpy.minimum.reduce(zv, axis)
except ValueError: except ValueError:
numpy_raised=True numpy_raised=True
elif scalar_op == or_: elif scalar_op == scalar.or_:
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = numpy.bitwise_or.reduce(zv, axis) zv = numpy.bitwise_or.reduce(zv, axis)
elif scalar_op == and_: elif scalar_op == scalar.and_:
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = numpy.bitwise_and.reduce(zv, axis) zv = numpy.bitwise_and.reduce(zv, axis)
elif scalar_op == xor: elif scalar_op == scalar.xor:
# There is no identity value for the xor function # There is no identity value for the xor function
# So we can't support shape of dimensions 0. # So we can't support shape of dimensions 0.
if numpy.prod(zv.shape)==0: if numpy.prod(zv.shape)==0:
...@@ -274,7 +276,7 @@ class test_CAReduce(unittest.TestCase): ...@@ -274,7 +276,7 @@ class test_CAReduce(unittest.TestCase):
zv = numpy.bitwise_xor.reduce(zv, axis) zv = numpy.bitwise_xor.reduce(zv, axis)
else: else:
raise Exception("Test for CAReduce with scalar_op %s not implemented"%str(scalar_op)) raise Exception("Test for CAReduce with scalar_op %s not implemented"%str(scalar_op))
if scalar_op in [maximum,minimum] and numpy_raised: if scalar_op in [scalar.maximum,scalar.minimum] and numpy_raised:
try: try:
out = f(xv) out = f(xv)
assert out.dtype == dtype assert out.dtype == dtype
...@@ -284,7 +286,7 @@ class test_CAReduce(unittest.TestCase): ...@@ -284,7 +286,7 @@ class test_CAReduce(unittest.TestCase):
self.fail() self.fail()
else: else:
#numpy.{all,any} return bool type. #numpy.{all,any} return bool type.
if scalar_op in [and_, or_]: if scalar_op in [scalar.and_, scalar.or_]:
zv = numpy.asarray(zv, dtype=dtype) zv = numpy.asarray(zv, dtype=dtype)
if test_nan: if test_nan:
self.assertTrue(theano.tensor.TensorType.values_eq(f(xv), zv), (f(xv), zv)) self.assertTrue(theano.tensor.TensorType.values_eq(f(xv), zv), (f(xv), zv))
...@@ -302,40 +304,40 @@ class test_CAReduce(unittest.TestCase): ...@@ -302,40 +304,40 @@ class test_CAReduce(unittest.TestCase):
e = tensor_op(x, axis=tosum) e = tensor_op(x, axis=tosum)
if tosum is None: tosum = range(len(xsh)) if tosum is None: tosum = range(len(xsh))
f = copy(linker).accept(Env([x], [e.shape])).make_function() f = copy(linker).accept(Env([x], [e.shape])).make_function()
if not(scalar_op in [maximum,minimum] and ((xsh==() or numpy.prod(xsh)==0))): if not(scalar_op in [scalar.maximum,scalar.minimum] and ((xsh==() or numpy.prod(xsh)==0))):
assert all(f(xv)== zv.shape) assert all(f(xv) == zv.shape)
def test_perform(self): def test_perform(self):
for dtype in ["floatX", "complex64", "complex128", "int8", "uint8"]: for dtype in ["floatX", "complex64", "complex128", "int8", "uint8"]:
self.with_linker(gof.PerformLinker(), add, dtype=dtype) self.with_linker(gof.PerformLinker(), scalar.add, dtype=dtype)
self.with_linker(gof.PerformLinker(), mul, dtype=dtype) self.with_linker(gof.PerformLinker(), scalar.mul, dtype=dtype)
self.with_linker(gof.PerformLinker(), maximum, dtype=dtype) self.with_linker(gof.PerformLinker(), scalar.maximum, dtype=dtype)
self.with_linker(gof.PerformLinker(), minimum, dtype=dtype) self.with_linker(gof.PerformLinker(), scalar.minimum, dtype=dtype)
self.with_linker(gof.PerformLinker(), and_, dtype=dtype, self.with_linker(gof.PerformLinker(), scalar.and_, dtype=dtype,
tensor_op=tensor.all) tensor_op=tensor.all)
self.with_linker(gof.PerformLinker(), or_, dtype=dtype, self.with_linker(gof.PerformLinker(), scalar.or_, dtype=dtype,
tensor_op=tensor.any) tensor_op=tensor.any)
for dtype in ["int8", "uint8"]: for dtype in ["int8", "uint8"]:
self.with_linker(gof.PerformLinker(), or_, dtype=dtype) self.with_linker(gof.PerformLinker(), scalar.or_, dtype=dtype)
self.with_linker(gof.PerformLinker(), and_, dtype=dtype) self.with_linker(gof.PerformLinker(), scalar.and_, dtype=dtype)
self.with_linker(gof.PerformLinker(), xor, dtype=dtype) self.with_linker(gof.PerformLinker(), scalar.xor, dtype=dtype)
@dec.knownfailureif( @dec.knownfailureif(
True, True,
("When there is nan in the input of CAReduce, we don't have a good output. ")) ("When there is nan in the input of CAReduce, we don't have a good output. "))
def test_perform_nan(self): def test_perform_nan(self):
for dtype in ["floatX", "complex64", "complex128"]: for dtype in ["floatX", "complex64", "complex128"]:
self.with_linker(gof.PerformLinker(), add, dtype=dtype, self.with_linker(gof.PerformLinker(), scalar.add, dtype=dtype,
test_nan=True) test_nan=True)
self.with_linker(gof.PerformLinker(), mul, dtype=dtype, self.with_linker(gof.PerformLinker(), scalar.mul, dtype=dtype,
test_nan=True) test_nan=True)
self.with_linker(gof.PerformLinker(), maximum, dtype=dtype, self.with_linker(gof.PerformLinker(), scalar.maximum, dtype=dtype,
test_nan=True) test_nan=True)
self.with_linker(gof.PerformLinker(), minimum, dtype=dtype, self.with_linker(gof.PerformLinker(), scalar.minimum, dtype=dtype,
test_nan=True) test_nan=True)
self.with_linker(gof.PerformLinker(), or_, dtype=dtype, self.with_linker(gof.PerformLinker(), scalar.or_, dtype=dtype,
test_nan=True) test_nan=True)
self.with_linker(gof.PerformLinker(), and_, dtype=dtype, self.with_linker(gof.PerformLinker(), scalar.and_, dtype=dtype,
test_nan=True) test_nan=True)
self.with_linker(gof.PerformLinker(), or_, dtype=dtype, self.with_linker(gof.PerformLinker(), or_, dtype=dtype,
test_nan=True, tensor_op=tensor.any) test_nan=True, tensor_op=tensor.any)
...@@ -344,34 +346,33 @@ class test_CAReduce(unittest.TestCase): ...@@ -344,34 +346,33 @@ class test_CAReduce(unittest.TestCase):
def test_c(self): def test_c(self):
for dtype in ["floatX", "complex64", "complex128", "int8", "uint8"]: for dtype in ["floatX", "complex64", "complex128", "int8", "uint8"]:
self.with_linker(gof.CLinker(), add, dtype=dtype) self.with_linker(gof.CLinker(), scalar.add, dtype=dtype)
self.with_linker(gof.CLinker(), mul, dtype=dtype) self.with_linker(gof.CLinker(), scalar.mul, dtype=dtype)
for dtype in ["floatX", "int8", "uint8"]: for dtype in ["floatX", "int8", "uint8"]:
self.with_linker(gof.CLinker(), minimum, dtype=dtype) self.with_linker(gof.CLinker(), scalar.minimum, dtype=dtype)
self.with_linker(gof.CLinker(), maximum, dtype=dtype) self.with_linker(gof.CLinker(), scalar.maximum, dtype=dtype)
# all and any use neq that don't have c code for complex self.with_linker(gof.CLinker(), scalar.and_, dtype=dtype,
self.with_linker(gof.CLinker(), and_, dtype=dtype,
tensor_op=tensor.all) tensor_op=tensor.all)
self.with_linker(gof.CLinker(), or_, dtype=dtype, self.with_linker(gof.CLinker(), scalar.or_, dtype=dtype,
tensor_op=tensor.any) tensor_op=tensor.any)
for dtype in ["int8", "uint8"]: for dtype in ["int8", "uint8"]:
self.with_linker(gof.CLinker(), or_, dtype=dtype) self.with_linker(gof.CLinker(), scalar.or_, dtype=dtype)
self.with_linker(gof.CLinker(), and_, dtype=dtype) self.with_linker(gof.CLinker(), scalar.and_, dtype=dtype)
self.with_linker(gof.CLinker(), xor, dtype=dtype) self.with_linker(gof.CLinker(), scalar.xor, dtype=dtype)
@dec.knownfailureif( @dec.knownfailureif(
True, True,
("When there is nan in the input of CAReduce, we don't have a good output. ")) ("When there is nan in the input of CAReduce, we don't have a good output. "))
def test_c_nan(self): def test_c_nan(self):
for dtype in ["floatX", "complex64", "complex128"]: for dtype in ["floatX", "complex64", "complex128"]:
self.with_linker(gof.CLinker(), add, dtype=dtype, self.with_linker(gof.CLinker(), scalar.add, dtype=dtype,
test_nan=True) test_nan=True)
self.with_linker(gof.CLinker(), mul, dtype=dtype, self.with_linker(gof.CLinker(), scalar.mul, dtype=dtype,
test_nan=True) test_nan=True)
for dtype in ["floatX"]: for dtype in ["floatX"]:
self.with_linker(gof.CLinker(), minimum, dtype=dtype, self.with_linker(gof.CLinker(), scalar.minimum, dtype=dtype,
test_nan=True) test_nan=True)
self.with_linker(gof.CLinker(), maximum, dtype=dtype, self.with_linker(gof.CLinker(), scalar.maximum, dtype=dtype,
test_nan=True) test_nan=True)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论