提交 d885239d authored 作者: bergstrj@iro.umontreal.ca's avatar bergstrj@iro.umontreal.ca

merged, adjusted Abs.c_code for complex and other types

......@@ -77,21 +77,75 @@ class T_conversion(unittest.TestCase):
class _testCase_dot(unittest.TestCase):
""" Types of sparse matrices to use for testing """
mtypes = [sparse.csc_matrix, sparse.csr_matrix]
#mtypes = [sparse.csc_matrix, sparse.csr_matrix, sparse.dok_matrix, sparse.lil_matrix, sparse.coo_matrix]
def setUp(self):
numpy.random.seed(44)
def test_basic0(self):
for mtype in [sparse.csc_matrix, sparse.csr_matrix]:
x = assparse(mtype(sparse.speye(5,3)))
y = tensor.astensor(numpy.random.rand(3, 2))
for mtype in self.mtypes:
x = assparse(mtype((500,3)))
x.data[(10, 1)] = 1
x.data[(20, 2)] = 2
y = tensor.astensor([[1., 2], [3, 4], [2, 1]])
zop = dot(x,y)
z = compile.eval_outputs([zop])
self.failUnless(z.shape == (5,2))
self.failUnless(z.shape == (500,2))
self.failUnless(type(z) is mtype)
def test_missing(self):
raise NotImplementedError('tests commented out')
w = mtype((500,2))
w[(10, 0)] = 3.
w[(20, 0)] = 4
w[(10, 1)] = 4
w[(20, 1)] = 2
self.failUnless(z.shape == w.shape)
self.failUnless(type(z) == type(w))
self.failUnless(z.dtype == w.dtype)
#self.failUnless(z == w)
self.failUnless(abs(z-w).nnz == 0)
z = z.todense()
w = w.todense()
self.failUnless((z == w).all() == True)
def test_basic1(self):
for mtype in self.mtypes:
x = assparse(mtype((500,3)))
x.data[(10, 1)] = 1
x.data[(20, 2)] = 2
y = tensor.astensor([[1., 2], [3, 4], [2, 1]])
x.data = x.data.T
y.data = y.data.T
# zop = dot(y, x)
zop = transpose(dot(y, x))
z = compile.eval_outputs([zop])
self.failUnless(z.shape == (500,2))
print mtype, type(z)
# self.failUnless(type(z) is mtype)
w = mtype((500,2))
w[(10, 0)] = 3.
w[(20, 0)] = 4
w[(10, 1)] = 4
w[(20, 1)] = 2
self.failUnless(z.shape == w.shape)
# Type should switch from csr to csc and vice-versa, so don't perform this test
#self.failUnless(type(z) == type(w))
self.failUnless(z.dtype == w.dtype)
# Type should switch from csr to csc and vice-versa, so don't perform this test
#self.failUnless(z == w)
self.failUnless(abs(z-w).nnz == 0)
z = z.todense()
w = w.todense()
self.failUnless((z == w).all() == True)
# def test_basic1(self):
# """dot: sparse left"""
......@@ -110,7 +164,10 @@ class _testCase_dot(unittest.TestCase):
# except Exception, e:
# print 'cccc', mtype, e, str(e)
# raise
#
def test_missing(self):
raise NotImplementedError('tests commented out')
# def test_basic2(self):
# """dot: sparse right"""
# a = numpy.random.rand(2, 5)
......
......@@ -324,12 +324,13 @@ class Abs(UnaryScalarOp):
def grad(self, (x, ), (gz, )):
return gz * sgn(x),
def c_code(self, (x, ), (z, ), sub):
dtype = self.inputs[0].dtype
if dtype in ('int32', 'int64'):
dtype = str(self.inputs[0].dtype)
if 'int' in dtype:
return "%(z)s = abs(%(x)s);" % locals()
if dtype in ('float32', 'float64'):
if 'float' in dtype:
return "%(z)s = fabs(%(x)s);" % locals()
raise NotImplementedError('Abs not implemented for dtype', dtype)
#complex, other?
raise NotImplementedError('dtype not supported', dtype)
class Sgn(UnaryScalarOp):
def impl(self, x):
......
......@@ -229,4 +229,5 @@ def dot(x, y, grad_preserves_dense=True):
if x_is_sparse:
return Dot(x,y,grad_preserves_dense).outputs[0]
else:
return transpose(Dot(transpose(y), transpose(x), grad_preserves_dense).outputs[0])
assert y_is_sparse
return transpose(Dot(y.T, x.T, grad_preserves_dense).outputs[0])
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论