提交 30723440 authored 作者: vdumoulin's avatar vdumoulin

Merge pull request #3601 from shabanian/typed_list_pep8

pep8 typed list directory
......@@ -51,13 +51,7 @@ whitelist_flake8 = [
"compile/tests/test_pfunc.py",
"compile/tests/test_debugmode.py",
"compile/tests/test_profiling.py",
"typed_list/type.py",
"typed_list/__init__.py",
"typed_list/opt.py",
"typed_list/basic.py",
"typed_list/tests/test_type.py",
"typed_list/tests/test_opt.py",
"typed_list/tests/test_basic.py",
"tensor/__init__.py",
"tensor/tests/test_subtensor.py",
"tensor/tests/test_utils.py",
......
import copy
import numpy
......@@ -109,7 +108,7 @@ Parameters
----------
x
Typed list.
index
index
The index of the value to return from `x`.
"""
......@@ -596,7 +595,7 @@ class MakeList(Op):
a2 = []
for elem in a:
if not isinstance(elem, theano.gof.Variable):
elem = as_tensor_variable(elem)
elem = theano.tensor.as_tensor_variable(elem)
a2.append(elem)
if not all(a2[0].type == elem.type for elem in a2):
raise TypeError(
......
from theano import gof
from theano import compile
from theano.gof import TopoOptimizer
from theano.typed_list.basic import (Reverse,
Append, Extend, Insert, Remove)
from theano.typed_list.basic import Reverse, Append, Extend, Insert, Remove
@gof.local_optimizer([Append, Extend, Insert, Reverse, Remove], inplace=True)
def typed_list_inplace_opt(node):
if isinstance(node.op, (Append, Extend, Insert, Reverse, Remove)) \
and not node.op.inplace:
if (isinstance(node.op, (Append, Extend, Insert, Reverse, Remove)) and not
node.op.inplace):
new_op = node.op.__class__(
inplace=True)
new_op = node.op.__class__(inplace=True)
new_node = new_op(*node.inputs)
return [new_node]
return False
compile.optdb.register('typed_list_inplace_opt',
TopoOptimizer(typed_list_inplace_opt,
failure_callback=TopoOptimizer.warn_inplace), 60,
'fast_run', 'inplace')
failure_callback=TopoOptimizer.warn_inplace),
60, 'fast_run', 'inplace')
......@@ -10,7 +10,7 @@ from theano.tensor.type_other import SliceType
from theano.typed_list.type import TypedListType
from theano.typed_list.basic import (GetItem, Insert,
Append, Extend, Remove, Reverse,
Index, Count, Length, make_list, MakeList)
Index, Count, Length, make_list)
from theano import sparse
from theano.tests import unittest_tools as utt
# TODO, handle the case where scipy isn't installed.
......@@ -23,8 +23,8 @@ except ImportError:
# took from tensors/tests/test_basic.py
def rand_ranged_matrix(minimum, maximum, shape):
return numpy.asarray(numpy.random.rand(*shape) * (maximum - minimum)
+ minimum, dtype=theano.config.floatX)
return numpy.asarray(numpy.random.rand(*shape) * (maximum - minimum) +
minimum, dtype=theano.config.floatX)
# took from sparse/tests/test_basic.py
......@@ -82,7 +82,6 @@ class test_get_item(unittest.TestCase):
z)
x = rand_ranged_matrix(-1000, 1000, [100, 101])
y = rand_ranged_matrix(-1000, 1000, [100, 101])
self.assertTrue(numpy.array_equal(f([x],
numpy.asarray(0, dtype='int64')),
......@@ -555,7 +554,7 @@ class test_length(unittest.TestCase):
self.assertTrue(f([x, x]) == 2)
class T_MakeList(unittest.TestCase):
class TestMakeList(unittest.TestCase):
def test_wrong_shape(self):
a = T.vector()
......@@ -563,22 +562,22 @@ class T_MakeList(unittest.TestCase):
self.assertRaises(TypeError, make_list, (a, b))
def correct_answer(self):
def test_correct_answer(self):
a = T.matrix()
b = T.matrix()
x = T.tensor3()
y = T.tensor3()
A = numpy.random.rand(5)
B = numpy.random.rand(7)
X = numpy.random.rand(5, 6)
Y = numpy.random.rand(1, 9)
A = numpy.cast[theano.config.floatX](numpy.random.rand(5, 3))
B = numpy.cast[theano.config.floatX](numpy.random.rand(7, 2))
X = numpy.cast[theano.config.floatX](numpy.random.rand(5, 6, 1))
Y = numpy.cast[theano.config.floatX](numpy.random.rand(1, 9, 3))
make_list((3., 4.))
c = make_list((a, b))
z = make_list((x, y))
fc = function([a, b], c)
fz = function([x, y], z)
self.assertTrue(f([A, B]) == [A, B])
self.assertTrue(f([X, Y]) == [X, Y])
fc = theano.function([a, b], c)
fz = theano.function([x, y], z)
self.assertTrue(fc(A, B) == [A, B])
self.assertTrue(fz(X, Y) == [X, Y])
......@@ -5,30 +5,28 @@ import numpy
import theano
import theano.typed_list
from theano import tensor as T
from theano.tensor.type_other import SliceType
from theano.typed_list.type import TypedListType
from theano.typed_list.basic import (GetItem, Insert,
Append, Extend, Remove, Reverse,
Index, Count)
from theano.typed_list.basic import (Insert,
Append, Extend, Remove, Reverse)
from theano import In
# took from tensors/tests/test_basic.py
def rand_ranged_matrix(minimum, maximum, shape):
return numpy.asarray(numpy.random.rand(*shape) * (maximum - minimum)
+ minimum, dtype=theano.config.floatX)
return numpy.asarray(numpy.random.rand(*shape) * (maximum - minimum) +
minimum, dtype=theano.config.floatX)
class test_inplace(unittest.TestCase):
def test_reverse_inplace(self):
mySymbolicMatricesList = TypedListType(T.TensorType(
theano.config.floatX, (False, False)))()
theano.config.floatX, (False, False)))()
z = Reverse()(mySymbolicMatricesList)
m = theano.compile.mode.get_default_mode().including("typed_list_inplace_opt")
f = theano.function([In(mySymbolicMatricesList, borrow=True,
mutable=True)], z, accept_inplace=True, mode=m)
mutable=True)], z, accept_inplace=True, mode=m)
self.assertTrue(f.maker.fgraph.toposort()[0].op.inplace)
x = rand_ranged_matrix(-1000, 1000, [100, 101])
......@@ -39,13 +37,14 @@ class test_inplace(unittest.TestCase):
def test_append_inplace(self):
mySymbolicMatricesList = TypedListType(T.TensorType(
theano.config.floatX, (False, False)))()
theano.config.floatX, (False, False)))()
mySymbolicMatrix = T.matrix()
z = Append()(mySymbolicMatricesList, mySymbolicMatrix)
m = theano.compile.mode.get_default_mode().including("typed_list_inplace_opt")
f = theano.function([In(mySymbolicMatricesList, borrow=True,
mutable=True), In(mySymbolicMatrix, borrow=True,
mutable=True)], z, accept_inplace=True, mode=m)
mutable=True),
In(mySymbolicMatrix, borrow=True,
mutable=True)], z, accept_inplace=True, mode=m)
self.assertTrue(f.maker.fgraph.toposort()[0].op.inplace)
x = rand_ranged_matrix(-1000, 1000, [100, 101])
......@@ -56,15 +55,15 @@ class test_inplace(unittest.TestCase):
def test_extend_inplace(self):
mySymbolicMatricesList1 = TypedListType(T.TensorType(
theano.config.floatX, (False, False)))()
theano.config.floatX, (False, False)))()
mySymbolicMatricesList2 = TypedListType(T.TensorType(
theano.config.floatX, (False, False)))()
theano.config.floatX, (False, False)))()
z = Extend()(mySymbolicMatricesList1, mySymbolicMatricesList2)
m = theano.compile.mode.get_default_mode().including("typed_list_inplace_opt")
f = theano.function([In(mySymbolicMatricesList1, borrow=True,
mutable=True), mySymbolicMatricesList2],
mutable=True), mySymbolicMatricesList2],
z, mode=m)
self.assertTrue(f.maker.fgraph.toposort()[0].op.inplace)
......@@ -76,7 +75,7 @@ class test_inplace(unittest.TestCase):
def test_insert_inplace(self):
mySymbolicMatricesList = TypedListType(T.TensorType(
theano.config.floatX, (False, False)))()
theano.config.floatX, (False, False)))()
mySymbolicIndex = T.scalar(dtype='int64')
mySymbolicMatrix = T.matrix()
......@@ -84,8 +83,8 @@ class test_inplace(unittest.TestCase):
m = theano.compile.mode.get_default_mode().including("typed_list_inplace_opt")
f = theano.function([In(mySymbolicMatricesList, borrow=True,
mutable=True), mySymbolicIndex, mySymbolicMatrix],
z, accept_inplace=True, mode=m)
mutable=True), mySymbolicIndex, mySymbolicMatrix],
z, accept_inplace=True, mode=m)
self.assertTrue(f.maker.fgraph.toposort()[0].op.inplace)
x = rand_ranged_matrix(-1000, 1000, [100, 101])
......@@ -93,11 +92,11 @@ class test_inplace(unittest.TestCase):
y = rand_ranged_matrix(-1000, 1000, [100, 101])
self.assertTrue(numpy.array_equal(f([x], numpy.asarray(1,
dtype='int64'), y), [x, y]))
dtype='int64'), y), [x, y]))
def test_remove_inplace(self):
mySymbolicMatricesList = TypedListType(T.TensorType(
theano.config.floatX, (False, False)))()
theano.config.floatX, (False, False)))()
mySymbolicMatrix = T.matrix()
z = Remove()(mySymbolicMatricesList, mySymbolicMatrix)
m = theano.compile.mode.get_default_mode().including("typed_list_inplace_opt")
......
......@@ -11,8 +11,8 @@ from theano.tests import unittest_tools as utt
# took from tensors/tests/test_basic.py
def rand_ranged_matrix(minimum, maximum, shape):
return numpy.asarray(numpy.random.rand(*shape) * (maximum - minimum)
+ minimum, dtype=theano.config.floatX)
return numpy.asarray(numpy.random.rand(*shape) * (maximum - minimum) +
minimum, dtype=theano.config.floatX)
class test_typed_list_type(unittest.TestCase):
......@@ -63,13 +63,13 @@ class test_typed_list_type(unittest.TestCase):
"""
# list of matrices
myType1 = TypedListType(T.TensorType(theano.config.floatX,
(False, False)))
(False, False)))
# list of matrices
myType2 = TypedListType(T.TensorType(theano.config.floatX,
(False, False)))
(False, False)))
# list of scalars
myType3 = TypedListType(T.TensorType(theano.config.floatX,
()))
()))
self.assertTrue(myType2 == myType1)
self.assertFalse(myType3 == myType1)
......@@ -118,7 +118,7 @@ class test_typed_list_type(unittest.TestCase):
Testing nested list with one level of depth
"""
myNestedType = TypedListType(T.TensorType(theano.config.floatX,
(False, False)))
(False, False)))
myType = TypedListType(myNestedType)
......@@ -131,7 +131,7 @@ class test_typed_list_type(unittest.TestCase):
Nested list with different depth aren't the same
"""
myNestedType = TypedListType(T.TensorType(theano.config.floatX,
(False, False)))
(False, False)))
myNestedType2 = TypedListType(myNestedType)
......@@ -144,13 +144,13 @@ class test_typed_list_type(unittest.TestCase):
test for the 'depth' optionnal argument
"""
myNestedType = TypedListType(T.TensorType(theano.config.floatX,
(False, False)), 3)
(False, False)), 3)
myType = TypedListType(T.TensorType(theano.config.floatX,
(False, False)))
myManualNestedType = TypedListType(TypedListType(
TypedListType(myType)))
TypedListType(myType)))
self.assertTrue(myNestedType == myManualNestedType)
......@@ -162,7 +162,7 @@ class test_typed_list_type(unittest.TestCase):
(False, False)))
myManualNestedType = TypedListType(TypedListType(
TypedListType(myType)))
TypedListType(myType)))
self.assertTrue(myManualNestedType.get_depth() == 3)
......@@ -175,17 +175,16 @@ class test_typed_list_type(unittest.TestCase):
(False, False)))
myManualNestedType1 = TypedListType(TypedListType(
TypedListType(myType)))
TypedListType(myType)))
myManualNestedType2 = TypedListType(TypedListType(
myType))
myManualNestedType2 = TypedListType(TypedListType(myType))
self.assertFalse(myManualNestedType1 == myManualNestedType2)
self.assertFalse(myManualNestedType2 == myManualNestedType1)
def test_variable_is_Typed_List_variable(self):
mySymbolicVariable = TypedListType(T.TensorType(theano.config.floatX,
(False, False)))()
(False, False)))()
self.assertTrue(isinstance(mySymbolicVariable,
theano.typed_list.TypedListVariable))
......@@ -3,7 +3,7 @@ from theano import gof
class TypedListType(gof.Type):
"""
Parameters
----------
ttype
......@@ -15,10 +15,10 @@ class TypedListType(gof.Type):
"""
def __init__(self, ttype, depth=0):
if depth < 0:
raise ValueError('Please specify a depth superior or'
'equal to 0')
'equal to 0')
if not isinstance(ttype, gof.Type):
raise TypeError('Expected a Theano Type')
......@@ -58,7 +58,7 @@ class TypedListType(gof.Type):
Two lists are equal if they contain the same type.
"""
return type(self) == type(other) and self.ttype == other.ttype
return type(self) == type(other) and self.ttype == other.ttype
def __hash__(self):
return gof.hashtype(self) ^ hash(self.ttype)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论