提交 df265150 authored 作者: Frederic Bastien's avatar Frederic Bastien

a better implementation of SparseType.may_share_memory. test it.

上级 db66b8e6
......@@ -190,7 +190,18 @@ class SparseType(gof.Type):
def may_share_memory(a,b):
# This is Fred suggestion for a quick and dirty way of checking
# aliasing .. this can potentially be further refined (ticket #374)
return a is b
if _is_sparse(a) and _is_sparse(b):
return a is b
if _is_sparse(b) and isinstance(a, numpy.ndarray):
a,b=b,a
if _is_sparse(a) and isinstance(b, numpy.ndarray):
if (numpy.may_share_memory(a.data,b) or
numpy.may_share_memory(a.indices,b) or
numpy.may_share_memory(a.indptr,b)):
#currently we can't share memory with a.shape as it is a tuple
return True
return False
def make_variable(self, name = None):
return SparseVariable(self, name = name)
......@@ -250,7 +261,7 @@ class _sparse_py_operators:
def __rdot__(right, left): return structured_dot(left, right)
#N.B. THIS IS COMMENTED OUT ON PURPOSE!!!
# Discussion with Fred & James (at least, and maybe others before)
# Discussion with Fred & James (at least, and maybe others before)
# we decided that casting from a sparse to dense should be explicit
# because it's usually something you want to be pretty careful about,
# and not to do by accident.
......
......@@ -485,6 +485,20 @@ def test_shape():
assert isinstance(topo[1].op,tensor.opt.Shape_i)
assert isinstance(topo[2].op,tensor.opt.MakeVector)
def test_may_share_memory():
a=scipy.sparse.csc_matrix(scipy.sparse.eye(5,3))
b=scipy.sparse.csc_matrix(scipy.sparse.eye(4,3))
as_ar = lambda a: theano._asarray(a, dtype='int32')
for a_,b_,rep in [(a,a,True),(b,b,True),(a,b,False),
(a,a.data,True),(a,a.indptr,True),(a,a.indices,True),(a,as_ar(a.shape),False),
(a.data,a,True),(a.indptr,a,True),(a.indices,a,True),(as_ar(a.shape),a,False),
(b,b.data,True),(b,b.indptr,True),(b,b.indices,True),(b,as_ar(b.shape),False),
(b.data,b,True),(b.indptr,b,True),(b.indices,b,True),(as_ar(b.shape),b,False),
(b.data,a,False),(b.indptr,a,False),(b.indices,a,False),(as_ar(b.shape),a,False),
]:
assert SparseType.may_share_memory(a_,b_)==rep
import theano.tensor.tests.test_sharedvar
test_shared_options=theano.tensor.tests.test_sharedvar.makeSharedTester(
theano.sparse.shared, 'float64',
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论