提交 cc9273c7 authored 作者: Razvan Pascanu's avatar Razvan Pascanu

PEP8 fixes

上级 ec8d4f6b
...@@ -1264,6 +1264,7 @@ class Scan(PureOp): ...@@ -1264,6 +1264,7 @@ class Scan(PureOp):
# is connected) # is connected)
return diff_inputs return diff_inputs
return [gmp.get(p, None) for p in diff_inputs] return [gmp.get(p, None) for p in diff_inputs]
def _get_inner_outs(oidx): def _get_inner_outs(oidx):
s = 0 s = 0
if self.n_mit_mot > 0: if self.n_mit_mot > 0:
...@@ -1277,6 +1278,7 @@ class Scan(PureOp): ...@@ -1277,6 +1278,7 @@ class Scan(PureOp):
else: else:
e += 1 e += 1
return self.outputs[s:e] return self.outputs[s:e]
def _get_inner_inps(iidx): def _get_inner_inps(iidx):
s = 0 s = 0
if self.n_seqs > 0: if self.n_seqs > 0:
...@@ -1292,16 +1294,14 @@ class Scan(PureOp): ...@@ -1292,16 +1294,14 @@ class Scan(PureOp):
node.inputs[iidx + 1]) node.inputs[iidx + 1])
return [self.inner_non_seqs(self.inputs)[loc_idx]] return [self.inner_non_seqs(self.inputs)[loc_idx]]
for p in xrange(iidx): for p in xrange(iidx):
s = e s = e
if p < self.n_seqs: if p < self.n_seqs:
e += 1 e += 1
else: else:
e += len(self.tap_array[p-self.n_seqs]) e += len(self.tap_array[p - self.n_seqs])
return self.inputs[s:e] return self.inputs[s:e]
for oidx, out in enumerate(node.outputs): for oidx, out in enumerate(node.outputs):
for iidx, inp in enumerate(node.inputs[1:]): for iidx, inp in enumerate(node.inputs[1:]):
ols = _get_inner_outs(oidx) ols = _get_inner_outs(oidx)
...@@ -1309,7 +1309,7 @@ class Scan(PureOp): ...@@ -1309,7 +1309,7 @@ class Scan(PureOp):
if ils is None: if ils is None:
# The gradient should be undefined, not disconnected # The gradient should be undefined, not disconnected
connection_pattern[iidx+1][oidx] = True connection_pattern[iidx + 1][oidx] = True
else: else:
for inner_out in ols: for inner_out in ols:
if hasattr(inner_out, 'dtype'): if hasattr(inner_out, 'dtype'):
...@@ -1321,7 +1321,7 @@ class Scan(PureOp): ...@@ -1321,7 +1321,7 @@ class Scan(PureOp):
# It should be undefined not disconnected # It should be undefined not disconnected
tmp = ils tmp = ils
if any([x is not None for x in tmp]): if any([x is not None for x in tmp]):
connection_pattern[iidx+1][oidx] = True connection_pattern[iidx + 1][oidx] = True
return connection_pattern return connection_pattern
### GRAD FUNCTION ### GRAD FUNCTION
...@@ -1498,9 +1498,11 @@ class Scan(PureOp): ...@@ -1498,9 +1498,11 @@ class Scan(PureOp):
inner_out_mitmot.append(dC_dinps_t[ins_pos]) inner_out_mitmot.append(dC_dinps_t[ins_pos])
if not disconnected_dC_dinps_t[ins_pos]: if not disconnected_dC_dinps_t[ins_pos]:
disconnected = False disconnected = False
for _sh in self.inner_shared(self_inputs): for _sh in self.inner_shared(self_inputs):
if _sh in gof.graph.inputs([dC_dinps_t[ins_pos]]): if _sh in gof.graph.inputs([dC_dinps_t[ins_pos]]):
undefined = True undefined = True
n_mitmot_inps_ += 1 n_mitmot_inps_ += 1
ins_pos += 1 ins_pos += 1
n_mitmot_outs += 1 n_mitmot_outs += 1
...@@ -1670,6 +1672,7 @@ class Scan(PureOp): ...@@ -1670,6 +1672,7 @@ class Scan(PureOp):
zip(outputs[offset:offset + self.n_seqs], zip(outputs[offset:offset + self.n_seqs],
type_outs[offset:offset + self.n_seqs])): type_outs[offset:offset + self.n_seqs])):
if t == 'undefined': if t == 'undefined':
gradients.append( gradients.append(
grad_undefined(self, grad_undefined(self,
p + 1, p + 1,
......
...@@ -692,7 +692,8 @@ class ScanSaveMem(gof.Optimizer): ...@@ -692,7 +692,8 @@ class ScanSaveMem(gof.Optimizer):
tmp_idx = tensor.switch(cval < initl, tmp_idx = tensor.switch(cval < initl,
cval + initl, cval + initl,
cval - initl) cval - initl)
tmp = pre_greedy_local_optimizer(list_opt_slice, tmp_idx) tmp = pre_greedy_local_optimizer(list_opt_slice,
tmp_idx)
tmp = pre_constant_merge([tmp])[0] tmp = pre_constant_merge([tmp])[0]
nw_input = scan_utils.expand(_nw_input, tmp) nw_input = scan_utils.expand(_nw_input, tmp)
......
...@@ -513,7 +513,7 @@ class T_Scan(unittest.TestCase): ...@@ -513,7 +513,7 @@ class T_Scan(unittest.TestCase):
def f_rnn(u_t, x_tm1, W_in, W): def f_rnn(u_t, x_tm1, W_in, W):
return (u_t * W_in + x_tm1 * W, return (u_t * W_in + x_tm1 * W,
tensor.cast(u_t+x_tm1, 'int64')) tensor.cast(u_t + x_tm1, 'int64'))
u = theano.tensor.fvector('u') u = theano.tensor.fvector('u')
x0 = theano.tensor.fscalar('x0') x0 = theano.tensor.fscalar('x0')
...@@ -561,7 +561,6 @@ class T_Scan(unittest.TestCase): ...@@ -561,7 +561,6 @@ class T_Scan(unittest.TestCase):
scan_node = scan_node[0] scan_node = scan_node[0]
assert scan_node.op.gpu assert scan_node.op.gpu
# simple rnn, one input, one state, weights for each; input/state # simple rnn, one input, one state, weights for each; input/state
# are vectors, weights are scalars; using shared variables # are vectors, weights are scalars; using shared variables
def test_one_sequence_one_output_weights_shared(self): def test_one_sequence_one_output_weights_shared(self):
...@@ -1737,8 +1736,8 @@ class T_Scan(unittest.TestCase): ...@@ -1737,8 +1736,8 @@ class T_Scan(unittest.TestCase):
def f_rnn_cmpl(u_t, x_tm1, W_in): def f_rnn_cmpl(u_t, x_tm1, W_in):
trng1 = theano.tensor.shared_randomstreams.RandomStreams(123) trng1 = theano.tensor.shared_randomstreams.RandomStreams(123)
x_t = theano.dot(u_t, W_in) + x_tm1 + \ rnd_nb = trng1.uniform(low=-.1, high=.1)
trng1.uniform(low=-.1, high=.1) x_t = theano.dot(u_t, W_in) + x_tm1 + rnd_nb
x_t = theano.tensor.cast(x_t, dtype=theano.config.floatX) x_t = theano.tensor.cast(x_t, dtype=theano.config.floatX)
return x_t return x_t
...@@ -1900,8 +1899,8 @@ class T_Scan(unittest.TestCase): ...@@ -1900,8 +1899,8 @@ class T_Scan(unittest.TestCase):
def test_scan_extra_inputs_hessian(self): def test_scan_extra_inputs_hessian(self):
x = theano.tensor.vector('x') x = theano.tensor.vector('x')
A = theano.tensor.matrix('A') A = theano.tensor.matrix('A')
fc1 = theano.shared(0.5, name = 'fc1') fc1 = theano.shared(0.5, name='fc1')
fc2 = theano.shared(0.9, name = 'fc2') fc2 = theano.shared(0.9, name='fc2')
y = fc1 * theano.dot(x * x, theano.dot(A, x)) y = fc1 * theano.dot(x * x, theano.dot(A, x))
y.name = 'y' y.name = 'y'
gy = theano.tensor.grad(y, x) gy = theano.tensor.grad(y, x)
...@@ -2554,7 +2553,7 @@ class T_Scan(unittest.TestCase): ...@@ -2554,7 +2553,7 @@ class T_Scan(unittest.TestCase):
_W = theano.tensor.specify_shape(W, v_W.shape) _W = theano.tensor.specify_shape(W, v_W.shape)
_W.name = '_W' _W.name = '_W'
[o,_], _ = theano.scan(rnn_fn, [o, _], _ = theano.scan(rnn_fn,
sequences=_u, sequences=_u,
outputs_info=[_h0, None], outputs_info=[_h0, None],
non_sequences=_W, non_sequences=_W,
...@@ -3137,6 +3136,7 @@ class T_Scan(unittest.TestCase): ...@@ -3137,6 +3136,7 @@ class T_Scan(unittest.TestCase):
loss, loss,
no_default_updates=True, no_default_updates=True,
allow_input_downcast=True) allow_input_downcast=True)
gw, gx = tensor.grad(loss, [w, xinit]) gw, gx = tensor.grad(loss, [w, xinit])
grad_fn = theano.function([xinit, w], [gx, gw], grad_fn = theano.function([xinit, w], [gx, gw],
allow_input_downcast=True) allow_input_downcast=True)
...@@ -3164,10 +3164,11 @@ class T_Scan(unittest.TestCase): ...@@ -3164,10 +3164,11 @@ class T_Scan(unittest.TestCase):
def test_grad_numeric_shared(self): def test_grad_numeric_shared(self):
shared_var = theano.shared(numpy.float32(1.)) shared_var = theano.shared(numpy.float32(1.))
def inner_fn(): def inner_fn():
return [], {shared_var:shared_var + numpy.float32(1.)} return [], {shared_var: shared_var + numpy.float32(1.)}
_, updates = theano.scan(inner_fn, _, updates = theano.scan(inner_fn,
n_steps = 10, n_steps=10,
truncate_gradient=-1, truncate_gradient=-1,
go_backwards=False) go_backwards=False)
cost = updates.values()[0] cost = updates.values()[0]
...@@ -3228,17 +3229,17 @@ class T_Scan(unittest.TestCase): ...@@ -3228,17 +3229,17 @@ class T_Scan(unittest.TestCase):
Hp = tensor.Rop(d_cost_wrt_pars, pars, p) Hp = tensor.Rop(d_cost_wrt_pars, pars, p)
def test_seq_tap_bug_jeremiah(self): def test_seq_tap_bug_jeremiah(self):
inp = numpy.arange(10).reshape(-1,1).astype(theano.config.floatX) inp = numpy.arange(10).reshape(-1, 1).astype(theano.config.floatX)
exp_out = numpy.zeros((10,1)).astype(theano.config.floatX) exp_out = numpy.zeros((10, 1)).astype(theano.config.floatX)
exp_out[4:] = inp[:-4] exp_out[4:] = inp[:-4]
def onestep(x, x_tm4): def onestep(x, x_tm4):
return x, x_tm4 return x, x_tm4
seq = tensor.matrix() seq = tensor.matrix()
initial_value = theano.shared(numpy.zeros((4,1), initial_value = theano.shared(numpy.zeros((4, 1),
dtype=theano.config.floatX)) dtype=theano.config.floatX))
outputs_info = [{'initial' : initial_value, 'taps' : [-4]}, None] outputs_info = [{'initial': initial_value, 'taps': [-4]}, None]
results, updates = theano.scan(fn=onestep, results, updates = theano.scan(fn=onestep,
sequences=seq, sequences=seq,
outputs_info=outputs_info) outputs_info=outputs_info)
...@@ -3248,23 +3249,23 @@ class T_Scan(unittest.TestCase): ...@@ -3248,23 +3249,23 @@ class T_Scan(unittest.TestCase):
def test_borrow_bug_jeremiah(self): def test_borrow_bug_jeremiah(self):
# This test fails if scan uses wrongly the borrow flag # This test fails if scan uses wrongly the borrow flag
inp = numpy.arange(10).reshape(-1,1).astype(theano.config.floatX) inp = numpy.arange(10).reshape(-1, 1).astype(theano.config.floatX)
exp_out = numpy.zeros((10,1)).astype(theano.config.floatX) exp_out = numpy.zeros((10, 1)).astype(theano.config.floatX)
exp_out[4:] = inp[:-4] exp_out[4:] = inp[:-4]
def onestep(x, x_tm4): def onestep(x, x_tm4):
return x, x_tm4 return x, x_tm4
seq = tensor.matrix() seq = tensor.matrix()
initial_value = theano.shared(numpy.zeros((4,1), initial_value = theano.shared(numpy.zeros((4, 1),
dtype=theano.config.floatX)) dtype=theano.config.floatX))
outputs_info = [{'initial' : initial_value, 'taps' : [-4]}, None] outputs_info = [{'initial': initial_value, 'taps': [-4]}, None]
results, _ = theano.scan(fn=onestep, results, _ = theano.scan(fn=onestep,
sequences=seq, sequences=seq,
outputs_info=outputs_info) outputs_info=outputs_info)
sharedvar = theano.shared(numpy.zeros((1,1), sharedvar = theano.shared(numpy.zeros((1, 1),
dtype=theano.config.floatX)) dtype=theano.config.floatX))
updates = {sharedvar : results[0][-1:]} updates = {sharedvar: results[0][-1:]}
f = theano.function([seq], results[1], updates=updates) f = theano.function([seq], results[1], updates=updates)
assert numpy.all(exp_out == f(inp)) assert numpy.all(exp_out == f(inp))
...@@ -3274,18 +3275,18 @@ class T_Scan(unittest.TestCase): ...@@ -3274,18 +3275,18 @@ class T_Scan(unittest.TestCase):
x_tm1.name = 'x' x_tm1.name = 'x'
y_tm1.name = 'y' y_tm1.name = 'y'
z_tm1.name = 'z' z_tm1.name = 'z'
return x_tm1**2, x_tm1 + y_tm1, x_tm1+1 return x_tm1 ** 2, x_tm1 + y_tm1, x_tm1 + 1
x0 = tensor.vector('X') x0 = tensor.vector('X')
y0 = tensor.vector('y0') y0 = tensor.vector('y0')
z0 = tensor.vector('Z') z0 = tensor.vector('Z')
[x,y,z], _ = theano.scan(inner_fn, [x, y, z], _ = theano.scan(inner_fn,
outputs_info=[x0,y0,z0], outputs_info=[x0, y0, z0],
n_steps=10) n_steps=10)
cost = (x+y+z).sum() cost = (x + y + z).sum()
gx0 = tensor.grad(cost, x0) #defined gx0 = tensor.grad(cost, x0) # defined
gy0 = tensor.grad(cost, y0) #defined gy0 = tensor.grad(cost, y0) # defined
self.assertRaises(ValueError, tensor.grad, cost, z0) self.assertRaises(ValueError, tensor.grad, cost, z0)
cost = x.sum() cost = x.sum()
self.assertRaises(ValueError, tensor.grad, cost, y0) self.assertRaises(ValueError, tensor.grad, cost, y0)
...@@ -3638,9 +3639,7 @@ if __name__ == '__main__': ...@@ -3638,9 +3639,7 @@ if __name__ == '__main__':
def test_compute_test_value(): def test_compute_test_value():
""" # Verify that test values can be used with scan.
Verify that test values can be used with scan.
"""
backup = theano.config.compute_test_value backup = theano.config.compute_test_value
theano.config.compute_test_value = 'raise' theano.config.compute_test_value = 'raise'
try: try:
...@@ -3652,7 +3651,7 @@ def test_compute_test_value(): ...@@ -3652,7 +3651,7 @@ def test_compute_test_value():
fn=lambda u, v: u + v, fn=lambda u, v: u + v,
sequences=[x, y]) sequences=[x, y])
assert not _ assert not _
z.name='z' z.name = 'z'
# The gradient computation used to crash before 6af465e. # The gradient computation used to crash before 6af465e.
g = tensor.grad(z.sum(), x) g = tensor.grad(z.sum(), x)
#f = theano.function([x], g) #f = theano.function([x], g)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论