提交 b9f75a3d authored 作者: Valentin Bisson's avatar Valentin Bisson

Fixed Remove0 testing.

上级 e9697dc4
...@@ -26,43 +26,44 @@ class TestSP(unittest.TestCase): ...@@ -26,43 +26,44 @@ class TestSP(unittest.TestCase):
# fixed parameters # fixed parameters
bsize = 10 # batch size bsize = 10 # batch size
imshp = (28,28) imshp = (28, 28)
kshp = (5,5) kshp = (5, 5)
nkern = 5 nkern = 5
ssizes = ((1,1),(2,2),(3,3),(4,4)) ssizes = ((1, 1), (2, 2), (3, 3), (4, 4))
convmodes = ('full','valid') convmodes = ('full', 'valid')
# symbolic stuff # symbolic stuff
bias = tensor.dvector() bias = tensor.dvector()
kerns = tensor.dmatrix() kerns = tensor.dmatrix()
input = tensor.dmatrix() input = tensor.dmatrix()
rng = numpy.random.RandomState(3423489) rng = numpy.random.RandomState(3423489)
filters = rng.randn(nkern,numpy.prod(kshp)) filters = rng.randn(nkern, numpy.prod(kshp))
biasvals = rng.randn(nkern) biasvals = rng.randn(nkern)
for mode in ('FAST_COMPILE','FAST_RUN'): #, profmode): for mode in ('FAST_COMPILE', 'FAST_RUN'): # , profmode):
ttot, ntot = 0, 0 ttot, ntot = 0, 0
for conv_mode in convmodes: for conv_mode in convmodes:
for ss in ssizes: for ss in ssizes:
output, outshp = sp.convolve(kerns, kshp, nkern, input,\ output, outshp = sp.convolve(kerns, kshp, nkern, input,\
imshp, ss, bias=bias, mode=conv_mode) imshp, ss, bias=bias, mode=conv_mode)
f = function([kerns, bias, input], output, mode=mode) f = function([kerns, bias, input], output, mode=mode)
# now test with real values # now test with real values
img2d = numpy.arange(bsize*numpy.prod(imshp)).reshape((bsize,)+imshp) img2d = numpy.arange(bsize * numpy.prod(imshp)).reshape(( \
img1d = img2d.reshape(bsize,-1) bsize,) + imshp)
img1d = img2d.reshape(bsize, -1)
# create filters (need to be flipped to use convolve2d) # create filters (need to be flipped to use convolve2d)
filtersflipped = numpy.zeros((nkern,)+kshp) filtersflipped = numpy.zeros((nkern,) + kshp)
for k in range(nkern): for k in range(nkern):
it = reversed(filters[k,:]) it = reversed(filters[k, :])
for i in range(kshp[0]): for i in range(kshp[0]):
for j in range(kshp[1]): for j in range(kshp[1]):
filtersflipped[k,i,j] = it.next() filtersflipped[k,i,j] = it.next()
# compute output with convolve2d # compute output with convolve2d
if conv_mode=='valid': if conv_mode == 'valid':
fulloutshp = numpy.array(imshp) - numpy.array(kshp) + 1 fulloutshp = numpy.array(imshp) - numpy.array(kshp) + 1
else: else:
fulloutshp = numpy.array(imshp) + numpy.array(kshp) - 1 fulloutshp = numpy.array(imshp) + numpy.array(kshp) - 1
...@@ -70,11 +71,11 @@ class TestSP(unittest.TestCase): ...@@ -70,11 +71,11 @@ class TestSP(unittest.TestCase):
refout = numpy.zeros((bsize,)+tuple(fulloutshp)+(nkern,)) refout = numpy.zeros((bsize,)+tuple(fulloutshp)+(nkern,))
for b in range(bsize): for b in range(bsize):
for n in range(nkern): for n in range(nkern):
refout[b,...,n] = convolve2d(\ refout[b,...,n] = convolve2d(img2d[b,:,:],
img2d[b,:,:], filtersflipped[n,...],conv_mode) filtersflipped[n,...],
conv_mode)
ntot += time.time() - ntime1 ntot += time.time() - ntime1
# need to flatten images # need to flatten images
bench1 = refout[:,0::ss[0],0::ss[1],:].reshape(bsize,-1,nkern) bench1 = refout[:,0::ss[0],0::ss[1],:].reshape(bsize,-1,nkern)
bench1 += biasvals.reshape(1,1,nkern) bench1 += biasvals.reshape(1,1,nkern)
...@@ -428,32 +429,31 @@ class TestSP(unittest.TestCase): ...@@ -428,32 +429,31 @@ class TestSP(unittest.TestCase):
def test_remove0(): def test_remove0():
print print
print 'test_remove0()' print 'test_remove0()'
import scipy
import numpy as N
configs=[ configs=[
# structure type, numpy matching class, optimized # structure type, numpy matching class
('csc',scipy.sparse.csc_matrix), ('csc',scipy.sparse.csc_matrix),
('csr',scipy.sparse.csr_matrix), ('csr',scipy.sparse.csr_matrix),
] ]
for format,matrix_class in configs: for format,matrix_class in configs:
print 'config: format=\'%(format)s\', matrix_class=%(matrix_class)s'%locals() print 'config: format=\'%(format)s\', matrix_class=%(matrix_class)s'%locals()
# real # real
origin = N.arange(9).reshape((3,3)).astype(theano.config.floatX) origin = (numpy.arange(9)+1).reshape((3,3)).astype(theano.config.floatX)
with0= matrix_class(origin).astype(theano.config.floatX) with0 = matrix_class(origin).astype(theano.config.floatX)
with0[0,1]=with0[1,0]=with0[2,2]=0
with0[0,1] = with0[1,0] = with0[2,2] = 0
assert with0.size == 9
# symbolic # symbolic
x = theano.sparse.SparseType(format=format,dtype=theano.config.floatX)() x = theano.sparse.SparseType(format=format, dtype=theano.config.floatX)()
f=theano.function([x],sp.Remove0()(x)) f = theano.function([x], sp.Remove0()(x))
# checking # checking
with0.eliminate_zeros()
# makes sense to change its name # makes sense to change its name
target=with0 target = with0
assert (f(with0).todense()==target.todense()).all() result = f(with0)
with0.eliminate_zeros()
assert result.size == target.size, 'Matrices sizes differ. Have zeros been removed ?'
def test_diagonal(): def test_diagonal():
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论