提交 97bd98fd authored 作者: Razvan Pascanu's avatar Razvan Pascanu

new tests to scan

上级 80cb32e6
...@@ -2025,6 +2025,59 @@ class T_Scan(unittest.TestCase): ...@@ -2025,6 +2025,59 @@ class T_Scan(unittest.TestCase):
m2 = theano.tensor.matrix() m2 = theano.tensor.matrix()
conv = theano.tensor.signal.conv.conv2d(m1, m2) conv = theano.tensor.signal.conv.conv2d(m1, m2)
def test_merge(self):
x = theano.tensor.vector()
y = theano.tensor.vector()
def sum(s):
return s+1
sx, upx = theano.scan(sum, sequences = [x])
sy, upy = theano.scan(sum, sequences = [y])
f = theano.function([x, y], [sx, sy])
topo = f.maker.env.toposort()
scans = filter(lambda n: isinstance(n.op, theano.scan_module.scan_op.Scan), topo)
self.assertTrue(len(scans) == 2)
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])
topo = f.maker.env.toposort()
scans = filter(lambda n: isinstance(n.op, theano.scan_module.scan_op.Scan), topo)
self.assertTrue(len(scans) == 2)
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])
topo = f.maker.env.toposort()
scans = filter(lambda n: isinstance(n.op, theano.scan_module.scan_op.Scan), topo)
self.assertTrue(len(scans) == 1)
sx, upx = theano.scan(sum, sequences = [x])
sy, upy = theano.scan(sum, sequences = [x])
f = theano.function([x, y], [sx, sy])
topo = f.maker.env.toposort()
scans = filter(lambda n: isinstance(n.op, theano.scan_module.scan_op.Scan), topo)
self.assertTrue(len(scans) == 1)
sx, upx = theano.scan(sum, sequences = [x])
sy, upy = theano.scan(sum, sequences = [x], mode='FAST_COMPILE')
f = theano.function([x, y], [sx, sy])
topo = f.maker.env.toposort()
scans = filter(lambda n: isinstance(n.op, theano.scan_module.scan_op.Scan), topo)
self.assertTrue(len(scans) == 2)
sx, upx = theano.scan(sum, sequences = [x])
sy, upy = theano.scan(sum, sequences = [x], truncate_gradient=1)
f = theano.function([x, y], [sx, sy])
topo = f.maker.env.toposort()
scans = filter(lambda n: isinstance(n.op, theano.scan_module.scan_op.Scan), topo)
self.assertTrue(len(scans) == 2)
def test_hash(self): def test_hash(self):
x = theano.tensor.vector() x = theano.tensor.vector()
...@@ -2130,6 +2183,222 @@ class T_Scan(unittest.TestCase): ...@@ -2130,6 +2183,222 @@ class T_Scan(unittest.TestCase):
f2 = theano.function([],gx) f2 = theano.function([],gx)
assert numpy.allclose( f2(), numpy.ones((10,))) assert numpy.allclose( f2(), numpy.ones((10,)))
def test_rop(self):
seed = utt.fetch_seed()
rng = numpy.random.RandomState(seed)
floatX = theano.config.floatX
v_u = numpy.array(rng.uniform(size=(20,5)),dtype=floatX)
v_W = numpy.array(rng.uniform(size=(5,5)), dtype=floatX)
v_h0 = numpy.array(rng.uniform(size=(5,)), dtype = floatX)
v_eu = numpy.array(rng.uniform(size=(20,5)),dtype=floatX)
v_eW = numpy.array(rng.uniform(size=(5,5)), dtype=floatX)
v_eh0 = numpy.array(rng.uniform(size=(5,)), dtype = floatX)
def rnn_fn(_u,_y,_W):
sl_o = theano.tensor.tanh(
theano.tensor.dot(_W, (_u+_y)))
return sl_o
u = theano.tensor.matrix('U')
h0 = theano.tensor.vector('h0')
W = theano.tensor.matrix('W')
_u = theano.tensor.specify_shape(u , v_u.shape )
_u.name = '_U'
_h0 = theano.tensor.specify_shape(h0, v_h0.shape)
_h0.name = '_h0'
_W = theano.tensor.specify_shape(W , v_W.shape )
_W.name = '_W'
o,_ = theano.scan(rnn_fn,
sequences = _u,
outputs_info = _h0,
non_sequences = _W,
name = 'rnn_fn')
o = o [-1]
eu = theano.tensor.matrix('eu')
eh0 = theano.tensor.vector('eh0')
eW = theano.tensor.matrix('eW')
nwo_u = theano.tensor.Rop(o, _u , eu )
nwo_h0 = theano.tensor.Rop(o, _h0, eh0)
nwo_W = theano.tensor.Rop(o, _W , eW )
fn_rop = theano.function([u,h0,W,eu,eh0,eW],
[nwo_u, nwo_h0, nwo_W])
n2o_u,_ = theano.scan( lambda i, o,u,h0,W,eu:
(theano.tensor.grad(o[i], u)*eu).sum(),
sequences = tensor.arange(o.shape[0]),
non_sequences = [o,u,h0,W,eu])
n2o_h0,_ = theano.scan( lambda i, o,u,h0,W,eh0:
(theano.tensor.grad(o[i], h0)*eh0).sum(),
sequences = tensor.arange(o.shape[0]),
non_sequences = [o,u,h0,W,eh0])
n2o_W,_ = theano.scan( lambda i, o,u,h0,W,eW:
(theano.tensor.grad(o[i], W)*eW).sum(),
sequences = tensor.arange(o.shape[0]),
non_sequences = [o,u,h0,W,eW])
fn_test = theano.function([u,h0,W,eu,eh0,eW],
[n2o_u, n2o_h0, n2o_W])
vnu,vnh0,vnW = fn_rop (v_u, v_h0, v_W, v_eu, v_eh0, v_eW)
tnu,tnh0,tnW = fn_test(v_u, v_h0, v_W, v_eu, v_eh0, v_eW)
assert numpy.allclose(vnu , tnu )
assert numpy.allclose(vnh0, tnh0)
assert numpy.allclose(vnW , tnW )
def test_pushout(self):
W1 = TT.matrix('W1')
W2 = TT.matrix('W2')
h0 = TT.vector('h0')
def lambda_fn(h, W1, W2):
return TT.dot(h, W1 + W2)
o, _ = theano.scan(lambda_fn, outputs_info= h0,
non_sequences =[W1,W2],
n_steps = 5)
f = theano.function([h0,W1,W2], o)
scan_node = [x for x in f.maker.env.toposort()
if isinstance(x.op,
theano.scan_module.scan_op.Scan)][0]
assert len([x for x in scan_node.op.fn.maker.env.toposort()
if isinstance(x.op, theano.tensor.Elemwise)]) == 0
def test_alloc_inputs1(self):
W1 = TT.matrix('W1')
W2 = TT.matrix('W2')
h0 = TT.vector('h0')
def lambda_fn(h, W1, W2):
return TT.dot(h, W1 * W2)
o, _ = theano.scan(lambda_fn, outputs_info= h0,
non_sequences =[W1,TT.zeros_like(W2)],
n_steps = 5)
f = theano.function([h0,W1,W2], o)
scan_node = [x for x in f.maker.env.toposort()
if isinstance(x.op,
theano.scan_module.scan_op.Scan)][0]
assert len([x for x in scan_node.op.fn.maker.env.toposort()
if isinstance(x.op, theano.tensor.Elemwise)]) == 0
def test_alloc_inputs2(self):
W1 = TT.matrix()
W2 = TT.matrix()
h0 = TT.vector()
def lambda_fn(W1,h, W2):
return W1 * TT.dot(h, W2)
o, _ = theano.scan(lambda_fn,
sequences = TT.zeros_like(W1),
outputs_info= h0,
non_sequences =[TT.zeros_like(W2)],
n_steps = 5)
f = theano.function([h0,W1,W2], o)
scan_node = [x for x in f.maker.env.toposort()
if isinstance(x.op,
theano.scan_module.scan_op.Scan)][0]
assert len([x for x in scan_node.op.fn.maker.env.toposort()
if isinstance(x.op, theano.tensor.Elemwise)]) == 0
def test_alloc_inputs3(self):
_W1 = TT.matrix()
_W2 = TT.matrix()
_h0 = TT.vector()
W1 = TT.specify_shape(_W1, (3,3))
W2 = TT.specify_shape(_W2, (3,3))
h0 = TT.specify_shape(_h0, (3,))
def lambda_fn(W1,h, W2):
return W1 * TT.dot(h, W2)
o, _ = theano.scan(lambda_fn,
sequences = TT.zeros_like(W1),
outputs_info= h0,
non_sequences =[TT.zeros_like(W2)],
n_steps = 5)
f = theano.function([_h0,_W1,_W2], o)
scan_node = [x for x in f.maker.env.toposort()
if isinstance(x.op,
theano.scan_module.scan_op.Scan)][0]
assert len(scan_node.op.inputs) == 1
def test_while0(self):
x = TT.vector('x')
def lambda_fn(x_t):
return x_t+1, theano.until( x_t > 3)
o, _ = theano.scan(lambda_fn, x)
f = theano.function([x], o)
vx = numpy.zeros((50,))
vx[23] = 4
out = f(vx)
assert numpy.sum(out[24:]) == 0
def test_while1(self):
x = TT.vector('x')
def lambda_fn(x_t):
return x_t+1, theano.until( x_t > 3)
o, _ = theano.scan(lambda_fn, x)
o2, _ = theano.scan(lambda x_t:x_t + 2,
x)
f = theano.function([x], [o,o2])
vx = numpy.zeros((50,))
vx[23] = 4
out, out2 = f(vx)
assert numpy.sum(out[24:]) == 0
assert numpy.all(out2 == vx + 2)
lssc = [x for x in f.maker.env.toposort()
if isinstance(x.op, theano.scan_module.scan_op.Scan)]
assert len(lssc) == 2
def test_while2(self):
x = TT.vector('x')
def lambda_fn(x_t):
return x_t+1, theano.until( x_t > 3)
o, _ = theano.scan(lambda_fn, x)
o2, _ = theano.scan(lambda x_t:( x_t + 2, theano.until(x_t>3)),
x)
f = theano.function([x], [o,o2])
vx = numpy.zeros((50,))
vx[23] = 4
out, out2 = f(vx)
assert numpy.sum(out[24:]) == 0
assert numpy.sum(out2[24:]) == 0
lssc = [x for x in f.maker.env.toposort()
if isinstance(x.op, theano.scan_module.scan_op.Scan)]
assert len(lssc) == 1
def test_speed(): def test_speed():
# #
# This function prints out the speed of very simple recurrent # This function prints out the speed of very simple recurrent
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论