提交 3912f005 authored 作者: Frederic Bastien's avatar Frederic Bastien

Allow bit-wise op to be of type int16 and add test for all allowed type.

上级 163a00fd
...@@ -778,7 +778,7 @@ switch = Switch() ...@@ -778,7 +778,7 @@ switch = Switch()
class UnaryBitOp(UnaryScalarOp): class UnaryBitOp(UnaryScalarOp):
def output_types(self, *input_types): def output_types(self, *input_types):
for i in input_types[0]: for i in input_types[0]:
if i not in (int8, int32, int64): if i not in (int8, int16, int32, int64):
raise TypeError('input to a BitOp must have type int8, int32 or int64... not %s' % i) raise TypeError('input to a BitOp must have type int8, int32 or int64... not %s' % i)
return upcast_out(*input_types[0]) return upcast_out(*input_types[0])
def grad(self, inputs, output_gradients): def grad(self, inputs, output_gradients):
......
...@@ -2458,43 +2458,52 @@ class test_comparison(unittest.TestCase): ...@@ -2458,43 +2458,52 @@ class test_comparison(unittest.TestCase):
self.failUnless(numpy.all(v == (l != r)), (v, (l!=r))) self.failUnless(numpy.all(v == (l != r)), (v, (l!=r)))
class test_bitwise(unittest.TestCase): class test_bitwise(unittest.TestCase):
dtype = ['int8', 'int16', 'int32', 'int64',]
def test_or(self): def test_or(self):
x, y = bvector(), bvector() for dtype in self.dtype:
fn = inplace_func([x,y], x|y) x, y = vector(dtype=dtype), vector(dtype=dtype)
l = theano._asarray([0,0,1,1], dtype = 'int8') fn = inplace_func([x,y], x|y)
r = theano._asarray([0,1,0,1], dtype = 'int8') l = theano._asarray([0,0,1,1], dtype = dtype)
v = fn(l, r) r = theano._asarray([0,1,0,1], dtype = dtype)
self.failUnless(numpy.all(v == (operator.or_(l, r))), (l, r, v)) v = fn(l, r)
self.failUnless(numpy.all(v == (operator.or_(l, r))), (l, r, v))
def test_xor(self): def test_xor(self):
x, y = bvector(), bvector() for dtype in self.dtype:
fn = inplace_func([x,y], x^y) x, y = vector(dtype=dtype), vector(dtype=dtype)
ix = x fn = inplace_func([x,y], x^y)
ix = inplace.xor_inplace(ix, y) ix = x
gn = inplace_func([x,y], ix) ix = inplace.xor_inplace(ix, y)
l = theano._asarray([0,0,1,1], dtype = 'int8') gn = inplace_func([x,y], ix)
r = theano._asarray([0,1,0,1], dtype = 'int8') l = theano._asarray([0,0,1,1], dtype = dtype)
v = fn(l, r) r = theano._asarray([0,1,0,1], dtype = dtype)
self.failUnless(numpy.all(v == (operator.xor(l, r))), (l, r, v)) v = fn(l, r)
v = gn(l, r) self.failUnless(numpy.all(v == (operator.xor(l, r))), (l, r, v))
#test the in-place stuff v = gn(l, r)
self.failUnless(numpy.all(l == numpy.asarray([0,1,1,0])), l) #test the in-place stuff
self.failUnless(numpy.all(l == numpy.asarray([0,1,1,0])), l)
def test_and(self): def test_and(self):
x, y = bvector(), bvector() for dtype in self.dtype:
fn = inplace_func([x,y], x&y) x, y = vector(dtype=dtype), vector(dtype=dtype)
l = theano._asarray([0,0,1,1], dtype = 'int8') fn = inplace_func([x,y], x&y)
r = theano._asarray([0,1,0,1], dtype = 'int8') l = theano._asarray([0,0,1,1], dtype = dtype)
v = fn(l, r) r = theano._asarray([0,1,0,1], dtype = dtype)
self.failUnless(numpy.all(v == (operator.and_(l, r))), (l, r, v)) v = fn(l, r)
self.failUnless(numpy.all(v == (operator.and_(l, r))), (l, r, v))
def test_inv(self): def test_inv(self):
x, y = bvector(), bvector() for dtype in self.dtype:
fn = inplace_func([x,y], ~x) x = vector(dtype=dtype)
l = theano._asarray([0,0,1,1], dtype = 'int8') fn = inplace_func([x], ~x)
r = theano._asarray([0,1,0,1], dtype = 'int8') for l in [[0,0,1,1],[0,1,0,1],
v = fn(l, r) [0,0,1,1],[0,1,0,1],
self.failUnless(numpy.all(v == (~l)), (l, r, v)) [-1,2**16, 2**16-1]
]:
l = theano._asarray([0,0,1,1], dtype = dtype)
v = fn(l)
self.failUnless(numpy.all(v == (~l)), (l, v))
def test_eye(self): def test_eye(self):
n = iscalar() n = iscalar()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论