提交 3f031546 authored 作者: James Bergstra's avatar James Bergstra

changes to sparse, more types supported

上级 48240dcd
差异被折叠。
...@@ -161,62 +161,154 @@ class test_structureddot(unittest.TestCase): ...@@ -161,62 +161,154 @@ class test_structureddot(unittest.TestCase):
def test_structuredot(self): def test_structuredot(self):
bsize = 2 bsize = 2
typenames = 'int8', 'int32', 'int16', 'int64', 'float32', 'float64', 'complex64', 'complex128'
# iterate 10 times just to make sure (cannot get this wrong !) for sparse_dtype in typenames:
for i in range(10): for dense_dtype in typenames:
spmat = sp.lil_matrix((4,6))
for i in range(5): output_dtype = theano.scalar.upcast(sparse_dtype, dense_dtype)
x = numpy.floor(numpy.random.rand()*spmat.shape[0]) #print 'output_dtype = ', output_dtype
y = numpy.floor(numpy.random.rand()*spmat.shape[1])
spmat[x,y] = numpy.random.rand()*10 #print '** sparse_dtype = ', sparse_dtype
spmat = sp.csc_matrix(spmat) #print '** dense_dtype = ', dense_dtype
kerns = tensor.dvector('kerns') # iterate for a few different random graph patterns
images = tensor.dmatrix('images') for i in range(10):
spmat = sp.csc_matrix((4,6), dtype=sparse_dtype)
## for i in range(5):
# Test compressed-sparse column matrices ### # set non-zeros in random locations (row x, col y)
## x = numpy.floor(numpy.random.rand()*spmat.shape[0])
y = numpy.floor(numpy.random.rand()*spmat.shape[1])
# build symbolic theano graph spmat[x,y] = numpy.random.rand()*10
def buildgraphCSC(kerns,images): spmat = sp.csc_matrix(spmat)
csc = CSC(kerns, spmat.indices[:spmat.size], spmat.indptr, spmat.shape)
return structured_dot(csc, images.T) kerns = tensor.Tensor(sparse_dtype, broadcastable=[False])('kerns')
out = buildgraphCSC(kerns,images) images = tensor.Tensor(dense_dtype, broadcastable=[False, False])('images')
f = theano.function([kerns,images], out) #print 'kerns.dtype = ', kerns.dtype
# compute theano outputs #print 'images.dtype = ', images.dtype
kernvals = spmat.data[:spmat.size]
imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1]) ##
outvals = f(kernvals,imvals) # Test compressed-sparse column matrices ###
# compare to scipy ##
c = spmat * (imvals.T)
assert _is_dense(c) # build symbolic theano graph
assert numpy.all(outvals == c) def buildgraphCSC(kerns,images):
csc = CSC(kerns, spmat.indices[:spmat.size], spmat.indptr, spmat.shape)
utt.verify_grad(buildgraphCSC, [kernvals,imvals]) assert csc.type.dtype == sparse_dtype
return structured_dot(csc, images.T)
##
# Test compressed-sparse row matrices ### out = buildgraphCSC(kerns,images)
## f = theano.function([kerns,images], out)
spmat = spmat.tocsr()
# compute theano outputs
# build theano graph #print 'spmat.data', spmat.data.dtype.num
def buildgraphCSR(kerns,images): kernvals = numpy.array(spmat.data[:spmat.size])
csr = CSR(kerns, spmat.indices[:spmat.size], spmat.indptr, spmat.shape) #print 'kdtype', kernvals.dtype, kernvals.shape, kernvals.ndim, kernvals.dtype.num
return structured_dot(csr, images.T) #print 'type of kernvals = ', kernvals.dtype
out = buildgraphCSR(kerns,images) imvals = 1.0 * numpy.array(numpy.arange(bsize*spmat.shape[1]).\
f = theano.function([kerns,images], out) reshape(bsize,spmat.shape[1]), dtype=dense_dtype)
# compute theano output outvals = f(kernvals,imvals)
kernvals = spmat.data[:spmat.size]
imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1]) # compare to scipy
outvals = f(kernvals,imvals) c = spmat * (imvals.T)
# compare to scipy assert _is_dense(c)
c = spmat * (imvals.T) assert str(outvals.dtype) == output_dtype
assert _is_dense(c)
assert numpy.all(outvals == c) assert numpy.all(numpy.abs(outvals -
numpy.array(c, dtype=output_dtype)) < 1e-4)
#if sparse_dtype.startswith('float') and dense_dtype.startswith('float'):
#utt.verify_grad(buildgraphCSC, [kernvals,imvals])
def notest(self):
##
# Test compressed-sparse row matrices ###
##
spmat = spmat.tocsr()
# build theano graph
def buildgraphCSR(kerns,images):
csr = CSR(kerns, spmat.indices[:spmat.size], spmat.indptr, spmat.shape)
assert csr.type.dtype == sparse_dtype
return structured_dot(csr, images.T)
out = buildgraphCSR(kerns,images)
f = theano.function([kerns,images], out)
# compute theano output
kernvals = spmat.data[:spmat.size]
imvals = 1.0 * numpy.arange(bsize*spmat.shape[1]).reshape(bsize,spmat.shape[1])
outvals = f(kernvals,imvals)
# compare to scipy
c = spmat * (imvals.T)
assert _is_dense(c)
assert str(outvals.dtype) == output_dtype
if not numpy.all(numpy.abs(outvals - numpy.array(c, dtype=output_dtype)) < 1e-5):
print numpy.abs(outvals - numpy.array(c, dtype=output_dtype))
assert numpy.all(numpy.abs(outvals -
numpy.array(c, dtype=output_dtype)) < 1e-4)
# we could test more, but hopefully this suffices?
if sparse_dtype.startswith('float') and dense_dtype.startswith('float'):
utt.verify_grad( buildgraphCSR, [kernvals,imvals]) utt.verify_grad( buildgraphCSR, [kernvals,imvals])
def test_opt_unpack(self):
kerns = tensor.Tensor(dtype='int64', broadcastable=[False])('kerns')
spmat = sp.csc_matrix((4,6), dtype='int64')
for i in range(5):
# set non-zeros in random locations (row x, col y)
x = numpy.floor(numpy.random.rand()*spmat.shape[0])
y = numpy.floor(numpy.random.rand()*spmat.shape[1])
spmat[x,y] = numpy.random.rand()*10
spmat = sp.csc_matrix(spmat)
images = tensor.Tensor(dtype='float32', broadcastable=[False, False])('images')
cscmat = CSC(kerns, spmat.indices[:spmat.size], spmat.indptr, spmat.shape)
f = theano.function([kerns, images], structured_dot(cscmat, images.T))
sdcscpresent = False
for node in f.maker.env.toposort():
print node.op
assert not isinstance(node.op, CSM)
assert not isinstance(node.op, CSMProperties)
if isinstance(f.maker.env.toposort()[1].op, StructuredDotCSC):
sdcscpresent = True
assert sdcscpresent
kernvals = numpy.array(spmat.data[:spmat.size])
#print 'kdtype', kernvals.dtype, kernvals.shape, kernvals.ndim, kernvals.dtype.num
#print 'type of kernvals = ', kernvals.dtype
bsize = 3
imvals = 1.0 * numpy.array(numpy.arange(bsize*spmat.shape[1]).\
reshape(bsize,spmat.shape[1]), dtype='float32')
outvals = f(kernvals,imvals)
print outvals
def test_opt_ones(self):
spmat = sp.csc_matrix((4,6), dtype='int64')
for i in range(5):
# set 1s in random locations (row x, col y)
x = numpy.floor(numpy.random.rand()*spmat.shape[0])
y = numpy.floor(numpy.random.rand()*spmat.shape[1])
spmat[x,y] = 1
spmat = sp.csc_matrix(spmat)
images = tensor.Tensor(dtype='float32', broadcastable=[False, False])('images')
f = theano.function([images], structured_dot(spmat, images.T))
sdones_present = False
for i, node in enumerate(f.maker.env.toposort()):
print ' ', i, node.op
if isinstance(node.op, StructuredDotCSC1):
sdones_present = True
assert sdones_present
#print 'kdtype', kernvals.dtype, kernvals.shape, kernvals.ndim, kernvals.dtype.num
#print 'type of kernvals = ', kernvals.dtype
bsize = 3
imvals = 1.0 * numpy.array(numpy.arange(bsize*spmat.shape[1]).\
reshape(bsize,spmat.shape[1]), dtype='float32')
outvals = f(imvals)
print outvals
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论