Unverified 提交 031f463a authored 作者: Frédéric Bastien's avatar Frédéric Bastien 提交者: GitHub

Merge pull request #6582 from nouiz/scan_name

Fix scan names of variables.
......@@ -83,6 +83,7 @@ Francesco Visin <fvisin@gmail.com> Francesco <fvisin@users.noreply.github.com>
Francesco Visin <fvisin@gmail.com> fvisin <fvisin@gmail.com>
Francois Savard <devnull@localhost> fsavard <devnull@localhost>
Frederic Bastien <nouiz@nouiz.org> Frederic Bastien <bastienf@briaree1.rqchp.qc.ca>
Frederic Bastien <nouiz@nouiz.org> Frederic Bastien <Frederic Bastien>
Frederic Bastien <nouiz@nouiz.org> Frederic Bastien <bastienf@iro.umontreal.ca>
Frederic Bastien <nouiz@nouiz.org> Frédéric Bastien <nouiz@nouiz.org>
Frederic Bastien <nouiz@nouiz.org> Nouiz <nouiz@Nouiz-lap-ub.(none)>
......
......@@ -536,11 +536,16 @@ def scan(fn,
maxtap_proxy = max(maxtap, 0)
mintap_proxy = min(mintap, 0)
start = (k - mintap_proxy)
nw_name = None
if k == maxtap_proxy:
nw_seq = seq['input'][start:]
if getattr(seq['input'], 'name', None) is not None:
nw_name = seq['input'].name + "[%d:]" % start
else:
end = -(maxtap_proxy - k)
nw_seq = seq['input'][start:end]
if getattr(seq['input'], 'name', None) is not None:
nw_name = seq['input'].name + "[%d:%d]" % (start, end)
if go_backwards:
nw_seq = nw_seq[::-1]
......@@ -549,6 +554,9 @@ def scan(fn,
inner_seqs.append(nw_slice)
inner_slices.append(actual_slice)
n_seqs += 1
# Add names -- it helps a lot when debugging
if nw_name is not None:
nw_seq.name = nw_name
# Since we've added all sequences now we need to level them up based on
# n_steps or their different shapes
......@@ -576,12 +584,6 @@ def scan(fn,
else:
actual_n_steps = tensor.as_tensor(n_steps)
# Add names -- it helps a lot when debugging
for (nw_seq, seq) in zip(scan_seqs, seqs):
if getattr(seq['input'], 'name', None) is not None:
nw_seq.name = seq['input'].name + '[%d:]' % k
scan_seqs = [seq[:actual_n_steps] for seq in scan_seqs]
# Conventions :
# mit_mot = multiple input taps, multiple output taps ( only provided
......
......@@ -1588,6 +1588,184 @@ class T_Scan(unittest.TestCase):
params = [u1, u2, x0, y0, W_in1]
gparams = theano.tensor.grad(cost, params)
print(".", file=sys.stderr)
# Test the output including names
output_str = theano.printing.debugprint(cost, file='str')
lines = output_str.split('\n')
expected_output = """Elemwise{add,no_inplace} [id A] ''
|Elemwise{add,no_inplace} [id B] ''
| |Elemwise{add,no_inplace} [id C] ''
| | |TensorConstant{0} [id D]
| | |Sum{acc_dtype=float64} [id E] ''
| | |Elemwise{mul,no_inplace} [id F] ''
| | |Subtensor{int64::} [id G] ''
| | | |for{cpu,scan_fn}.1 [id H] ''
| | | | |Elemwise{minimum,no_inplace} [id I] ''
| | | | | |Elemwise{minimum,no_inplace} [id J] ''
| | | | | | |Elemwise{minimum,no_inplace} [id K] ''
| | | | | | | |Subtensor{int64} [id L] ''
| | | | | | | | |Shape [id M] ''
| | | | | | | | | |Subtensor{int64::} [id N] 'u1[0:]'
| | | | | | | | | |u1 [id O]
| | | | | | | | | |Constant{0} [id P]
| | | | | | | | |Constant{0} [id Q]
| | | | | | | |Subtensor{int64} [id R] ''
| | | | | | | |Shape [id S] ''
| | | | | | | | |Subtensor{int64:int64:} [id T] 'u2[0:-2]'
| | | | | | | | |u2 [id U]
| | | | | | | | |Constant{0} [id V]
| | | | | | | | |Constant{-2} [id W]
| | | | | | | |Constant{0} [id X]
| | | | | | |Subtensor{int64} [id Y] ''
| | | | | | |Shape [id Z] ''
| | | | | | | |Subtensor{int64:int64:} [id BA] 'u2[1:-1]'
| | | | | | | |u2 [id U]
| | | | | | | |Constant{1} [id BB]
| | | | | | | |Constant{-1} [id BC]
| | | | | | |Constant{0} [id BD]
| | | | | |Subtensor{int64} [id BE] ''
| | | | | |Shape [id BF] ''
| | | | | | |Subtensor{int64::} [id BG] 'u2[2:]'
| | | | | | |u2 [id U]
| | | | | | |Constant{2} [id BH]
| | | | | |Constant{0} [id BI]
| | | | |Subtensor{:int64:} [id BJ] ''
| | | | | |Subtensor{int64::} [id N] 'u1[0:]'
| | | | | |ScalarFromTensor [id BK] ''
| | | | | |Elemwise{minimum,no_inplace} [id I] ''
| | | | |Subtensor{:int64:} [id BL] ''
| | | | | |Subtensor{int64:int64:} [id T] 'u2[0:-2]'
| | | | | |ScalarFromTensor [id BM] ''
| | | | | |Elemwise{minimum,no_inplace} [id I] ''
| | | | |Subtensor{:int64:} [id BN] ''
| | | | | |Subtensor{int64:int64:} [id BA] 'u2[1:-1]'
| | | | | |ScalarFromTensor [id BO] ''
| | | | | |Elemwise{minimum,no_inplace} [id I] ''
| | | | |Subtensor{:int64:} [id BP] ''
| | | | | |Subtensor{int64::} [id BG] 'u2[2:]'
| | | | | |ScalarFromTensor [id BQ] ''
| | | | | |Elemwise{minimum,no_inplace} [id I] ''
| | | | |IncSubtensor{Set;:int64:} [id BR] ''
| | | | | |AllocEmpty{dtype='%(float)s'} [id BS] ''
| | | | | | |Elemwise{add,no_inplace} [id BT] ''
| | | | | | |Elemwise{minimum,no_inplace} [id I] ''
| | | | | | |Subtensor{int64} [id BU] ''
| | | | | | |Shape [id BV] ''
| | | | | | | |Subtensor{:int64:} [id BW] ''
| | | | | | | |y0 [id BX]
| | | | | | | |Constant{3} [id BY]
| | | | | | |Constant{0} [id BZ]
| | | | | |Subtensor{:int64:} [id BW] ''
| | | | | |ScalarFromTensor [id CA] ''
| | | | | |Subtensor{int64} [id BU] ''
| | | | |IncSubtensor{Set;:int64:} [id CB] ''
| | | | | |AllocEmpty{dtype='%(float)s'} [id CC] ''
| | | | | | |Elemwise{add,no_inplace} [id CD] ''
| | | | | | | |Elemwise{minimum,no_inplace} [id I] ''
| | | | | | | |Subtensor{int64} [id CE] ''
| | | | | | | |Shape [id CF] ''
| | | | | | | | |Rebroadcast{0} [id CG] ''
| | | | | | | | |InplaceDimShuffle{x,0} [id CH] ''
| | | | | | | | |x0 [id CI]
| | | | | | | |Constant{0} [id CJ]
| | | | | | |Subtensor{int64} [id CK] ''
| | | | | | |Shape [id CL] ''
| | | | | | | |Rebroadcast{0} [id CG] ''
| | | | | | |Constant{1} [id CM]
| | | | | |Rebroadcast{0} [id CG] ''
| | | | | |ScalarFromTensor [id CN] ''
| | | | | |Subtensor{int64} [id CE] ''
| | | | |Elemwise{minimum,no_inplace} [id I] ''
| | | | |win2 [id CO]
| | | | |w [id CP]
| | | | |wout [id CQ]
| | | | |win [id CR]
| | | |Constant{1} [id CS]
| | |RandomFunction{uniform}.1 [id CT] ''
| | |<RandomStateType> [id CU]
| | |Shape [id CV] ''
| | | |Subtensor{int64::} [id G] ''
| | |TensorConstant{0.1} [id CW]
| | |TensorConstant{0.9} [id CX]
| |Sum{acc_dtype=float64} [id CY] ''
| |Elemwise{mul,no_inplace} [id CZ] ''
| |Subtensor{int64::} [id DA] ''
| | |for{cpu,scan_fn}.0 [id H] ''
| | |Constant{3} [id DB]
| |RandomFunction{uniform}.1 [id DC] ''
| |<RandomStateType> [id DD]
| |Shape [id DE] ''
| | |Subtensor{int64::} [id DA] ''
| |TensorConstant{0.1} [id CW]
| |TensorConstant{0.9} [id CX]
|Sum{acc_dtype=float64} [id DF] ''
|Elemwise{mul,no_inplace} [id DG] ''
|for{cpu,scan_fn}.2 [id H] ''
|RandomFunction{uniform}.1 [id DH] ''
|<RandomStateType> [id DI]
|Shape [id DJ] ''
| |for{cpu,scan_fn}.2 [id H] ''
|TensorConstant{0.1} [id CW]
|TensorConstant{0.9} [id CX]
Inner graphs of the scan ops:
for{cpu,scan_fn}.1 [id H] ''
>Elemwise{Composite{((i0 + i1) * i2)}} [id DK] ''
> |y0[t-1] [id DL] -> [id BR]
> |y0[t-3] [id DM] -> [id BR]
> |InplaceDimShuffle{} [id DN] ''
> |CGemv{inplace} [id DO] ''
> |AllocEmpty{dtype='%(float)s'} [id DP] ''
> | |TensorConstant{1} [id DQ]
> |TensorConstant{1.0} [id DR]
> |InplaceDimShuffle{x,0} [id DS] ''
> | |wout_copy [id DT] -> [id CQ]
> |x0[t-1] [id DU] -> [id CB]
> |TensorConstant{0.0} [id DV]
>Elemwise{Composite{(i0 + ((i1 + (i2 * i3)) * i4) + i5)}} [id DW] ''
> |CGemv{no_inplace} [id DX] ''
> | |AllocEmpty{dtype='%(float)s'} [id DY] ''
> | | |Shape_i{1} [id DZ] ''
> | | |win_copy [id EA] -> [id CR]
> | |TensorConstant{1.0} [id DR]
> | |InplaceDimShuffle{1,0} [id EB] 'win_copy.T'
> | | |win_copy [id EA] -> [id CR]
> | |u1[t] [id EC] -> [id BJ]
> | |TensorConstant{0.0} [id DV]
> |u2[t] [id ED] -> [id BN]
> |u2[t-1] [id EE] -> [id BL]
> |u2[t+1] [id EF] -> [id BP]
> |win2_copy [id EG] -> [id CO]
> |CGemv{inplace} [id EH] ''
> |AllocEmpty{dtype='%(float)s'} [id EI] ''
> | |Shape_i{1} [id EJ] ''
> | |w_copy [id EK] -> [id CP]
> |TensorConstant{1.0} [id DR]
> |InplaceDimShuffle{1,0} [id EL] 'w_copy.T'
> | |w_copy [id EK] -> [id CP]
> |x0[t-1] [id DU] -> [id CB]
> |TensorConstant{0.0} [id DV]
>CGemv{no_inplace} [id DX] ''
for{cpu,scan_fn}.0 [id H] ''
>Elemwise{Composite{((i0 + i1) * i2)}} [id DK] ''
>Elemwise{Composite{(i0 + ((i1 + (i2 * i3)) * i4) + i5)}} [id DW] ''
>CGemv{no_inplace} [id DX] ''
for{cpu,scan_fn}.2 [id H] ''
>Elemwise{Composite{((i0 + i1) * i2)}} [id DK] ''
>Elemwise{Composite{(i0 + ((i1 + (i2 * i3)) * i4) + i5)}} [id DW] ''
>CGemv{no_inplace} [id DX] ''
for{cpu,scan_fn}.2 [id H] ''
>Elemwise{Composite{((i0 + i1) * i2)}} [id DK] ''
>Elemwise{Composite{(i0 + ((i1 + (i2 * i3)) * i4) + i5)}} [id DW] ''
>CGemv{no_inplace} [id DX] ''
""" % {"float":theano.config.floatX}
if theano.config.mode != 'FAST_COMPILE' and theano.config.floatX == 'float64':
for truth, out in zip(expected_output.split("\n"), lines):
assert truth.strip() == out.strip(), (truth, out)
cost_fn = theano.function([u1, u2, x0, y0, W_in1],
cost,
updates=updates,
......
......@@ -275,9 +275,7 @@ def test_scan_debugprint1():
final_result = result[-1]
output_str = theano.printing.debugprint(final_result, file='str')
lines = []
for line in output_str.split('\n'):
lines += [line]
lines = output_str.split('\n')
expected_output = """Subtensor{int64} [id A] ''
|Subtensor{int64::} [id B] ''
......@@ -338,9 +336,7 @@ def test_scan_debugprint2():
polynomial = components.sum()
output_str = theano.printing.debugprint(polynomial, file='str')
lines = []
for line in output_str.split('\n'):
lines += [line]
lines = output_str.split('\n')
expected_output = """Sum{acc_dtype=float64} [id A] ''
|for{cpu,scan_fn} [id B] ''
......@@ -421,9 +417,7 @@ def test_scan_debugprint3():
final_result = polynomial
output_str = theano.printing.debugprint(final_result, file='str')
lines = []
for line in output_str.split('\n'):
lines += [line]
lines = output_str.split('\n')
expected_output = """Sum{acc_dtype=float64} [id A] ''
|for{cpu,scan_fn} [id B] ''
......@@ -516,9 +510,7 @@ def test_scan_debugprint4():
final_result = a + b
output_str = theano.printing.debugprint(final_result, file='str')
lines = []
for line in output_str.split('\n'):
lines += [line]
lines = output_str.split('\n')
expected_output = """Elemwise{add,no_inplace} [id A] ''
|Subtensor{int64::} [id B] ''
......@@ -587,9 +579,7 @@ def test_scan_debugprint5():
final_result = tensor.grad(result[-1].sum(), A)
output_str = theano.printing.debugprint(final_result, file='str')
lines = []
for line in output_str.split('\n'):
lines += [line]
lines = output_str.split('\n')
expected_output = """Subtensor{int64} [id A] ''
|for{cpu,grad_of_scan_fn}.1 [id B] ''
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论