提交 ca3fc8f6 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Fix flake8.

上级 78657991
...@@ -13,7 +13,7 @@ from theano.tensor.tests.test_sharedvar import makeSharedTester ...@@ -13,7 +13,7 @@ from theano.tensor.tests.test_sharedvar import makeSharedTester
from .config import test_ctx_name from .config import test_ctx_name
from .test_basic_ops import rand_gpuarray from .test_basic_ops import rand_gpuarray
from ..type import GpuArrayType, gpuarray_shared_constructor from ..type import GpuArrayType, gpuarray_shared_constructor, get_context
import pygpu import pygpu
...@@ -93,14 +93,14 @@ test_shared_options = makeSharedTester( ...@@ -93,14 +93,14 @@ test_shared_options = makeSharedTester(
cls=pygpu._array.ndgpuarray), cls=pygpu._array.ndgpuarray),
test_internal_type_=lambda a: isinstance(a, pygpu.gpuarray.GpuArray), test_internal_type_=lambda a: isinstance(a, pygpu.gpuarray.GpuArray),
theano_fct_=theano.tensor.exp, theano_fct_=theano.tensor.exp,
ref_fct_=numpy.exp, ref_fct_=np.exp,
cast_value_=lambda v: pygpu.asarray(v, context=get_context(test_ctx_name), cast_value_=lambda v: pygpu.asarray(v, context=get_context(test_ctx_name),
cls=pygpu._array.ndgpuarray), cls=pygpu._array.ndgpuarray),
name='test_shared_options') name='test_shared_options')
test_shared_options2 = makeSharedTester( test_shared_options2 = makeSharedTester(
shared_constructor_=gpuarray_shared_constructor shared_constructor_=gpuarray_shared_constructor,
dtype_=theano.config.floatX, dtype_=theano.config.floatX,
get_value_borrow_true_alias_=False, get_value_borrow_true_alias_=False,
shared_borrow_true_alias_=False, shared_borrow_true_alias_=False,
...@@ -112,7 +112,7 @@ test_shared_options2 = makeSharedTester( ...@@ -112,7 +112,7 @@ test_shared_options2 = makeSharedTester(
cls=pygpu._array.ndgpuarray), cls=pygpu._array.ndgpuarray),
test_internal_type_=lambda a: isinstance(a, pygpu.gpuarray.GpuArray), test_internal_type_=lambda a: isinstance(a, pygpu.gpuarray.GpuArray),
theano_fct_=theano.tensor.exp, theano_fct_=theano.tensor.exp,
ref_fct_=numpy.exp, ref_fct_=np.exp,
cast_value_=lambda v: pygpu.asarray(v, context=get_context(test_ctx_name), cast_value_=lambda v: pygpu.asarray(v, context=get_context(test_ctx_name),
cls=pygpu._array.ndgpuarray), cls=pygpu._array.ndgpuarray),
name='test_shared_options2') name='test_shared_options2')
...@@ -5,11 +5,10 @@ import warnings ...@@ -5,11 +5,10 @@ import warnings
import theano import theano
from theano import Op, Apply from theano import Op, Apply
import theano.tensor as T import theano.tensor as T
from theano.gof import local_optimizer
from theano.tensor import NotScalarConstantError, get_scalar_constant_value
from theano.scalar import as_scalar from theano.scalar import as_scalar
import copy import copy
class MultinomialFromUniform(Op): class MultinomialFromUniform(Op):
# TODO : need description for parameter 'odtype' # TODO : need description for parameter 'odtype'
""" """
......
...@@ -9,7 +9,6 @@ import numpy as np ...@@ -9,7 +9,6 @@ import numpy as np
import theano import theano
from theano import config, function, tensor from theano import config, function, tensor
from theano.sandbox import multinomial from theano.sandbox import multinomial
from theano.compile.mode import get_default_mode
import theano.tests.unittest_tools as utt import theano.tests.unittest_tools as utt
from theano.compat import PY3 from theano.compat import PY3
from theano.misc.pkl_utils import CompatUnpickler from theano.misc.pkl_utils import CompatUnpickler
...@@ -127,7 +126,7 @@ def test_multinomial_large(): ...@@ -127,7 +126,7 @@ def test_multinomial_large():
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)
pval = np.arange(10000 * 4, dtype='float32').reshape((10000, 4)) + 0.1 pval = np.arange(10000 * 4, dtype='float32').reshape((10000, 4)) + 0.1
pval = pval / pval.sum(axis=1)[:, None] pval = pval / pval.sum(axis=1)[:, None]
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import copy
import os import os
import sys import sys
import time import time
import unittest import unittest
import functools
from nose.plugins.skip import SkipTest
from nose.tools import assert_raises from nose.tools import assert_raises
import numpy as np import numpy as np
from six.moves import xrange from six.moves import xrange
...@@ -245,8 +242,8 @@ def test_uniform(): ...@@ -245,8 +242,8 @@ def test_uniform():
# TODO: test ndim!=size.ndim # TODO: test ndim!=size.ndim
# TODO: test bad seed # TODO: test bad seed
# TODO: test size=Var, with shape that change from call to call # TODO: test size=Var, with shape that change from call to call
if (mode in ['DEBUG_MODE', 'DebugMode', 'FAST_COMPILE'] or if (config.mode in ['DEBUG_MODE', 'DebugMode', 'FAST_COMPILE'] or
mode == 'Mode' and config.linker in ['py']): config.mode == 'Mode' and config.linker in ['py']):
sample_size = (10, 100) sample_size = (10, 100)
steps = 50 steps = 50
else: else:
...@@ -266,8 +263,6 @@ def test_uniform(): ...@@ -266,8 +263,6 @@ def test_uniform():
# TEST CPU IMPLEMENTATION # TEST CPU IMPLEMENTATION
# The python and C implementation are tested with DebugMode # The python and C implementation are tested with DebugMode
# print ''
# print 'ON CPU with size=(%s):' % str(size)
x = tensor.matrix() x = tensor.matrix()
R = MRG_RandomStreams(234) R = MRG_RandomStreams(234)
# Note: we specify `nstreams` to avoid a warning. # Note: we specify `nstreams` to avoid a warning.
...@@ -276,15 +271,10 @@ def test_uniform(): ...@@ -276,15 +271,10 @@ def test_uniform():
# warning using the warning module. # warning using the warning module.
u = R.uniform(size=size, u = R.uniform(size=size,
nstreams=rng_mrg.guess_n_streams(size, warn=False)) nstreams=rng_mrg.guess_n_streams(size, warn=False))
f = theano.function(var_input, u, mode=mode) f = theano.function(var_input, u)
assert any([isinstance(node.op, theano.sandbox.rng_mrg.mrg_uniform) assert any([isinstance(node.op, theano.sandbox.rng_mrg.mrg_uniform)
for node in f.maker.fgraph.toposort()]) for node in f.maker.fgraph.toposort()])
# theano.printing.debugprint(f) f(*input)
cpu_out = f(*input)
# print 'CPU: random?[:10], random?[-10:]'
# print cpu_out[0, 0:10]
# print cpu_out[-1, -10:]
# Increase the number of steps if sizes implies only a few samples # Increase the number of steps if sizes implies only a few samples
if np.prod(const_size) < 10: if np.prod(const_size) < 10:
...@@ -293,12 +283,10 @@ def test_uniform(): ...@@ -293,12 +283,10 @@ def test_uniform():
steps_ = steps steps_ = steps
basictest(f, steps_, const_size, prefix='mrg cpu', inputs=input) basictest(f, steps_, const_size, prefix='mrg cpu', inputs=input)
# print ''
# print 'ON CPU w Numpy with size=(%s):' % str(size)
RR = theano.tensor.shared_randomstreams.RandomStreams(234) RR = theano.tensor.shared_randomstreams.RandomStreams(234)
uu = RR.uniform(size=size) uu = RR.uniform(size=size)
ff = theano.function(var_input, uu, mode=mode) ff = theano.function(var_input, uu)
# It's not our problem if numpy generates 0 or 1 # It's not our problem if numpy generates 0 or 1
basictest(ff, steps_, const_size, prefix='numpy', basictest(ff, steps_, const_size, prefix='numpy',
allow_01=True, inputs=input) allow_01=True, inputs=input)
...@@ -345,8 +333,8 @@ def test_binomial(): ...@@ -345,8 +333,8 @@ def test_binomial():
# we test size in a tuple of int and a tensor.shape. # we test size in a tuple of int and a tensor.shape.
# we test the param p with int. # we test the param p with int.
if (mode in ['DEBUG_MODE', 'DebugMode', 'FAST_COMPILE'] or if (config.mode in ['DEBUG_MODE', 'DebugMode', 'FAST_COMPILE'] or
mode == 'Mode' and config.linker in ['py']): config.mode == 'Mode' and config.linker in ['py']):
sample_size = (10, 50) sample_size = (10, 50)
steps = 50 steps = 50
rtol = 0.02 rtol = 0.02
...@@ -373,8 +361,8 @@ def test_binomial(): ...@@ -373,8 +361,8 @@ def test_binomial():
def t_binomial(mean, size, const_size, var_input, input, steps, rtol): def t_binomial(mean, size, const_size, var_input, input, steps, rtol):
R = MRG_RandomStreams(234) R = MRG_RandomStreams(234)
u = R.binomial(size=size, p=mean) u = R.binomial(size=size, p=mean)
f = theano.function(var_input, u, mode=mode) f = theano.function(var_input, u)
out = f(*input) f(*input)
# Increase the number of steps if sizes implies only a few samples # Increase the number of steps if sizes implies only a few samples
if np.prod(const_size) < 10: if np.prod(const_size) < 10:
...@@ -388,7 +376,7 @@ def t_binomial(mean, size, const_size, var_input, input, steps, rtol): ...@@ -388,7 +376,7 @@ def t_binomial(mean, size, const_size, var_input, input, steps, rtol):
RR = theano.tensor.shared_randomstreams.RandomStreams(234) RR = theano.tensor.shared_randomstreams.RandomStreams(234)
uu = RR.binomial(size=size, p=mean) uu = RR.binomial(size=size, p=mean)
ff = theano.function(var_input, uu, mode=mode) ff = theano.function(var_input, uu)
# It's not our problem if numpy generates 0 or 1 # It's not our problem if numpy generates 0 or 1
basictest(ff, steps_, const_size, prefix='numpy', allow_01=True, basictest(ff, steps_, const_size, prefix='numpy', allow_01=True,
inputs=input, target_avg=mean, mean_rtol=rtol) inputs=input, target_avg=mean, mean_rtol=rtol)
...@@ -399,8 +387,8 @@ def test_normal0(): ...@@ -399,8 +387,8 @@ def test_normal0():
steps = 50 steps = 50
std = 2. std = 2.
if (mode in ['DEBUG_MODE', 'DebugMode', 'FAST_COMPILE'] or if (config.mode in ['DEBUG_MODE', 'DebugMode', 'FAST_COMPILE'] or
mode == 'Mode' and config.linker in ['py']): config.mode == 'Mode' and config.linker in ['py']):
sample_size = (25, 30) sample_size = (25, 30)
default_rtol = .02 default_rtol = .02
else: else:
...@@ -435,17 +423,13 @@ def test_normal0(): ...@@ -435,17 +423,13 @@ def test_normal0():
((2,), (2,), [], [], -5., default_rtol, 0.02), ((2,), (2,), [], [], -5., default_rtol, 0.02),
((3,), (3,), [], [], -5., default_rtol, 0.02), ((3,), (3,), [], [], -5., default_rtol, 0.02),
]: ]:
# print ''
# print 'ON CPU:'
R = MRG_RandomStreams(234) R = MRG_RandomStreams(234)
# Note: we specify `nstreams` to avoid a warning. # Note: we specify `nstreams` to avoid a warning.
n = R.normal(size=size, avg=avg, std=std, n = R.normal(size=size, avg=avg, std=std,
nstreams=rng_mrg.guess_n_streams(size, warn=False)) nstreams=rng_mrg.guess_n_streams(size, warn=False))
f = theano.function(var_input, n, mode=mode) f = theano.function(var_input, n)
# theano.printing.debugprint(f) f(*input)
out = f(*input)
# print 'random?[:10]\n', out[0, 0:10]
# Increase the number of steps if size implies only a few samples # Increase the number of steps if size implies only a few samples
if np.prod(const_size) < 10: if np.prod(const_size) < 10:
...@@ -458,8 +442,6 @@ def test_normal0(): ...@@ -458,8 +442,6 @@ def test_normal0():
sys.stdout.flush() sys.stdout.flush()
# print ''
# print 'ON CPU w NUMPY:'
RR = theano.tensor.shared_randomstreams.RandomStreams(234) RR = theano.tensor.shared_randomstreams.RandomStreams(234)
nn = RR.normal(size=size, avg=avg, std=std) nn = RR.normal(size=size, avg=avg, std=std)
...@@ -497,42 +479,30 @@ def basic_multinomialtest(f, steps, sample_size, target_pvals, n_samples, ...@@ -497,42 +479,30 @@ def basic_multinomialtest(f, steps, sample_size, target_pvals, n_samples,
def test_multinomial(): def test_multinomial():
steps = 100 steps = 100
mode_ = mode
if mode == 'FAST_COMPILE':
mode_ = 'FAST_RUN'
if (mode in ['DEBUG_MODE', 'DebugMode', 'FAST_COMPILE'] or if (config.mode in ['DEBUG_MODE', 'DebugMode', 'FAST_COMPILE'] or
mode == 'Mode' and config.linker in ['py']): config.mode == 'Mode' and config.linker in ['py']):
sample_size = (49, 5) sample_size = (49, 5)
else: else:
sample_size = (450, 6) sample_size = (450, 6)
mode_ = theano.compile.mode.get_mode(mode_)
# print ''
# print 'ON CPU:'
pvals = np.asarray(np.random.uniform(size=sample_size)) pvals = np.asarray(np.random.uniform(size=sample_size))
pvals = np.apply_along_axis(lambda row: row / np.sum(row), 1, pvals) pvals = np.apply_along_axis(lambda row: row / np.sum(row), 1, pvals)
R = MRG_RandomStreams(234) R = MRG_RandomStreams(234)
# Note: we specify `nstreams` to avoid a warning. # Note: we specify `nstreams` to avoid a warning.
m = R.multinomial(pvals=pvals, dtype=config.floatX, nstreams=30 * 256) m = R.multinomial(pvals=pvals, dtype=config.floatX, nstreams=30 * 256)
f = theano.function([], m, mode=mode_) f = theano.function([], m)
# theano.printing.debugprint(f) f()
out = f()
basic_multinomialtest(f, steps, sample_size, pvals, n_samples=1, basic_multinomialtest(f, steps, sample_size, pvals, n_samples=1,
prefix='mrg ') prefix='mrg ')
def test_multinomial_n_samples(): def test_multinomial_n_samples():
mode_ = mode if (config.mode in ['DEBUG_MODE', 'DebugMode', 'FAST_COMPILE'] or
if mode == 'FAST_COMPILE': config.mode == 'Mode' and config.linker in ['py']):
mode_ = 'FAST_RUN'
if (mode in ['DEBUG_MODE', 'DebugMode', 'FAST_COMPILE'] or
mode == 'Mode' and config.linker in ['py']):
sample_size = (49, 5) sample_size = (49, 5)
else: else:
sample_size = (450, 6) sample_size = (450, 6)
mode_ = theano.compile.mode.get_mode(mode_)
pvals = np.asarray(np.random.uniform(size=sample_size)) pvals = np.asarray(np.random.uniform(size=sample_size))
pvals = np.apply_along_axis(lambda row: row / np.sum(row), 1, pvals) pvals = np.apply_along_axis(lambda row: row / np.sum(row), 1, pvals)
...@@ -541,7 +511,7 @@ def test_multinomial_n_samples(): ...@@ -541,7 +511,7 @@ def test_multinomial_n_samples():
for n_samples, steps in zip([5, 10, 100, 1000], [20, 10, 1, 1]): for n_samples, steps in zip([5, 10, 100, 1000], [20, 10, 1, 1]):
m = R.multinomial(pvals=pvals, n=n_samples, m = R.multinomial(pvals=pvals, n=n_samples,
dtype=config.floatX, nstreams=30 * 256) dtype=config.floatX, nstreams=30 * 256)
f = theano.function([], m, mode=mode_) f = theano.function([], m)
basic_multinomialtest(f, steps, sample_size, pvals, basic_multinomialtest(f, steps, sample_size, pvals,
n_samples, prefix='mrg ') n_samples, prefix='mrg ')
sys.stdout.flush() sys.stdout.flush()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论