* Verified StructuredDotGradCSR (everything looks ok). Added more loops around

unit test just to make sure. Also changed dimensions of matrix in unit test not to be square.
上级 19239b85
...@@ -1193,7 +1193,7 @@ class StructuredDotGradCSR(gof.Op): ...@@ -1193,7 +1193,7 @@ class StructuredDotGradCSR(gof.Op):
const npy_int32 * __restrict__ indptr = (npy_int32 *)%(_indptr)s->data; const npy_int32 * __restrict__ indptr = (npy_int32 *)%(_indptr)s->data;
const npy_int32 * __restrict__ indices = (npy_int32 *)%(_indices)s->data; const npy_int32 * __restrict__ indices = (npy_int32 *)%(_indices)s->data;
// loop over rows // loop over columns of sparse matrix
for (npy_int32 i = 0; i < N; ++i) for (npy_int32 i = 0; i < N; ++i)
{ {
// for each non-null value in the sparse row // for each non-null value in the sparse row
......
...@@ -153,61 +153,65 @@ class T_conversion(unittest.TestCase): ...@@ -153,61 +153,65 @@ class T_conversion(unittest.TestCase):
import scipy.sparse as sp import scipy.sparse as sp
class test_structureddot(unittest.TestCase): class test_structureddot(unittest.TestCase):
def setUp(self):
unittest_tools.seed_rng()
def test_structuredot(self): def test_structuredot(self):
bsize = 2 bsize = 2
spmat = sp.csc_matrix((5,5))
spmat[0,1] = 1 # iterate 10 times just to make sure (cannot get this wrong !)
spmat[0,2] = 2 for i in range(10):
spmat[1,2] = 3 spmat = sp.csc_matrix((4,6))
spmat[1,4] = 4 for i in range(5):
spmat[3,4] = 5 x = numpy.floor(numpy.random.rand()*spmat.shape[0])
y = numpy.floor(numpy.random.rand()*spmat.shape[1])
kerns = tensor.dvector('kerns') spmat[x,y] = numpy.random.rand()*10
images = tensor.dmatrix('images')
kerns = tensor.dvector('kerns')
## images = tensor.dmatrix('images')
# Test compressed-sparse column matrices ###
## ##
# Test compressed-sparse column matrices ###
# build symbolic theano graph ##
def buildgraphCSC(kerns,images):
csc = CSC(kerns, spmat.indices[:spmat.size], spmat.indptr, spmat.shape) # build symbolic theano graph
return structured_dot(csc, images.T) def buildgraphCSC(kerns,images):
out = buildgraphCSC(kerns,images) csc = CSC(kerns, spmat.indices[:spmat.size], spmat.indptr, spmat.shape)
f = theano.function([kerns,images], out) return structured_dot(csc, images.T)
# compute theano outputs out = buildgraphCSC(kerns,images)
kernvals = spmat.data[:spmat.size] f = theano.function([kerns,images], out)
imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1]) # compute theano outputs
outvals = f(kernvals,imvals) kernvals = spmat.data[:spmat.size]
# compare to scipy imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1])
c = spmat.dot(imvals.T) outvals = f(kernvals,imvals)
assert _is_dense(c) # compare to scipy
assert numpy.all(outvals == c) c = spmat.dot(imvals.T)
assert _is_dense(c)
tensor.verify_grad(None, buildgraphCSC, [kernvals,imvals]) assert numpy.all(outvals == c)
## tensor.verify_grad(None, buildgraphCSC, [kernvals,imvals])
# Test compressed-sparse row matrices ###
## ##
spmat = spmat.tocsr() # Test compressed-sparse row matrices ###
##
# build theano graph spmat = spmat.tocsr()
def buildgraphCSR(kerns,images):
csr = CSR(kerns, spmat.indices[:spmat.size], spmat.indptr, spmat.shape) # build theano graph
return structured_dot(csr, images.T) def buildgraphCSR(kerns,images):
out = buildgraphCSR(kerns,images) csr = CSR(kerns, spmat.indices[:spmat.size], spmat.indptr, spmat.shape)
f = theano.function([kerns,images], out) return structured_dot(csr, images.T)
# compute theano output out = buildgraphCSR(kerns,images)
kernvals = spmat.data[:spmat.size] f = theano.function([kerns,images], out)
imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1]) # compute theano output
outvals = f(kernvals,imvals) kernvals = spmat.data[:spmat.size]
# compare to scipy imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1])
c = spmat.dot(imvals.T) outvals = f(kernvals,imvals)
assert _is_dense(c) # compare to scipy
assert numpy.all(outvals == c) c = spmat.dot(imvals.T)
assert _is_dense(c)
tensor.verify_grad(None, buildgraphCSR, [kernvals,imvals]) assert numpy.all(outvals == c)
tensor.verify_grad(None, buildgraphCSR, [kernvals,imvals])
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论