* All theano tests should now have a deterministic seed

* Seeding should be done through unittest_tools.set_seed ** seeds with THEANO_UNITTEST_SEED env. var if it is present ** if env. var is not present, it will use the seed provided by the user ** if user provided no seed, it will seed with None (random seed) * when creating RandomState objects, the parameter should be the return value of fetch_seed() which will try to get the env. var. seed, if not default to the user value or None. * Fixed 2 tests in test_basic which had been hardcoded for a specific seed
上级 64146cea
......@@ -5,6 +5,7 @@ from theano.gof import OpSub, TopoOptimizer
from pylearn.algorithms.minimizer import make_minimizer # minimizer
from theano.printing import Print
from theano.tests import unittest_tools
#import sgd #until Olivier's module-import thing works better
####################
......@@ -177,7 +178,7 @@ class ExampleRNN(Module):
def _instance_initialize(self, obj):
n_vis = self.n_vis
rng = N.random.RandomState(2342)
rng = N.random.RandomState(unittest_tools.fetch_seed(2342))
obj.z0 = N.zeros(n_vis)
obj.w = rng.randn(n_vis, n_vis) * 0.01
......@@ -193,7 +194,7 @@ def test_example_rnn():
rnn = rnn_module.make(mode='FAST_RUN')
rng = N.random.RandomState(7722342)
rng = N.random.RandomState(unittest_tools.fetch_seed(7722342))
x = rng.randn(10,n_vis)
y = rng.randn(10,n_out)
......@@ -215,7 +216,7 @@ def test_example_rnn():
def test_WEIRD_STUFF():
n_vis = 3
rng = N.random.RandomState(7722342)
rng = N.random.RandomState(unittest_tools.fetch_seed(7722342))
x = rng.randn(10,n_vis)
y = rng.randn(10,n_vis)
......
......@@ -10,6 +10,7 @@ from theano import gof
from theano.sparse.basic import _is_dense, _is_sparse, _is_dense_result, _is_sparse_result
from theano.sparse.basic import _mtypes, _mtype_to_str
from theano.tests import unittest_tools
def eval_outputs(outputs):
......@@ -17,7 +18,8 @@ def eval_outputs(outputs):
class T_transpose(unittest.TestCase):
def setUp(self):
numpy.random.seed(44)
unittest_tools.seed_rng()
def test_transpose_csc(self):
sp = sparse.csc_matrix(sparse.eye(5,3))
a = as_sparse(sp)
......@@ -123,7 +125,7 @@ class T_Add(unittest.TestCase):
class T_conversion(unittest.TestCase):
def setUp(self):
numpy.random.seed(44)
unittest_tools.seed_rng()
def test0(self):
a = tensor.as_tensor(numpy.random.rand(5))
......
......@@ -6,6 +6,7 @@ from functools import partial
from collections import deque
import numpy
from copy import copy
from ...compile import (SymbolicInputKit, SymbolicInput,
Module, module, Method, Member, In, Component)
......@@ -32,7 +33,7 @@ class KitComponent(Component):
r = input.result
if r not in memo:
input = copy(input)
input.value = gof.Container(r, storage = [None])
input.value = Container(r, storage = [None])
memo[r] = input
def build(self, mode, memo):
......
......@@ -16,7 +16,10 @@ from theano import gof
from theano.tensor.elemwise import DimShuffle
from theano.compile.mode import default_mode
from theano import function
from theano.tests import unittest_tools
### seed random number generator so that unittests are deterministic ###
unittest_tools.seed_rng()
def inplace_func(inputs, outputs, mode=default_mode):
return function(inputs, outputs, mode=mode, accept_inplace=True)
......@@ -604,7 +607,7 @@ class T_Cast(unittest.TestCase):
class T_max_and_argmax(unittest.TestCase):
def setUp(self):
numpy.random.seed(123784)
unittest_tools.seed_rng()
MaxAndArgmax.debug = 0
def test0(self):
......@@ -620,13 +623,17 @@ class T_max_and_argmax(unittest.TestCase):
self.failUnless(i == 2)
def test2(self):
n = as_tensor(numpy.random.rand(2,3))
data = numpy.random.rand(2,3)
n = as_tensor(data)
v,i = eval_outputs(max_and_argmax(n))
self.failUnless(numpy.all(i == [0,1]))
self.failUnless(numpy.all(v == numpy.max(data,-1)))
self.failUnless(numpy.all(i == numpy.argmax(data,-1)))
def test2b(self):
n = as_tensor(numpy.random.rand(2,3))
data = numpy.random.rand(2,3)
n = as_tensor(data)
v,i = eval_outputs(max_and_argmax(n,0))
self.failUnless(numpy.all(i == [0,1,1]))
self.failUnless(numpy.all(v == numpy.max(data,0)))
self.failUnless(numpy.all(i == numpy.argmax(data,0)))
def test2_invalid(self):
n = as_tensor(numpy.random.rand(2,3))
try:
......@@ -663,7 +670,7 @@ class T_max_and_argmax(unittest.TestCase):
class T_subtensor(unittest.TestCase):
def setUp(self):
Subtensor.debug = False
numpy.random.seed(12353123)
unittest_tools.seed_rng()
def test0_err_invalid(self):
#it is impossible to retrieve a view of a 0-d tensor
......@@ -1063,6 +1070,9 @@ class test_bitwise(unittest.TestCase):
class T_add(unittest.TestCase):
def setUp(self):
unittest_tools.seed_rng()
def test_complex_all_ops(self):
for nbits in (64, 128):
a = value(numpy.ones(3, dtype='complex%i' % nbits)+0.5j)
......@@ -1158,7 +1168,7 @@ class T_exp(unittest.TestCase):
# class T_mul(unittest.TestCase):
# def setUp(self):
# numpy.random.seed([1,2,3,4])
# unittest_tools.seed_rng()
# def test_elemwise(self):
# a = as_tensor(0.0)
......@@ -1221,7 +1231,7 @@ class T_exp(unittest.TestCase):
# class T_div(unittest.TestCase):
# def setUp(self):
# numpy.random.seed(9999)
# unittest_tools.seed_rng()
# def test_grad_e(self):
# verify_grad(self, Div, [numpy.random.rand(3), numpy.ones(3)])
# verify_grad(self, Div, [numpy.random.rand(3,5), numpy.random.rand(3,5)+0.1])
......@@ -1233,10 +1243,14 @@ class T_exp(unittest.TestCase):
# verify_grad(self, Div, [numpy.random.rand(3,5), numpy.random.rand(1,1)])
# class T_log2(unittest.TestCase):
# def setUp(self):
# unittest_tools.seed_rng()
# def test0(self):
# verify_grad(self, Log2, [numpy.random.rand(3,1)+0.0001])
# class T_log(unittest.TestCase):
# def setUp(self):
# unittest_tools.seed_rng()
# def test0(self):
# verify_grad(self, Log, [numpy.random.rand(3,1)+0.0001])
# def test1(self):
......@@ -1248,7 +1262,7 @@ class T_exp(unittest.TestCase):
# class T_pow(unittest.TestCase):
# def setUp(self):
# numpy.random.seed(9999)
# unittest_tools.seed_rng()
# def test_elemwise(self):
# verify_grad(self, Div, [numpy.random.rand(3,4), numpy.random.rand(3,4)+0.1])
# verify_grad(self, Pow, [numpy.random.rand(3,4), numpy.random.rand(3,4)])
......@@ -1264,7 +1278,7 @@ class T_exp(unittest.TestCase):
class test_matinv(unittest.TestCase):
def setUp(self):
numpy.random.seed(1)
unittest_tools.seed_rng()
def mat_reciprocal(self,dim):
# symbolic program
......@@ -1303,8 +1317,7 @@ class test_matinv(unittest.TestCase):
class t_dot(unittest.TestCase):
def setUp(self):
numpy.random.seed(44)
unittest_tools.seed_rng()
@staticmethod
def rand(*args):
return numpy.random.rand(*args)
......@@ -1409,6 +1422,8 @@ class T_tensorfromscalar(unittest.TestCase):
# class T_tensor(unittest.TestCase):
# def setUp(self):
# unittest_tools.seed_rng()
# def test0(self): # allocate from a scalar float
# t = _tensor(1.0)
# self.failUnless(isinstance(t, Tensor))
......@@ -1578,7 +1593,8 @@ class test_grad(unittest.TestCase):
self.failUnless(g2.data == 0)
class T_op_cache(unittest.TestCase):
def setUp(self):
unittest_tools.seed_rng()
def test0(self):
"""trigger bug in ticket #162"""
lr = constant(0.011)
......@@ -1707,6 +1723,8 @@ def test_tile():
class test_tensordot(unittest.TestCase):
def setUp(self):
unittest_tools.seed_rng()
def test0(self):
......
......@@ -5,6 +5,7 @@ import numpy
from theano.tensor.blas import *
from theano.tensor.blas import _dot22, res_is_a
from unittest import TestCase
from theano.tests import unittest_tools
from copy import copy
_as_scalar = GemmLocalOptimizer._as_scalar
......@@ -17,7 +18,7 @@ from .test_basic import (_approx_eq, as_tensor, inplace_func,
class t_gemm(TestCase):
"""This test suite is supposed to establish that gemm works as it is supposed to."""
def setUp(self):
numpy.random.seed(44)
unittest_tools.seed_rng()
_approx_eq.debug = 0
Gemm.debug = False
......@@ -265,9 +266,9 @@ def just_gemm(i, o, ishapes = [(4,3), (3,5), (4,5), (), ()]):
for node in g.maker.env.nodes:
if node.op == gemm: raise Exception('gemm in original graph')
rng = numpy.random.RandomState(234)
rng = numpy.random.RandomState(unittest_tools.fetch_seed(234))
r0 = f(*[rng.randn(*sh) for sh in ishapes])
rng = numpy.random.RandomState(234)
rng = numpy.random.RandomState(unittest_tools.fetch_seed(234))
r1 = g(*[rng.randn(*sh) for sh in ishapes])
max_abs_err = numpy.max(numpy.abs(r0[0] - r1[0]))
if max_abs_err > 1.0e-8:
......@@ -329,9 +330,9 @@ def test_gemm_opt_double_gemm():
#for node in g.maker.env.nodes:
# if node.op == gemm: raise Failure('gemm in graph')
rng = numpy.random.RandomState(234)
rng = numpy.random.RandomState(unittest_tools.fetch_seed(234))
r0 = f(*[rng.randn(*sh) for sh in ishapes])
rng = numpy.random.RandomState(234)
rng = numpy.random.RandomState(unittest_tools.fetch_seed(234))
r1 = g(*[rng.randn(*sh) for sh in ishapes])
max_abs_err = numpy.max(numpy.abs(r0[0] - r1[0]))
if max_abs_err > 1.0e-8:
......
......@@ -9,6 +9,7 @@ from theano.scalar import *
from theano import tensor
from theano.tensor.elemwise import *
from theano.tests import unittest_tools
def Env(i, o):
......@@ -36,6 +37,8 @@ class test_DimShuffle(unittest.TestCase):
class test_Broadcast(unittest.TestCase):
def setUp(self):
unittest_tools.seed_rng()
def with_linker(self, linker):
for xsh, ysh in [((3, 5), (3, 5)),
......@@ -120,6 +123,8 @@ class test_Broadcast(unittest.TestCase):
class test_CAReduce(unittest.TestCase):
def setUp(self):
unittest_tools.seed_rng()
def with_linker(self, linker):
for xsh, tosum in [((5, 6), None),
......
......@@ -9,7 +9,7 @@ from theano.compile.mode import default_mode
from theano import tensor as T, sparse as S
import numpy as N
import sys
from theano.tests import unittest_tools
def cross_entropy(target, output, axis=1):
"""
......@@ -175,11 +175,9 @@ class QuadraticDenoisingAA(module.Module):
if (input_size is None) ^ (hidden_size is None):
raise ValueError("Must specify input_size and hidden_size or neither.")
super(QuadraticDenoisingAA, self)._instance_initialize(obj, {})
obj.random.initialize()
if seed is not None:
R = N.random.RandomState(seed)
else:
R = N.random
R = N.random.RandomState(unittest_tools.fetch_seed(seed))
if input_size is not None:
sz = (input_size, hidden_size)
inf = 1/N.sqrt(input_size)
......@@ -210,6 +208,8 @@ class SigmoidXEQuadraticDenoisingAA(QuadraticDenoisingAA):
@todo: Merge this into the above.
@todo: Default noise level for all daa levels
"""
def setUp(self):
unittest_tools.seed_rng()
def build_corrupted_input(self):
self.noise_level = theano.Member(T.scalar())
......@@ -312,10 +312,7 @@ class ConvolutionalMLPInstance(module.FancyModuleInstance, Loss01):
# ASK JAMES: Is the following necessary?
# super(ConvolutionalMLPInstance, self)._instance_initialize(obj, **kwargs)
if seed is not None:
R = N.random.RandomState(seed)
else:
R = N.random
R = N.random.RandomState(unittest_tools.fetch_seed(seed))
self.input_size = input_size
self.input_representation_size = input_representation_size
......@@ -512,7 +509,7 @@ def test_naacl_model(iters_per_unsup=10, iters_per_sup=10,
print "PROGRAM LEN %i HASH %i"% (len(m.pretraining_update.maker.env.nodes), reduce(lambda a, b: hash(a) ^ hash(b),prog_str))
rng = N.random.RandomState(23904)
rng = N.random.RandomState(unittest_tools.fetch_seed(23904))
inputs = [rng.rand(10,9) for i in 1,2,3]
targets = N.asarray([0,3,4,2,3,4,4,2,1,0])
......
......@@ -5,13 +5,14 @@ from theano import tensor as T
from theano import gof
import test_basic as TT
import numpy
from theano.tests import unittest_tools
from theano.tensor.nnet import *
class T_sigmoid(unittest.TestCase):
def setUp(self):
numpy.random.seed(9999)
unittest_tools.seed_rng()
def test_elemwise(self):
print dir(self)
assert 0
......@@ -19,13 +20,13 @@ class T_sigmoid(unittest.TestCase):
class T_softplus(unittest.TestCase):
def setUp(self):
numpy.random.seed(9999)
unittest_tools.seed_rng()
def test_elemwise(self):
TT.verify_grad(self, softplus, [numpy.random.rand(3,4)])
class T_Softmax(unittest.TestCase):
def setUp(self):
numpy.random.seed(9999)
unittest_tools.seed_rng()
def test0(self):
def f(a):
return softmax(a)[:,0]
......@@ -46,7 +47,7 @@ class T_Softmax(unittest.TestCase):
class T_SoftmaxWithBias(unittest.TestCase):
def setUp(self):
numpy.random.seed(9999)
unittest_tools.seed_rng()
def test0(self):
def f(a, b):
return softmax_with_bias(a, b)[:,0]
......@@ -70,7 +71,7 @@ class T_SoftmaxWithBias(unittest.TestCase):
class T_CrossentropySoftmax1Hot(unittest.TestCase):
def setUp(self):
numpy.random.seed(9999)
unittest_tools.seed_rng()
def test0(self):
y_idx = [0,1,3]
def f(a, b):
......@@ -84,6 +85,8 @@ class T_CrossentropySoftmax1Hot(unittest.TestCase):
TT.verify_grad(self, f, [numpy.random.rand(3,4)])
class T_prepend(unittest.TestCase):
def setUp(self):
unittest_tools.seed_rng()
def test0(self):
"""basic functionality"""
x=tensor.matrix('x')
......@@ -109,7 +112,7 @@ class T_prepend(unittest.TestCase):
class T_solve(unittest.TestCase):
def setUp(self):
self.rng = numpy.random.RandomState(666)
self.rng = numpy.random.RandomState(unittest_tools.fetch_seed(666))
def test0(self):
A=self.rng.randn(5,5)
......
......@@ -2,6 +2,7 @@ __docformat__ = "restructuredtext en"
import sys
import unittest
import numpy as N
from theano.tests import unittest_tools
from theano.tensor.raw_random import *
......
......@@ -8,8 +8,12 @@ import test_basic as TT
import random
import numpy.random
from theano.tests import unittest_tools
class T_XlogX(unittest.TestCase):
def setUp(self):
unittest_tools.seed_rng()
def test0(self):
x = as_tensor([1, 0])
y = xlogx(x)
......
import unittest
import numpy
import os, sys
def fetch_seed(pseed=None):
seed = os.getenv("THEANO_UNITTEST_SEED", pseed)
try:
seed = int(seed) if seed else None
except ValueError:
print >> sys.stderr, 'Error: THEANO_UNITTEST_SEED contains '\
'invalid seed, using None instead'
seed = None
return seed
def seed_rng(pseed=None):
seed = fetch_seed(pseed)
if pseed and pseed!=seed:
print >> sys.stderr, 'Warning: using seed given by THEANO_UNITTEST_SEED=%i'\
'instead of seed %i given as parameter' % (seed, pseed)
numpy.random.seed(seed)
return seed
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论