提交 12c6df35 authored 作者: Hengjean's avatar Hengjean

Fixed errors and typos

上级 b40baa32
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# Slice type and Op. None Type and NoneConst. # Slice type and Op. None Type and NoneConst.
# #
import theano import theano
from theano.gof import Apply, Constant, Generic, Op, Type from theano.gof import Apply, Constant, Generic, Op, Type, hashtype
from theano.gradient import DisconnectedType from theano.gradient import DisconnectedType
...@@ -55,6 +55,9 @@ class SliceType(Type): ...@@ -55,6 +55,9 @@ class SliceType(Type):
def __eq__(self, other): def __eq__(self, other):
return type(self) is SliceType and type(other) is SliceType return type(self) is SliceType and type(other) is SliceType
def __hash__(self):
return hashtype(self)
slicetype = SliceType() slicetype = SliceType()
......
...@@ -8,13 +8,13 @@ from theano import tensor as T ...@@ -8,13 +8,13 @@ from theano import tensor as T
class _typed_list_py_operators: class _typed_list_py_operators:
def __getitem__(self, index): def __getitem__(self, index):
return get_item()(self, index) return GetItem()(self, index)
def append(self, toAppend): def append(self, toAppend):
return append()(self, toAppend) return Append()(self, toAppend)
def extend(self, toAppend): def extend(self, toAppend):
return extend()(self, toAppend) return Extend()(self, toAppend)
class TypedListVariable(_typed_list_py_operators, Variable): class TypedListVariable(_typed_list_py_operators, Variable):
...@@ -25,7 +25,7 @@ class TypedListVariable(_typed_list_py_operators, Variable): ...@@ -25,7 +25,7 @@ class TypedListVariable(_typed_list_py_operators, Variable):
TypedListType.Variable = TypedListVariable TypedListType.Variable = TypedListVariable
class get_item(Op): class GetItem(Op):
""" """
get specified slice of a typed list get specified slice of a typed list
""" """
...@@ -37,7 +37,7 @@ class get_item(Op): ...@@ -37,7 +37,7 @@ class get_item(Op):
def make_node(self, x, index): def make_node(self, x, index):
assert isinstance(x.type, TypedListType) assert isinstance(x.type, TypedListType)
if index.type == SliceType(): if isinstance(index.type, SliceType):
return Apply(self, [x, index], [x.type()]) return Apply(self, [x, index], [x.type()])
elif isinstance(index, T.TensorVariable) and index.ndim == 0: elif isinstance(index, T.TensorVariable) and index.ndim == 0:
return Apply(self, [x, index], [x.ttype()]) return Apply(self, [x, index], [x.ttype()])
...@@ -53,7 +53,7 @@ class get_item(Op): ...@@ -53,7 +53,7 @@ class get_item(Op):
return self.__class__.__name__ return self.__class__.__name__
class append(Op): class Append(Op):
""" """
#append an element at the end of another list #append an element at the end of another list
""" """
...@@ -76,7 +76,7 @@ class append(Op): ...@@ -76,7 +76,7 @@ class append(Op):
return self.__class__.__name__ return self.__class__.__name__
class extend(Op): class Extend(Op):
""" """
append all element of a list at the end of another list append all element of a list at the end of another list
""" """
......
import unittest import unittest
from nose.plugins.skip import SkipTest
from nose.plugins.attrib import attr
import numpy import numpy
from numpy.testing import dec, assert_array_equal, assert_allclose
from numpy.testing.noseclasses import KnownFailureTest
import theano import theano
import theano.typed_list import theano.typed_list
from theano import tensor as T from theano import tensor as T
from theano.tensor.type_other import SliceType from theano.tensor.type_other import SliceType
from theano.typed_list.type import TypedListType from theano.typed_list.type import TypedListType
from theano.typed_list.basic import (get_item, append, extend) from theano.typed_list.basic import (GetItem, Append, Extend)
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
...@@ -33,14 +29,14 @@ class test_get_item(unittest.TestCase): ...@@ -33,14 +29,14 @@ class test_get_item(unittest.TestCase):
mySymbolicSlice = SliceType()() mySymbolicSlice = SliceType()()
z = get_item()(mySymbolicMatricesList, mySymbolicSlice) z = GetItem()(mySymbolicMatricesList, mySymbolicSlice)
self.assertFalse(isinstance(z, T.TensorVariable)) self.assertFalse(isinstance(z, T.TensorVariable))
f = theano.function([mySymbolicMatricesList, mySymbolicSlice], f = theano.function([mySymbolicMatricesList, mySymbolicSlice],
z) z)
x = rand_ranged_matrix(-1000, 1000, [100, 100]) x = rand_ranged_matrix(-1000, 1000, [100, 101])
self.assertTrue(numpy.array_equal(f([x], slice(0, 1, 1)), [x])) self.assertTrue(numpy.array_equal(f([x], slice(0, 1, 1)), [x]))
...@@ -51,12 +47,12 @@ class test_get_item(unittest.TestCase): ...@@ -51,12 +47,12 @@ class test_get_item(unittest.TestCase):
mySymbolicScalar = T.scalar() mySymbolicScalar = T.scalar()
z = get_item()(mySymbolicMatricesList, mySymbolicScalar) z = GetItem()(mySymbolicMatricesList, mySymbolicScalar)
f = theano.function([mySymbolicMatricesList, mySymbolicScalar], f = theano.function([mySymbolicMatricesList, mySymbolicScalar],
z) z)
x = rand_ranged_matrix(-1000, 1000, [100, 100]) x = rand_ranged_matrix(-1000, 1000, [100, 101])
self.assertTrue(numpy.array_equal(f([x], numpy.asarray(0)), x)) self.assertTrue(numpy.array_equal(f([x], numpy.asarray(0)), x))
...@@ -70,7 +66,7 @@ class test_get_item(unittest.TestCase): ...@@ -70,7 +66,7 @@ class test_get_item(unittest.TestCase):
f = theano.function([mySymbolicMatricesList, mySymbolicScalar], f = theano.function([mySymbolicMatricesList, mySymbolicScalar],
z) z)
x = rand_ranged_matrix(-1000, 1000, [100, 100]) x = rand_ranged_matrix(-1000, 1000, [100, 101])
self.assertTrue(numpy.array_equal(f([x], numpy.asarray(0)), x)) self.assertTrue(numpy.array_equal(f([x], numpy.asarray(0)), x))
...@@ -79,7 +75,7 @@ class test_get_item(unittest.TestCase): ...@@ -79,7 +75,7 @@ class test_get_item(unittest.TestCase):
theano.config.floatX, (False, False)))() theano.config.floatX, (False, False)))()
mySymbolicMatrix = T.matrix() mySymbolicMatrix = T.matrix()
self.assertRaises(TypeError, get_item(), mySymbolicMatricesList, self.assertRaises(TypeError, GetItem(), mySymbolicMatricesList,
mySymbolicMatrix) mySymbolicMatrix)
...@@ -90,13 +86,13 @@ class test_append(unittest.TestCase): ...@@ -90,13 +86,13 @@ class test_append(unittest.TestCase):
theano.config.floatX, (False, False)))() theano.config.floatX, (False, False)))()
myMatrix = T.matrix() myMatrix = T.matrix()
z = append()(mySymbolicMatricesList, myMatrix) z = Append()(mySymbolicMatricesList, myMatrix)
f = theano.function([mySymbolicMatricesList, myMatrix], z) f = theano.function([mySymbolicMatricesList, myMatrix], z)
x = rand_ranged_matrix(-1000, 1000, [100, 100]) x = rand_ranged_matrix(-1000, 1000, [100, 101])
y = rand_ranged_matrix(-1000, 1000, [100, 100]) y = rand_ranged_matrix(-1000, 1000, [100, 101])
self.assertTrue(numpy.array_equal(f([x], y), [x, y])) self.assertTrue(numpy.array_equal(f([x], y), [x, y]))
...@@ -109,13 +105,13 @@ class test_extend(unittest.TestCase): ...@@ -109,13 +105,13 @@ class test_extend(unittest.TestCase):
mySymbolicMatricesList2 = TypedListType(T.TensorType( mySymbolicMatricesList2 = TypedListType(T.TensorType(
theano.config.floatX, (False, False)))() theano.config.floatX, (False, False)))()
z = extend()(mySymbolicMatricesList1, mySymbolicMatricesList2) z = Extend()(mySymbolicMatricesList1, mySymbolicMatricesList2)
f = theano.function([mySymbolicMatricesList1, mySymbolicMatricesList2], f = theano.function([mySymbolicMatricesList1, mySymbolicMatricesList2],
z) z)
x = rand_ranged_matrix(-1000, 1000, [100, 100]) x = rand_ranged_matrix(-1000, 1000, [100, 101])
y = rand_ranged_matrix(-1000, 1000, [100, 100]) y = rand_ranged_matrix(-1000, 1000, [100, 101])
self.assertTrue(numpy.array_equal(f([x], [y]), [x, y])) self.assertTrue(numpy.array_equal(f([x], [y]), [x, y]))
import unittest import unittest
from nose.plugins.skip import SkipTest
from nose.plugins.attrib import attr
import numpy import numpy
from numpy.testing import dec, assert_array_equal, assert_allclose
from numpy.testing.noseclasses import KnownFailureTest
import theano import theano
import theano.typed_list import theano.typed_list
...@@ -91,15 +87,14 @@ class test_typed_list_type(unittest.TestCase): ...@@ -91,15 +87,14 @@ class test_typed_list_type(unittest.TestCase):
def test_intern_filter(self): def test_intern_filter(self):
""" """
(supposing theano.config.floatX = floatX)
Test checking if values contained are themselves Test checking if values contained are themselves
filtered. If they weren't this code would raise filtered. If they weren't this code would raise
an exception. an exception.
""" """
myType = TypedListType(T.TensorType(theano.config.floatX, myType = TypedListType(T.TensorType('float64',
(False, False))) (False, False)))
x = numpy.asarray([[4, 5], [4, 5]], dtype='Float32') x = numpy.asarray([[4, 5], [4, 5]], dtype='float32')
self.assertTrue(numpy.array_equal(myType.filter([x]), [x])) self.assertTrue(numpy.array_equal(myType.filter([x]), [x]))
......
...@@ -15,8 +15,8 @@ class TypedListType(gof.Type): ...@@ -15,8 +15,8 @@ class TypedListType(gof.Type):
if depth < 0: if depth < 0:
raise ValueError('Please specify a depth superior or' raise ValueError('Please specify a depth superior or'
'equal to 0') 'equal to 0')
if not hasattr(ttype, 'is_valid_value'): if not isinstance(ttype, gof.Type):
raise TypeError('Expected a Theano type') raise TypeError('Expected a Theano Type')
if depth == 0: if depth == 0:
self.ttype = ttype self.ttype = ttype
...@@ -36,8 +36,6 @@ class TypedListType(gof.Type): ...@@ -36,8 +36,6 @@ class TypedListType(gof.Type):
if not isinstance(x, list): if not isinstance(x, list):
raise TypeError('Expected a python list') raise TypeError('Expected a python list')
else: else:
x = list(x)
x = [self.ttype.filter(y) for y in x] x = [self.ttype.filter(y) for y in x]
if all(self.ttype.is_valid_value(y) for y in x): if all(self.ttype.is_valid_value(y) for y in x):
...@@ -55,27 +53,20 @@ class TypedListType(gof.Type): ...@@ -55,27 +53,20 @@ class TypedListType(gof.Type):
if not hasattr(other, 'ttype'): if not hasattr(other, 'ttype'):
return False return False
return (self.ttype == other.ttype) return type(self) == type(other) and self.ttype == other.ttype
def __hash__(self):
return gof.hashtype(self) ^ hash(self.ttype)
def __str__(self): def __str__(self):
return 'Typed List <' + str(self.ttype) + '>' return 'TypedList <' + str(self.ttype) + '>'
def get_depth(self): def get_depth(self):
""" """
utilitary function to get the 0 based utilitary function to get the 0 based
level of the list level of the list
""" """
if hasattr(self.ttype, 'get_depth'): if isinstance(self.ttype, TypedListType):
return self.ttype.get_depth() + 1 return self.ttype.get_depth() + 1
else: else:
return 0 return 0
def make_variable(self, name=None):
"""Return a `TypedListVariable` of this type
:Parameters:
- `name`: str
A pretty name to identify this `Variable` when printing and
debugging
"""
return self.Variable(self, name=name)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论