提交 7a021c47 authored 作者: lamblin's avatar lamblin

Merge pull request #900 from nouiz/small

Small
......@@ -308,6 +308,8 @@ type this:
THEANO_FLAGS='mode=FAST_RUN' nosetests
THEANO_FLAGS='mode=DEBUG_MODE' nosetests
.. _random_value_in_tests:
Using Random Values in Test Cases
---------------------------------
......
......@@ -350,24 +350,16 @@ Here is some code that allow to make the op Optional:
Random number in tests
----------------------
Making test errors more reproducable is a good practice. To make your
Making tests errors more reproducable is a good practice. To make your
tests more reproducable, you need a way to get the same random
number. You can do this by seeding NumPy's randon number
generator. There is the Theano flag unittest.rseed that specify the
seed that should be used to init random number generators. There is 2
ways to do this it numpy, here is one:
generator.
.. code-block:: python
# You can set NumPy's internal random number generator state with
numpy.random.seed(utt.fetch_seed())
# All following call to numpy.random.*() function will get affected.
# Or you can create a new RandomState separate from the others
rng = numpy.random.RandomState(utt.fetch_seed())
# You can call all numpy's random number generator function's on rng
rng.rand(5, 5)
For conveniance, the classes InferShapeTester and RopLop_checker
already do this for you. If you implement your own ``setUp`` function,
don't forget to call the parent ``setUp`` function.
For more details see :ref:`random_value_in_tests`.
GPU Op
------
......
......@@ -86,6 +86,10 @@ def run_mercurial_command(hg_command):
hg_executable = os.environ.get("HG", "hg")
hg_command_tuple = hg_command.split()
hg_command_tuple.insert(0, hg_executable)
#If you install your own mercurial version in your home
#hg_executable don't always have execution permission.
if not os.access(hg_executable, os.X_OK):
hg_command_tuple.insert(0, sys.executable)
try:
hg_subprocess = Popen(hg_command_tuple, stdout=PIPE, stderr=PIPE)
except OSError, e:
......
......@@ -15,6 +15,7 @@ from theano import tensor
from theano.compile.pfunc import rebuild_collect_shared
from theano.gof.python25 import any
from theano.tests import unittest_tools as utt
import theano.scalar.sharedvar
from numpy.testing.noseclasses import KnownFailureTest
......@@ -1121,7 +1122,6 @@ class T_Scan(unittest.TestCase):
v_vsample = numpy.array(rng.binomial(1, .5, size=(3, 20),),
dtype='float32')
vsample = theano.shared(v_vsample)
import theano.sandbox.rng_mrg
trng = theano.sandbox.rng_mrg.MRG_RandomStreams(
utt.fetch_seed())
......@@ -3217,7 +3217,6 @@ def test_speed_rnn():
# The computation being tested here is a repeated tanh of a matrix-vector
# multiplication - the heart of an ESN or RNN.
#
import theano.scalar.sharedvar
#We need the CVM for this speed test
if not theano.config.cxx:
......@@ -3297,7 +3296,6 @@ def test_speed_batchrnn():
# The computation being tested here is a repeated tanh of a matrix-vector
# multiplication - the heart of an ESN or RNN.
#
import theano.scalar.sharedvar
#We need the CVM for this speed test
if not theano.config.cxx:
......
......@@ -1397,7 +1397,7 @@ _good_broadcast_unary_gammaln = dict(
normal=(rand_ranged(-1 + 1e-2, 10, (2, 3)),),
empty=(numpy.asarray([]),),)
_grad_broadcast_unary_gammaln = dict(
# smaler range as our grad method don't estimate it good enough.
# smaller range as our grad method don't estimate it good enough.
normal=(rand_ranged(1e-8, 8, (2, 3)),),)
GammaTester = makeBroadcastTester(
......
......@@ -2,7 +2,8 @@ import numpy
import unittest
from theano.tests import unittest_tools as utt
import theano
import theano.tensor as T
import theano.tensor as tt
class Test_inc_subtensor(unittest.TestCase):
"""Partial testing.
......@@ -14,8 +15,10 @@ class Test_inc_subtensor(unittest.TestCase):
- indices: scalar vs slice, constant vs variable, out of bound, ...
- inplace
NOTE: these are the same tests as test_incsubtensor.py, but using the new (read: not
deprecated) inc_subtensor, set_subtensor functions.
NOTE: these are the same tests as test_incsubtensor.py, but using
the new (read: not deprecated) inc_subtensor, set_subtensor
functions.
"""
def setUp(self):
utt.seed_rng()
......@@ -24,22 +27,22 @@ class Test_inc_subtensor(unittest.TestCase):
"""Increments or sets part of a tensor by a scalar using full slice and
a partial slice depending on a scalar.
"""
a = T.dmatrix()
increment = T.dscalar()
a = tt.dmatrix()
increment = tt.dscalar()
sl1 = slice(None)
sl2_end = T.lscalar()
sl2_end = tt.lscalar()
sl2 = slice(sl2_end)
for do_set in [False,True]:
for do_set in [False, True]:
if do_set:
resut = T.set_subtensor(a[sl1, sl2], increment)
resut = tt.set_subtensor(a[sl1, sl2], increment)
else:
resut = T.inc_subtensor(a[sl1, sl2], increment)
resut = tt.inc_subtensor(a[sl1, sl2], increment)
f = theano.function([a, increment, sl2_end], resut)
val_a = numpy.ones((5,5))
val_a = numpy.ones((5, 5))
val_inc = 2.3
val_sl2_end = 2
......@@ -47,9 +50,9 @@ class Test_inc_subtensor(unittest.TestCase):
expected_result = numpy.copy(val_a)
if do_set:
expected_result[:,:val_sl2_end] = val_inc
expected_result[:, :val_sl2_end] = val_inc
else:
expected_result[:,:val_sl2_end] += val_inc
expected_result[:, :val_sl2_end] += val_inc
self.assertTrue(numpy.array_equal(result, expected_result))
......@@ -57,24 +60,24 @@ class Test_inc_subtensor(unittest.TestCase):
"""Increments or sets part of a tensor by a scalar using full slice and
a partial slice depending on a scalar.
"""
a = T.dtensor3()
increment = T.dscalar()
a = tt.dtensor3()
increment = tt.dscalar()
sl1 = slice(None)
sl2_end = T.lscalar()
sl2_end = tt.lscalar()
sl2 = slice(sl2_end)
sl3 = 2
for do_set in [True,False]:
for do_set in [True, False]:
print "Set", do_set
if do_set:
resut = T.set_subtensor(a[sl1, sl3, sl2], increment)
resut = tt.set_subtensor(a[sl1, sl3, sl2], increment)
else:
resut = T.inc_subtensor(a[sl1, sl3, sl2], increment)
resut = tt.inc_subtensor(a[sl1, sl3, sl2], increment)
f = theano.function([a, increment, sl2_end], resut)
val_a = numpy.ones((5,3,4))
val_a = numpy.ones((5, 3, 4))
val_inc = 2.3
val_sl2_end = 2
......@@ -82,39 +85,39 @@ class Test_inc_subtensor(unittest.TestCase):
result = f(val_a, val_inc, val_sl2_end)
if do_set:
expected_result[:,sl3,:val_sl2_end] = val_inc
expected_result[:, sl3, :val_sl2_end] = val_inc
else:
expected_result[:,sl3,:val_sl2_end] += val_inc
expected_result[:, sl3, :val_sl2_end] += val_inc
self.assertTrue(numpy.array_equal(result, expected_result))
def test_grad_inc_set(self):
def inc_slice(*s):
def just_numeric_args(a,b):
return T.inc_subtensor(a[s], b)
def just_numeric_args(a, b):
return tt.inc_subtensor(a[s], b)
return just_numeric_args
def set_slice(*s):
def just_numeric_args(a,b):
return T.set_subtensor(a[s], b)
def just_numeric_args(a, b):
return tt.set_subtensor(a[s], b)
return just_numeric_args
for f_slice in [inc_slice, set_slice]:
# vector
utt.verify_grad(
f_slice(slice(2,4,None)),
(numpy.asarray([0,1,2,3,4,5.]),
numpy.asarray([9,9.]),))
f_slice(slice(2, 4, None)),
(numpy.asarray([0, 1, 2, 3, 4, 5.]),
numpy.asarray([9, 9.]), ))
# matrix
utt.verify_grad(
f_slice(slice(1,2,None), slice(None, None, None)),
(numpy.asarray([[0,1],[2,3],[4,5.]]),
numpy.asarray([[9,9.]]),))
f_slice(slice(1, 2, None), slice(None, None, None)),
(numpy.asarray([[0, 1], [2, 3], [4, 5.]]),
numpy.asarray([[9, 9.]]), ))
#single element
utt.verify_grad(
f_slice(2, 1),
(numpy.asarray([[0,1],[2,3],[4,5.]]),
(numpy.asarray([[0, 1], [2, 3], [4, 5.]]),
numpy.asarray(9.),))
......@@ -3781,4 +3781,3 @@ if __name__ == '__main__':
# unittest.main()
test_fusion().tes_memory_leak()
"""
......@@ -58,6 +58,7 @@ class RopLop_checker(unittest.TestCase):
Rop to class that inherit from it."""
def setUp(self):
utt.seed_rng()
# Using vectors make things a lot simpler for generating the same
# computations using scan
self.x = tensor.vector('x')
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论