提交 5a78fd3c authored 作者: Frederic Bastien's avatar Frederic Bastien

Small test refactorization to remove duplicate code about the mode.

上级 2ea62d98
......@@ -24,7 +24,11 @@ from numpy.testing.noseclasses import KnownFailureTest
* There is some of scan functionality that is not well documented
'''
if theano.config.mode == 'FAST_COMPILE':
mode_with_opt = theano.compile.mode.get_mode('FAST_RUN')
else:
mode_with_opt = theano.compile.mode.get_default_mode()
mode_with_gpu = mode_with_opt.including('gpu','scan')
class multiple_outputs_numeric_grad:
"""WRITEME"""
type_eps = {'float64': 1e-7,
......@@ -255,14 +259,9 @@ class T_Scan(unittest.TestCase):
W_in = theano.tensor.fscalar('win')
W = theano.tensor.fscalar('w')
if theano.config.mode == 'FAST_COMPILE':
mode = theano.compile.mode.get_mode('FAST_RUN')
else:
mode = theano.compile.mode.get_default_mode()
mode = mode.including('gpu','scan')
# The following line is needed to have the first case being used
# Otherwise, it is the second that is tested.
mode = mode.excluding('InputToGpuOptimizer')
mode = mode_with_gpu.excluding('InputToGpuOptimizer')
output, updates = theano.scan(f_rnn, u,x0,[W_in,W]
, n_steps = None
, truncate_gradient = -1
......@@ -328,20 +327,15 @@ class T_Scan(unittest.TestCase):
x0 = theano.tensor.fscalar('x0')
W_in = theano.tensor.fscalar('win')
W = theano.tensor.fscalar('w')
if theano.config.mode == 'FAST_COMPILE':
mode = theano.compile.mode.get_mode('FAST_RUN')
else:
mode = theano.compile.mode.get_default_mode()
mode = mode.including('gpu','scan')
output, updates = theano.scan(f_rnn, u,x0,[W_in,W]
, n_steps = None
, truncate_gradient = -1
, go_backwards = False
, mode = mode)
, mode = mode_with_gpu)
f2 = theano.function([u,x0,W_in,W], output, updates = updates,
allow_input_downcast = True,
mode = mode)
mode = mode_with_gpu)
# get random initial values
rng = numpy.random.RandomState(utt.fetch_seed())
......@@ -914,13 +908,6 @@ class T_Scan(unittest.TestCase):
if cuda.cuda_available == False:
raise SkipTest('Optional package cuda disabled')
if theano.config.mode == 'FAST_COMPILE':
mode = theano.compile.mode.get_mode('FAST_RUN')
else:
mode = theano.compile.mode.get_default_mode()
mode = mode.including('gpu','scan')
rng = numpy.random.RandomState(utt.fetch_seed())
v_vsample = numpy.array(rng.binomial(1,0.5, size=(3,20), )
, dtype = 'float32')
......@@ -936,12 +923,12 @@ class T_Scan(unittest.TestCase):
, n_steps = 10
, truncate_gradient=-1
, go_backwards = False
, mode = mode
, mode = mode_with_gpu
)
my_f = theano.function([], theano_vsamples[-1]
, updates = updates
, allow_input_downcast = True
, mode = mode
, mode = mode_with_gpu
)
# I leave this to tested by debugmode, this test was anyway more of
......@@ -1963,10 +1950,6 @@ class T_Scan(unittest.TestCase):
dtype = theano.config.floatX),
m + trng.uniform(size=[3])]
if theano.config.mode == 'FAST_COMPILE':
mode = theano.compile.mode.get_mode('FAST_RUN')
else:
mode = theano.compile.mode.get_default_mode()
[o1,o2], updates = theano.scan( lm,
sequences = x,
n_steps = None,
......@@ -1975,7 +1958,7 @@ class T_Scan(unittest.TestCase):
go_backwards = False)
go1 = theano.tensor.grad(o1.mean(), wrt = x)
f = theano.function([x],go1, updates = updates,
allow_input_downcast = True, mode= mode)
allow_input_downcast = True, mode= mode_with_opt)
self.assertTrue(numpy.allclose(f([1,2,3]), 2./3))
#theano.printing.debugprint(f, print_type=True)
......@@ -2001,14 +1984,10 @@ class T_Scan(unittest.TestCase):
def sum(s):
return s+1
if theano.config.mode == 'FAST_COMPILE':
mode = theano.compile.mode.get_mode('FAST_RUN')
else:
mode = theano.compile.mode.get_default_mode()
sx, upx = theano.scan(sum, sequences = [x])
sy, upy = theano.scan(sum, sequences = [y])
f = theano.function([x, y], [sx, sy], mode = mode)
f = theano.function([x, y], [sx, sy], mode = mode_with_opt)
topo = f.maker.env.toposort()
scans = filter(lambda n: isinstance(n.op, theano.scan_module.scan_op.Scan), topo)
self.assertTrue(len(scans) == 2)
......@@ -2016,7 +1995,7 @@ class T_Scan(unittest.TestCase):
sx, upx = theano.scan(sum, sequences = [x], n_steps=2)
sy, upy = theano.scan(sum, sequences = [y], n_steps=3)
f = theano.function([x, y], [sx, sy], mode= mode)
f = theano.function([x, y], [sx, sy], mode= mode_with_opt)
topo = f.maker.env.toposort()
scans = filter(lambda n: isinstance(n.op, theano.scan_module.scan_op.Scan), topo)
self.assertTrue(len(scans) == 2)
......@@ -2024,7 +2003,7 @@ class T_Scan(unittest.TestCase):
sx, upx = theano.scan(sum, sequences = [x], n_steps=4)
sy, upy = theano.scan(sum, sequences = [y], n_steps=4)
f = theano.function([x, y], [sx, sy], mode = mode)
f = theano.function([x, y], [sx, sy], mode = mode_with_opt)
topo = f.maker.env.toposort()
scans = filter(lambda n: isinstance(n.op, theano.scan_module.scan_op.Scan), topo)
self.assertTrue(len(scans) == 1)
......@@ -2032,7 +2011,7 @@ class T_Scan(unittest.TestCase):
sx, upx = theano.scan(sum, sequences = [x])
sy, upy = theano.scan(sum, sequences = [x])
f = theano.function([x, y], [sx, sy], mode= mode)
f = theano.function([x, y], [sx, sy], mode= mode_with_opt)
topo = f.maker.env.toposort()
scans = filter(lambda n: isinstance(n.op, theano.scan_module.scan_op.Scan), topo)
self.assertTrue(len(scans) == 1)
......@@ -2040,7 +2019,7 @@ class T_Scan(unittest.TestCase):
sx, upx = theano.scan(sum, sequences = [x])
sy, upy = theano.scan(sum, sequences = [x], mode='FAST_COMPILE')
f = theano.function([x, y], [sx, sy], mode= mode)
f = theano.function([x, y], [sx, sy], mode= mode_with_opt)
topo = f.maker.env.toposort()
scans = filter(lambda n: isinstance(n.op, theano.scan_module.scan_op.Scan), topo)
self.assertTrue(len(scans) == 2)
......@@ -2048,7 +2027,7 @@ class T_Scan(unittest.TestCase):
sx, upx = theano.scan(sum, sequences = [x])
sy, upy = theano.scan(sum, sequences = [x], truncate_gradient=1)
f = theano.function([x, y], [sx, sy], mode = mode)
f = theano.function([x, y], [sx, sy], mode = mode_with_opt)
topo = f.maker.env.toposort()
scans = filter(lambda n: isinstance(n.op, theano.scan_module.scan_op.Scan), topo)
self.assertTrue(len(scans) == 2)
......@@ -2241,10 +2220,6 @@ class T_Scan(unittest.TestCase):
W2 = tensor.matrix('W2')
h0 = tensor.vector('h0')
if theano.config.mode == 'FAST_COMPILE':
mode = theano.compile.mode.get_mode('FAST_RUN')
else:
mode = theano.compile.mode.get_default_mode()
def lambda_fn(h, W1, W2):
return tensor.dot(h, W1 + W2)
......@@ -2252,7 +2227,7 @@ class T_Scan(unittest.TestCase):
non_sequences =[W1,W2],
n_steps = 5)
f = theano.function([h0,W1,W2], o, mode= mode)
f = theano.function([h0,W1,W2], o, mode= mode_with_opt)
scan_node = [x for x in f.maker.env.toposort()
if isinstance(x.op,
......@@ -2273,11 +2248,7 @@ class T_Scan(unittest.TestCase):
non_sequences =[W1,tensor.zeros_like(W2)],
n_steps = 5)
if theano.config.mode == 'FAST_COMPILE':
mode = theano.compile.mode.get_mode('FAST_RUN')
else:
mode = theano.compile.mode.get_default_mode()
f = theano.function([h0,W1,W2], o, mode = mode)
f = theano.function([h0,W1,W2], o, mode = mode_with_opt)
scan_node = [x for x in f.maker.env.toposort()
if isinstance(x.op,
theano.scan_module.scan_op.Scan)][0]
......@@ -2302,11 +2273,7 @@ class T_Scan(unittest.TestCase):
non_sequences =[tensor.zeros_like(W2)],
n_steps = 5)
if theano.config.mode == 'FAST_COMPILE':
mode = theano.compile.mode.get_mode('FAST_RUN')
else:
mode = theano.compile.mode.get_default_mode()
f = theano.function([h0,W1,W2], o, mode = mode)
f = theano.function([h0,W1,W2], o, mode = mode_with_opt)
scan_node = [x for x in f.maker.env.toposort()
if isinstance(x.op,
theano.scan_module.scan_op.Scan)][0]
......@@ -2334,11 +2301,7 @@ class T_Scan(unittest.TestCase):
non_sequences =[tensor.zeros_like(W2)],
n_steps = 5)
if theano.config.mode == 'FAST_COMPILE':
mode = theano.compile.mode.get_mode('FAST_RUN')
else:
mode = theano.compile.mode.get_default_mode()
f = theano.function([_h0,_W1,_W2], o, mode = mode)
f = theano.function([_h0,_W1,_W2], o, mode = mode_with_opt)
scan_node = [x for x in f.maker.env.toposort()
if isinstance(x.op,
theano.scan_module.scan_op.Scan)][0]
......@@ -2384,11 +2347,7 @@ class T_Scan(unittest.TestCase):
o2, _ = theano.scan(lambda x_t:( x_t + 2, theano.scan_module.until(x_t>3)),
x)
if theano.config.mode == 'FAST_COMPILE':
mode = theano.compile.mode.get_mode('FAST_RUN')
else:
mode = theano.compile.mode.get_default_mode()
f = theano.function([x], [o,o2], mode=mode)
f = theano.function([x], [o,o2], mode=mode_with_opt)
vx = numpy.zeros((50,), dtype = theano.config.floatX)
vx[23] = 4
out, out2 = f(vx)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论