提交 7197ebcd authored 作者: Frederic Bastien's avatar Frederic Bastien

pep8

上级 a1304650
......@@ -8,11 +8,11 @@ from theano.sandbox import multinomial
from theano.compile.mode import get_default_mode, predefined_linkers
import theano.sandbox.cuda as cuda
import theano.tests.unittest_tools as utt
import six.moves.cPickle as pickle
import os
from theano.compat import PY3
from theano.misc.pkl_utils import CompatUnpickler
def get_mode(gpu):
mode = get_default_mode()
mode = copy.copy(mode)
......@@ -42,9 +42,9 @@ def test_n_samples_1():
numpy.random.seed(12345)
for i in [1, 5, 10, 100, 1000, 10000]:
uni = numpy.random.rand(2*i).astype(config.floatX)
uni = numpy.random.rand(2 * i).astype(config.floatX)
res = f([[1.0, 0.0], [0.0, 1.0]], uni, i)
utt.assert_allclose(res, [[i*1.0, 0.0], [0.0, i*1.0]])
utt.assert_allclose(res, [[i * 1.0, 0.0], [0.0, i * 1.0]])
def test_n_samples_2():
......@@ -58,18 +58,20 @@ def test_n_samples_2():
numpy.random.seed(12345)
for i in [1, 5, 10, 100, 1000]:
uni = numpy.random.rand(i).astype(config.floatX)
pvals = numpy.random.randint(1,1000,(1,1000)).astype(config.floatX)
pvals = numpy.random.randint(1, 1000, (1, 1000)).astype(config.floatX)
pvals /= pvals.sum(1)
res = f(pvals, uni, i)
assert res.sum() == i
for i in [1, 5, 10, 100, 1000]:
uni = numpy.random.rand(i).astype(config.floatX)
pvals = numpy.random.randint(1,1000000,(1,1000000)).astype(config.floatX)
pvals = numpy.random.randint(
1, 1000000, (1, 1000000)).astype(config.floatX)
pvals /= pvals.sum(1)
res = f(pvals, uni, i)
assert res.sum() == i
def test_n_samples_compatibility():
"""
This test checks if the new change to MultinomialFromUniform is still compatible
......@@ -83,16 +85,18 @@ def test_n_samples_compatibility():
pickle.dump([X, samples], open("multinomial_test_graph.pkl", "w"))
"""
folder = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(folder, "multinomial_test_graph.pkl"), "rb") as pkl_file:
with open(os.path.join(folder, "multinomial_test_graph.pkl"),
"rb") as pkl_file:
if PY3:
u = CompatUnpickler(pkl_file, encoding="latin1")
else:
u = CompatUnpickler(pkl_file)
X, samples = u.load()
f = theano.function([X], samples)
res = f(numpy.random.randn(20,10))
res = f(numpy.random.randn(20, 10))
assert numpy.all(res.sum(axis=1) == 1)
def test_multinomial_0():
# This tests the MultinomialFromUniform Op directly, not going through the
# multinomial() call in GPU random generation.
......@@ -104,7 +108,7 @@ def test_multinomial_0():
def body(mode, gpu):
# 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:
assert any([type(node.op) is multinomial.GpuMultinomialFromUniform
......@@ -140,12 +144,12 @@ def test_multinomial_large():
p = tensor.fmatrix()
u = tensor.fvector()
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:
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]
uval = numpy.ones_like(pval[:, 0]) * 0.5
mval = f(pval, uval)
......@@ -160,7 +164,7 @@ def test_multinomial_large():
else:
raise NotImplementedError(config.cast_policy)
utt.assert_allclose(mval.sum(axis=1), 2)
asdf = numpy.asarray([0, 0, 2, 0])+0*pval
asdf = numpy.asarray([0, 0, 2, 0]) + 0 * pval
utt.assert_allclose(mval, asdf) # broadcast over all rows
run_with_c(body)
if cuda.cuda_available:
......@@ -201,10 +205,10 @@ def test_gpu_opt():
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()])
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]
uval = numpy.ones_like(pval[:, 0]) * 0.5
mval = f(pval, uval)
f(pval, uval)
# Test with a row, it was failing in the past.
r = tensor.frow()
......@@ -215,7 +219,7 @@ def test_gpu_opt():
f = function([r, 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()])
pval = numpy.arange(1 * 4, dtype='float32').reshape((1, 4))+0.1
pval = numpy.arange(1 * 4, dtype='float32').reshape((1, 4)) + 0.1
pval = pval / pval.sum(axis=1)[:, None]
uval = numpy.ones_like(pval[:, 0]) * 0.5
mval2 = f(pval, uval)
f(pval, uval)
......@@ -104,7 +104,6 @@ whitelist_flake8 = [
"sandbox/tests/test_scan.py",
"sandbox/tests/test_rng_mrg.py",
"sandbox/tests/test_neighbourhoods.py",
"sandbox/tests/test_multinomial.py",
"sandbox/tests/__init__.py",
"sandbox/cuda/var.py",
"sandbox/cuda/GpuConvGrad3D.py",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论