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