提交 006f1de2 authored 作者: abergeron's avatar abergeron

Merge pull request #2947 from carriepl/scan_sequence_taps

Fix sequence cutting in Scan
...@@ -500,20 +500,18 @@ def scan(fn, ...@@ -500,20 +500,18 @@ def scan(fn,
nw_slice.name = nw_name nw_slice.name = nw_name
# We cut the sequence such that seq[i] to correspond to # We cut the sequence such that seq[i] to correspond to
# seq[i-k] # seq[i-k]. For the purposes of cutting the sequences, we
if maxtap < 0: # need to pretend tap 0 is used to avoid cutting the sequences
offset = abs(maxtap) # too long if the taps are all lower or all higher than 0.
maxtap_proxy = max(maxtap, 0)
mintap_proxy = min(mintap, 0)
start = (k - mintap_proxy)
if k == maxtap_proxy:
nw_seq = seq['input'][start:]
else: else:
offset = 0 end = -(maxtap_proxy - k)
if maxtap == mintap and maxtap != 0: nw_seq = seq['input'][start:end]
if maxtap < 0:
nw_seq = seq['input'][:maxtap]
else:
nw_seq = seq['input'][maxtap:]
elif maxtap - k != 0:
nw_seq = seq['input'][offset + k - mintap: -(maxtap - k)]
else:
nw_seq = seq['input'][offset + k - mintap:]
if go_backwards: if go_backwards:
nw_seq = nw_seq[::-1] nw_seq = nw_seq[::-1]
......
...@@ -623,6 +623,20 @@ class T_Scan(unittest.TestCase): ...@@ -623,6 +623,20 @@ class T_Scan(unittest.TestCase):
rval = theano.function([x], y, updates=updates)(inp) rval = theano.function([x], y, updates=updates)(inp)
assert numpy.all(rval == inp[:-1]) assert numpy.all(rval == inp[:-1])
def test_using_negative_taps_sequence(self):
# This test refers to a bug reported on github on May 22 2015 by
# user june-qijun
def lp(x, x2):
return x
x = tensor.fvector('x')
res, upd = theano.scan(lp,
sequences=dict(input=x, taps=[-2, -1]))
f = theano.function([x], res, updates = upd)
output = f([1, 2, 3, 4, 5])
expected_output = numpy.array([1, 2, 3], dtype="float32")
utt.assert_allclose(output, expected_output)
def test_connection_pattern(self): def test_connection_pattern(self):
"""Test connection_pattern() in the presence of recurrent outputs """Test connection_pattern() in the presence of recurrent outputs
with multiple taps. with multiple taps.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论