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

Fixed errors and typos

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