def test_multilayer_sparse(self): # fixed parameters bsize = 10 # batch size imshp = (5,5) kshp = ((3,3),(2,2)) nkerns = (10,20) # per output pixel ssizes = ((1,1),(2,2)) convmodes = ('full','valid',) # symbolic stuff kerns = [tensor.dvector(),tensor.dvector()] input = tensor.dmatrix() rng = numpy.random.RandomState(3423489) # build actual input images img2d = numpy.arange(bsize*numpy.prod(imshp)).reshape((bsize,)+imshp) img1d = img2d.reshape(bsize,-1) for mode in ('FAST_COMPILE','FAST_RUN'): for conv_mode in convmodes: for ss in ssizes: l1hid, l1outshp = sp.applySparseFilter(kerns[0], kshp[0],\ nkerns[0], input, imshp, ss, mode=conv_mode) l2hid, l2outshp = sp.applySparseFilter(kerns[1], kshp[1],\ nkerns[1], l1hid, l1outshp, ss, mode=conv_mode) l1propup = function([kerns[0], input], l1hid, mode=mode) l2propup = function([kerns[1], l1hid], l2hid, mode=mode) # actual values l1kernvals = numpy.arange(numpy.prod(l1outshp)*numpy.prod(kshp[0])) l2kernvals = numpy.arange(numpy.prod(l2outshp)*numpy.prod(kshp[1])*nkerns[0]) l1hidval = l1propup(l1kernvals,img1d) l2hidval = l2propup(l2kernvals,l1hidval)
def test_multilayer_sparse(self): # fixed parameters bsize = 10 # batch size imshp = (5, 5) kshp = ((3, 3), (2, 2)) nkerns = (10, 20) # per output pixel ssizes = ((1, 1), (2, 2)) convmodes = ('full', 'valid',) # symbolic stuff kerns = [tensor.dvector(), tensor.dvector()] input = tensor.dmatrix() rng = numpy.random.RandomState(3423489) # build actual input images img2d = numpy.arange(bsize*numpy.prod(imshp)).reshape((bsize,)+imshp) img1d = img2d.reshape(bsize, -1) for mode in ('FAST_COMPILE', 'FAST_RUN'): for conv_mode in convmodes: for ss in ssizes: l1hid, l1outshp = sp.applySparseFilter(kerns[0], kshp[0],\ nkerns[0], input, imshp, ss, mode=conv_mode) l2hid, l2outshp = sp.applySparseFilter(kerns[1], kshp[1],\ nkerns[1], l1hid, l1outshp, ss, mode=conv_mode) l1propup = function([kerns[0], input], l1hid, mode=mode) l2propup = function([kerns[1], l1hid], l2hid, mode=mode) # actual values l1kernvals = numpy.arange(numpy.prod(l1outshp)*numpy.prod(kshp[0])) l2kernvals = numpy.arange(numpy.prod(l2outshp)*numpy.prod(kshp[1])*nkerns[0]) l1hidval = l1propup(l1kernvals, img1d) l2hidval = l2propup(l2kernvals, l1hidval)
def test_sparse(self): print '\n\n*************************************************' print ' TEST SPARSE' print '*************************************************' # fixed parameters bsize = 10 # batch size imshp = (28, 28) kshp = (5, 5) nkern = 1 # per output pixel ssizes = ((1, 1), (2, 2)) convmodes = ( 'full', 'valid', ) # symbolic stuff bias = tensor.dvector() kerns = tensor.dvector() input = tensor.dmatrix() rng = numpy.random.RandomState(3423489) import theano.gof as gof #Mode(optimizer='fast_run', linker=gof.OpWiseCLinker(allow_gc=False)),): for mode in ('FAST_COMPILE', 'FAST_RUN'): #,profmode): ntot, ttot = 0, 0 for conv_mode in convmodes: for ss in ssizes: output, outshp = sp.applySparseFilter(kerns, kshp,\ nkern, input, imshp, ss, bias=bias, mode=conv_mode) f = function([kerns, bias, input], output, mode=mode) # build actual input images img2d = numpy.arange( bsize * numpy.prod(imshp)).reshape((bsize, ) + imshp) img1d = img2d.reshape(bsize, -1) zeropad_img = numpy.zeros((bsize,\ img2d.shape[1]+2*(kshp[0]-1),\ img2d.shape[2]+2*(kshp[1]-1))) zeropad_img[:, kshp[0] - 1:kshp[0] - 1 + img2d.shape[1], kshp[1] - 1:kshp[1] - 1 + img2d.shape[2]] = img2d # build kernel matrix -- flatten it for theano stuff filters = numpy.arange(numpy.prod(outshp)*numpy.prod(kshp)).\ reshape(nkern,numpy.prod(outshp[1:]),numpy.prod(kshp)) spfilt = filters.flatten() biasvals = numpy.arange(numpy.prod(outshp)) # compute output by hand ntime1 = time.time() refout = numpy.zeros((bsize, nkern, outshp[1], outshp[2])) patch = numpy.zeros((kshp[0], kshp[1])) for b in xrange(bsize): for k in xrange(nkern): pixi = 0 # pixel index in raster order for j in xrange(outshp[1]): for i in xrange(outshp[2]): n = j * ss[0] m = i * ss[1] patch = zeropad_img[b, n:n + kshp[0], m:m + kshp[1]] refout[b,k,j,i] = numpy.dot(filters[k,pixi,:],\ patch.flatten()) pixi += 1 refout = refout.reshape(bsize, -1) + biasvals ntot += time.time() - ntime1 # need to flatten images ttime1 = time.time() out1 = f(spfilt, biasvals, img1d) ttot += time.time() - ttime1 temp = refout - out1 assert (temp < 1e-10).all() # test downward propagation vis = tensor.grad(0.5 * tensor.sqr(output).sum(), input) downprop = function([kerns, output], vis) temp1 = time.time() for zz in range(100): visval = downprop(spfilt, out1) indices, indptr, spmat_shape, sptype, outshp, kmap = \ sp.convolution_indices.sparse_eval(imshp,kshp,nkern,ss,conv_mode) spmat = sparse.csc_matrix((spfilt[kmap], indices, indptr), spmat_shape) visref = numpy.dot(out1, spmat.todense()) assert numpy.all(visref == visval), (visref, visval) print '**** Sparse Profiling Results (', mode, ') ****' print 'Numpy processing time: ', ntot print 'Theano processing time: ', ttot
def test_sparse(self): # print '\n\n*************************************************' # print ' TEST SPARSE' # print '*************************************************' # fixed parameters bsize = 10 # batch size imshp = (28, 28) kshp = (5, 5) nkern = 1 # per output pixel ssizes = ((1, 1), (2, 2)) convmodes = ("full", "valid") # symbolic stuff bias = tensor.dvector() kerns = tensor.dvector() input = tensor.dmatrix() rng = numpy.random.RandomState(3423489) import theano.gof as gof # Mode(optimizer='fast_run', linker=gof.OpWiseCLinker(allow_gc=False)),): for mode in ("FAST_COMPILE", "FAST_RUN"): # ,profmode): ntot, ttot = 0, 0 for conv_mode in convmodes: for ss in ssizes: output, outshp = sp.applySparseFilter( kerns, kshp, nkern, input, imshp, ss, bias=bias, mode=conv_mode ) f = function([kerns, bias, input], output, mode=mode) # build actual input images img2d = numpy.arange(bsize * numpy.prod(imshp)).reshape((bsize,) + imshp) img1d = img2d.reshape(bsize, -1) zeropad_img = numpy.zeros( (bsize, img2d.shape[1] + 2 * (kshp[0] - 1), img2d.shape[2] + 2 * (kshp[1] - 1)) ) zeropad_img[ :, kshp[0] - 1 : kshp[0] - 1 + img2d.shape[1], kshp[1] - 1 : kshp[1] - 1 + img2d.shape[2] ] = img2d # build kernel matrix -- flatten it for theano stuff filters = numpy.arange(numpy.prod(outshp) * numpy.prod(kshp)).reshape( nkern, numpy.prod(outshp[1:]), numpy.prod(kshp) ) spfilt = filters.flatten() biasvals = numpy.arange(numpy.prod(outshp)) # compute output by hand ntime1 = time.time() refout = numpy.zeros((bsize, nkern, outshp[1], outshp[2])) patch = numpy.zeros((kshp[0], kshp[1])) for b in xrange(bsize): for k in xrange(nkern): pixi = 0 # pixel index in raster order for j in xrange(outshp[1]): for i in xrange(outshp[2]): n = j * ss[0] m = i * ss[1] patch = zeropad_img[b, n : n + kshp[0], m : m + kshp[1]] refout[b, k, j, i] = numpy.dot(filters[k, pixi, :], patch.flatten()) pixi += 1 refout = refout.reshape(bsize, -1) + biasvals ntot += time.time() - ntime1 # need to flatten images ttime1 = time.time() out1 = f(spfilt, biasvals, img1d) ttot += time.time() - ttime1 temp = refout - out1 assert (temp < 1e-10).all() # test downward propagation vis = tensor.grad(0.5 * tensor.sqr(output).sum(), input) downprop = function([kerns, output], vis) temp1 = time.time() for zz in range(100): visval = downprop(spfilt, out1) indices, indptr, spmat_shape, sptype, outshp, kmap = sp.convolution_indices.sparse_eval( imshp, kshp, nkern, ss, conv_mode ) spmat = sparse.csc_matrix((spfilt[kmap], indices, indptr), spmat_shape) visref = numpy.dot(out1, spmat.todense()) assert numpy.all(visref == visval), (visref, visval)