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