提交 27f6c59e authored 作者: Frederic's avatar Frederic

pep8

上级 064feaf1
...@@ -9,15 +9,19 @@ from theano.compile.mode import get_default_mode, predefined_linkers ...@@ -9,15 +9,19 @@ from theano.compile.mode import get_default_mode, predefined_linkers
from theano.gof.python25 import any from theano.gof.python25 import any
import theano.sandbox.cuda as cuda import theano.sandbox.cuda as cuda
def get_mode(gpu): def get_mode(gpu):
mode = get_default_mode() mode = get_default_mode()
mode = copy.copy(mode) mode = copy.copy(mode)
if gpu: if gpu:
mode = mode.including('gpu', 'gpu_local_optimizations', 'local_cut_gpu_host_gpu', 'local_gpu_multinomial') mode = mode.including('gpu', 'gpu_local_optimizations',
'local_cut_gpu_host_gpu',
'local_gpu_multinomial')
if isinstance(mode.linker, theano.gof.PerformLinker): if isinstance(mode.linker, theano.gof.PerformLinker):
mode.linker = predefined_linkers['c|py'] mode.linker = predefined_linkers['c|py']
return mode return mode
def run_with_c(f, gpu=False): def run_with_c(f, gpu=False):
mode = get_mode(gpu) mode = get_mode(gpu)
f(mode, gpu) f(mode, gpu)
...@@ -30,52 +34,54 @@ def test_multinomial_0(): ...@@ -30,52 +34,54 @@ def test_multinomial_0():
p = tensor.fmatrix() p = tensor.fmatrix()
u = tensor.fvector() u = tensor.fvector()
m = multinomial.MultinomialFromUniform('auto')(p,u) m = multinomial.MultinomialFromUniform('auto')(p, u)
def body(mode, gpu): def body(mode, gpu):
#the m*2 allows the multinomial to reuse output #the m*2 allows the multinomial to reuse output
f = function([p,u], m*2, allow_input_downcast=True, mode=mode) f = function([p, u], m*2, allow_input_downcast=True, mode=mode)
if gpu: if gpu:
assert any([type(node.op) is multinomial.GpuMultinomialFromUniform for node in f.maker.fgraph.toposort()]) assert any([type(node.op) is multinomial.GpuMultinomialFromUniform
for node in f.maker.fgraph.toposort()])
# test that both first and second samples can be drawn # test that both first and second samples can be drawn
assert numpy.allclose(f([[1,0], [0,1]], [.1, .1]), assert numpy.allclose(f([[1, 0], [0, 1]], [.1, .1]),
[[2,0], [0,2]]) [[2, 0], [0, 2]])
# test that both second labels can be drawn # test that both second labels can be drawn
r = f([[.2,.8], [.3,.7]], [.31, .31]) r = f([[.2, .8], [.3, .7]], [.31, .31])
assert numpy.allclose(r, [[0,2], [0,2]]), r assert numpy.allclose(r, [[0, 2], [0, 2]]), r
# test that both first labels can be drawn # test that both first labels can be drawn
r = f([[.2,.8], [.3,.7]], [.21, .21]) r = f([[.2, .8], [.3, .7]], [.21, .21])
assert numpy.allclose(r, [[0,2], [2,0]]), r assert numpy.allclose(r, [[0, 2], [2, 0]]), r
#change the size to make sure output gets reallocated ok #change the size to make sure output gets reallocated ok
# and also make sure that the GPU version doesn't screw up the # and also make sure that the GPU version doesn't screw up the
# transposed-ness # transposed-ness
r = f([[.2,.8] ], [.25]) r = f([[.2, .8]], [.25])
assert numpy.allclose(r, [[0,2]]), r assert numpy.allclose(r, [[0, 2]]), r
run_with_c(body) run_with_c(body)
if cuda.cuda_available: if cuda.cuda_available:
run_with_c(body, True) run_with_c(body, True)
#TODO: check a bigger example (make sure blocking on GPU is handled correctly) #TODO: check a bigger example (make sure blocking on GPU is handled correctly)
def test_multinomial_large(): def test_multinomial_large():
# DEBUG_MODE will test this on GPU # DEBUG_MODE will test this on GPU
def body(mode, gpu): def body(mode, gpu):
p = tensor.fmatrix() p = tensor.fmatrix()
u = tensor.fvector() u = tensor.fvector()
m = multinomial.MultinomialFromUniform('auto')(p,u) m = multinomial.MultinomialFromUniform('auto')(p, u)
f = function([p,u], m*2, allow_input_downcast=True, mode=mode) f = function([p, u], m*2, allow_input_downcast=True, mode=mode)
if gpu: if gpu:
assert any([type(node.op) is multinomial.GpuMultinomialFromUniform for node in f.maker.fgraph.toposort()]) assert any([type(node.op) is multinomial.GpuMultinomialFromUniform
for node in f.maker.fgraph.toposort()])
pval = numpy.arange(10000 * 4, dtype='float32').reshape((10000, 4))+0.1 pval = numpy.arange(10000 * 4, dtype='float32').reshape((10000, 4))+0.1
pval = pval / pval.sum(axis=1)[:,None] pval = pval / pval.sum(axis=1)[:, None]
uval = numpy.ones_like(pval[:,0]) * 0.5 uval = numpy.ones_like(pval[:, 0]) * 0.5
mval = f(pval,uval) mval = f(pval, uval)
assert mval.shape == pval.shape assert mval.shape == pval.shape
if config.cast_policy == 'custom': if config.cast_policy == 'custom':
...@@ -88,7 +94,7 @@ def test_multinomial_large(): ...@@ -88,7 +94,7 @@ def test_multinomial_large():
raise NotImplementedError(config.cast_policy) raise NotImplementedError(config.cast_policy)
assert numpy.allclose(mval.sum(axis=1), 2) assert numpy.allclose(mval.sum(axis=1), 2)
asdf = numpy.asarray([0, 0, 2, 0])+0*pval asdf = numpy.asarray([0, 0, 2, 0])+0*pval
assert numpy.allclose(mval, asdf) #broadcast over all rows assert numpy.allclose(mval, asdf) # broadcast over all rows
run_with_c(body) run_with_c(body)
if cuda.cuda_available: if cuda.cuda_available:
run_with_c(body, True) run_with_c(body, True)
...@@ -97,36 +103,38 @@ def test_multinomial_large(): ...@@ -97,36 +103,38 @@ def test_multinomial_large():
def test_multinomial_dtypes(): def test_multinomial_dtypes():
p = tensor.dmatrix() p = tensor.dmatrix()
u = tensor.dvector() u = tensor.dvector()
m = multinomial.MultinomialFromUniform('auto')(p,u) m = multinomial.MultinomialFromUniform('auto')(p, u)
assert m.dtype == 'float64', m.dtype assert m.dtype == 'float64', m.dtype
p = tensor.fmatrix() p = tensor.fmatrix()
u = tensor.fvector() u = tensor.fvector()
m = multinomial.MultinomialFromUniform('auto')(p,u) m = multinomial.MultinomialFromUniform('auto')(p, u)
assert m.dtype == 'float32', m.dtype assert m.dtype == 'float32', m.dtype
p = tensor.fmatrix() p = tensor.fmatrix()
u = tensor.fvector() u = tensor.fvector()
m = multinomial.MultinomialFromUniform('float64')(p,u) m = multinomial.MultinomialFromUniform('float64')(p, u)
assert m.dtype == 'float64', m.dtype assert m.dtype == 'float64', m.dtype
def test_gpu_opt(): def test_gpu_opt():
if not cuda.cuda_available: if not cuda.cuda_available:
# Skip test if cuda_ndarray is not available. # Skip test if cuda_ndarray is not available.
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
raise SkipTest('Optional package cuda not available') raise SkipTest('Optional package cuda not available')
# We test the case where we put the op on the gpu when the output is moved to the gpu. # We test the case where we put the op on the gpu when the output
# is moved to the gpu.
p = tensor.fmatrix() p = tensor.fmatrix()
u = tensor.fvector() u = tensor.fvector()
m = multinomial.MultinomialFromUniform('auto')(p,u) m = multinomial.MultinomialFromUniform('auto')(p, u)
assert m.dtype == 'float32', m.dtype assert m.dtype == 'float32', m.dtype
m_gpu = cuda.gpu_from_host(m) m_gpu = cuda.gpu_from_host(m)
f = function([p,u], m_gpu, allow_input_downcast=True, mode=get_mode(True)) f = function([p, u], m_gpu, allow_input_downcast=True, mode=get_mode(True))
assert any([type(node.op) is multinomial.GpuMultinomialFromUniform for node in f.maker.fgraph.toposort()]) assert any([type(node.op) is multinomial.GpuMultinomialFromUniform
for node in f.maker.fgraph.toposort()])
pval = numpy.arange(10000 * 4, dtype='float32').reshape((10000, 4))+0.1 pval = numpy.arange(10000 * 4, dtype='float32').reshape((10000, 4))+0.1
pval = pval / pval.sum(axis=1)[:,None] pval = pval / pval.sum(axis=1)[:, None]
uval = numpy.ones_like(pval[:,0]) * 0.5 uval = numpy.ones_like(pval[:, 0]) * 0.5
mval = f(pval,uval) mval = f(pval, uval)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论