Implemented StructuredDotCSR (with fast c-implementation)

上级 dff4d08c
差异被折叠。
...@@ -155,60 +155,54 @@ import scipy.sparse as sp ...@@ -155,60 +155,54 @@ import scipy.sparse as sp
class test_structureddot(unittest.TestCase): class test_structureddot(unittest.TestCase):
def test_structuredot(self): def test_structuredot(self):
#bsize = 5
#spmat = sp.csc_matrix((8,15))
#spmat[1,2] = 3
#spmat[4,7] = 6
#spmat[2,7] = 72
#spmat[1,9] = 2
#spmat[7,12] = 1
#spmat[4,2] = 7
bsize = 2 bsize = 2
spmat = sp.csc_matrix((5,5)) spmat = sp.csc_matrix((5,5))
spmat[1,2] = 1 spmat[0,1] = 1
spmat[0,1] = 2 spmat[0,2] = 2
spmat[0,2] = 3 spmat[1,2] = 3
spmat[1,4] = 4
spmat[3,4] = 5
kerns = tensor.dvector() kerns = tensor.dvector('kerns')
images = tensor.dmatrix() images = tensor.dmatrix('images')
##
# Test compressed-sparse column matrices ###
##
# build symbolic theano graph
def buildgraphCSC(kerns,images): def buildgraphCSC(kerns,images):
csc = CSC(kerns, spmat.indices[:spmat.size], spmat.indptr, spmat.shape) csc = CSC(kerns, spmat.indices[:spmat.size], spmat.indptr, spmat.shape)
return structured_dot(csc, images.T) return structured_dot(csc, images.T)
out = buildgraphCSC(kerns,images) out = buildgraphCSC(kerns,images)
f = theano.function([kerns,images], out) f = theano.function([kerns,images], out)
# compute theano outputs
kernvals = spmat.data[:spmat.size] kernvals = spmat.data[:spmat.size]
imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1]) imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1])
outvals = f(kernvals,imvals) outvals = f(kernvals,imvals)
print type(spmat.dot(imvals.T)) # compare to scipy
print spmat.dot(imvals.T)
print dir(spmat.dot(imvals.T))
# scipy 0.7.0 should already make the output dense
# assert numpy.all(outvals == spmat.dot(imvals.T).todense())
c = spmat.dot(imvals.T) c = spmat.dot(imvals.T)
assert _is_dense(c) assert _is_dense(c)
assert numpy.all(outvals == c) assert numpy.all(outvals == c)
tensor.verify_grad(None, buildgraphCSC, [kernvals,imvals]) tensor.verify_grad(None, buildgraphCSC, [kernvals,imvals])
##
# Test compressed-sparse row matrices ###
##
spmat = spmat.tocsr() spmat = spmat.tocsr()
# build theano graph
def buildgraphCSR(kerns,images): def buildgraphCSR(kerns,images):
csr = CSR(kerns, spmat.indices[:spmat.size], spmat.indptr, spmat.shape) csr = CSR(kerns, spmat.indices[:spmat.size], spmat.indptr, spmat.shape)
return structured_dot(csr, images.T) return structured_dot(csr, images.T)
out = buildgraphCSR(kerns,images) out = buildgraphCSR(kerns,images)
f = theano.function([kerns,images], out) f = theano.function([kerns,images], out)
# compute theano output
kernvals = spmat.data[:spmat.size] kernvals = spmat.data[:spmat.size]
imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1]) imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1])
outvals = f(kernvals,imvals) outvals = f(kernvals,imvals)
# compare to scipy
# scipy 0.7.0 should already make the output dense
# assert numpy.all(outvals == spmat.dot(imvals.T).todense())
c = spmat.dot(imvals.T) c = spmat.dot(imvals.T)
assert _is_dense(c) assert _is_dense(c)
assert numpy.all(outvals == c) assert numpy.all(outvals == c)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论