提交 b295b284 authored 作者: nouiz's avatar nouiz

Merge pull request #365 from delallea/minor

Minor stuff (PEP8 and typos mostly)
...@@ -468,17 +468,17 @@ class PureOp(object): ...@@ -468,17 +468,17 @@ class PureOp(object):
returns: a list of n elements returns: a list of n elements
rval[i] should be Rop(f = f_i(inputs), rval[i] should be Rop(f=f_i(inputs),
wrt = inputs, wrt=inputs,
eval_points = eval_points) eval_points=eval_points)
""" """
raise NotImplementedError(
raise NotImplementedError(str(self)+' of type '+str(self.__class__.__name__) "%s of class %s does not "
+" does not "
"implement R_op. If this is a theano op, write to the " "implement R_op. If this is a theano op, write to the "
"theano-dev mailing list for assistance. If it is your " "theano-dev mailing list for assistance. If it is your "
"own op, implement the R_op method.") "own op, implement the R_op method." %
(self, self.__class__.__name__))
def perform(self, node, inputs, output_storage): def perform(self, node, inputs, output_storage):
...@@ -649,7 +649,7 @@ def debug_error_message(msg): ...@@ -649,7 +649,7 @@ def debug_error_message(msg):
warnings.warn(msg, stacklevel=2) warnings.warn(msg, stacklevel=2)
def debug_assert(condition, msg = None): def debug_assert(condition, msg=None):
if msg is None: if msg is None:
msg = 'debug_assert failed' msg = 'debug_assert failed'
if not condition: if not condition:
......
...@@ -317,7 +317,7 @@ def ifelse(condition, then_branch, else_branch, name=None): ...@@ -317,7 +317,7 @@ def ifelse(condition, then_branch, else_branch, name=None):
# we will store them in these new_... lists. # we will store them in these new_... lists.
new_then_branch = [] new_then_branch = []
new_else_branch = [] new_else_branch = []
for then_branch_elem, else_branch_elem in zip(then_branch, else_branch): for then_branch_elem, else_branch_elem in izip(then_branch, else_branch):
if not isinstance(then_branch_elem, theano.Variable): if not isinstance(then_branch_elem, theano.Variable):
then_branch_elem = theano.tensor.as_tensor_variable(then_branch_elem) then_branch_elem = theano.tensor.as_tensor_variable(then_branch_elem)
if not isinstance(else_branch_elem, theano.Variable): if not isinstance(else_branch_elem, theano.Variable):
...@@ -341,12 +341,12 @@ def ifelse(condition, then_branch, else_branch, name=None): ...@@ -341,12 +341,12 @@ def ifelse(condition, then_branch, else_branch, name=None):
if then_branch_elem.type != else_branch_elem.type: if then_branch_elem.type != else_branch_elem.type:
# If the types still don't match, there is a problem. # If the types still don't match, there is a problem.
raise ValueError( raise ValueError(
('The two branches should have identical types, ' 'The two branches should have identical types, but '
' but they are '+str(then_branch_elem.type)+' and '+ 'they are %s and %s respectively. This error could be '
str(else_branch_elem.type)+' respectively. ' 'raised if for example you provided a one element '
'This error could be raised if for example ' 'list on the `then` branch but a tensor on the `else` '
' you provided a one element list on the then ' 'branch.' %
' branch but a tensor on the else branch')) (then_branch_elem.type, else_branch_elem.type))
new_then_branch.append(then_branch_elem) new_then_branch.append(then_branch_elem)
new_else_branch.append(else_branch_elem) new_else_branch.append(else_branch_elem)
......
...@@ -944,7 +944,7 @@ def min_informative_str(obj, indent_level=0, ...@@ -944,7 +944,7 @@ def min_informative_str(obj, indent_level=0,
obj: the name to convert to a string obj: the name to convert to a string
indent_level: the number of tabs the tree should start printing at indent_level: the number of tabs the tree should start printing at
(nested levels of the tree will get more tabs) (nested levels of the tree will get more tabs)
_prev_obs: should only be used to by min_informative_str _prev_obs: should only be used by min_informative_str
a dictionary mapping previously converted a dictionary mapping previously converted
objects to short tags objects to short tags
......
...@@ -1298,11 +1298,12 @@ class Scan(PureOp): ...@@ -1298,11 +1298,12 @@ class Scan(PureOp):
else: else:
dim_offset = 0 dim_offset = 0
if maxtap == mintap and maxtap != 0: if maxtap == mintap and maxtap != 0:
nw_seq =seq[:abs(maxtap)] nw_seq = seq[:abs(maxtap)]
elif maxtap -k != 0 : elif maxtap -k != 0 :
nw_seq = seq[dim_offset +k -mintap - 1: -(maxtap -k + 1)][::-1] tmp = seq[dim_offset + k - mintap - 1:-(maxtap -k + 1)]
nw_seq = tmp[::-1]
else: else:
nw_seq = seq[dim_offset +k -mintap - 1: -1 ][::-1] nw_seq = seq[dim_offset + k - mintap - 1: -1][::-1]
if getattr(seq,'name', None) is not None: if getattr(seq,'name', None) is not None:
nw_seq.name = seq.name + '[%d:]'%k nw_seq.name = seq.name + '[%d:]'%k
scan_seqs.append(nw_seq) scan_seqs.append(nw_seq)
...@@ -1593,7 +1594,7 @@ class Scan(PureOp): ...@@ -1593,7 +1594,7 @@ class Scan(PureOp):
# Outputs # Outputs
n_mit_mot_outs = int(numpy.sum([len(x) for x in n_mit_mot_outs = int(numpy.sum([len(x) for x in
self.mit_mot_out_slices])) self.mit_mot_out_slices]))
info['n_mit_mot_outs'] = n_mit_mot_outs*2 info['n_mit_mot_outs'] = n_mit_mot_outs * 2
b = 0 b = 0
e = n_mit_mot_outs e = n_mit_mot_outs
inner_out_mit_mot = self_outputs[b:e] + rop_outs[b:e] inner_out_mit_mot = self_outputs[b:e] + rop_outs[b:e]
......
...@@ -687,16 +687,17 @@ class Elemwise(Op): ...@@ -687,16 +687,17 @@ class Elemwise(Op):
msg2 += [str(d)] msg2 += [str(d)]
msg.append('(%s)' % ", ".join(msg2)) msg.append('(%s)' % ", ".join(msg2))
base_exc_str = 'Dimension mismatch; shapes are %s' % (', '.join(msg)) base_exc_str = 'Dimension mismatch; shapes are %s' % (
', '.join(msg))
if config.exception_verbosity == 'high': if config.exception_verbosity == 'high':
msg_chunks = [ base_exc_str ] msg_chunks = [base_exc_str]
for i, ipt in enumerate(node.inputs): for i, ipt in enumerate(node.inputs):
msg_chunks.append('input %d: %s' % (i, min_informative_str(ipt))) msg_chunks.append('input %d: %s' %
(i, min_informative_str(ipt)))
raise ValueError('\n'.join(msg_chunks)) raise ValueError('\n'.join(msg_chunks))
else: else:
raise ValueError(base_exc_str) raise ValueError(base_exc_str)
#', '.join('(%s)' % ', '.join(msg)))
#backport #backport
#raise ValueError('Dimension mismatch; shapes are %s' % #raise ValueError('Dimension mismatch; shapes are %s' %
# ', '.join('(%s)' % ', '.join('*' if b else str(d) # ', '.join('(%s)' % ', '.join('*' if b else str(d)
......
...@@ -28,13 +28,13 @@ def format_as(use_list, use_tuple, outputs): ...@@ -28,13 +28,13 @@ def format_as(use_list, use_tuple, outputs):
""" """
Formats the outputs according to the flags `use_list` and `use_tuple`. Formats the outputs according to the flags `use_list` and `use_tuple`.
If `use_list` is True, `outputs` is returned as a list (if `outputs` If `use_list` is True, `outputs` is returned as a list (if `outputs`
is not a list or a tuple then it is converted in a one element list) is not a list or a tuple then it is converted in a one element list).
If `use_tuple` is True, `outputs` is returned as a tuple (if `outputs` If `use_tuple` is True, `outputs` is returned as a tuple (if `outputs`
is not a list or a tuple then it is converted into a one element tuple) is not a list or a tuple then it is converted into a one element tuple).
Otherwise (if both flags are false), `outputs` is returned. Otherwise (if both flags are false), `outputs` is returned.
""" """
assert not (use_list and use_tuple), \ assert not (use_list and use_tuple), \
"Both flags can not be simultaneously True" "Both flags cannot be simultaneously True"
if (use_list or use_tuple) and not isinstance(outputs, (list, tuple)): if (use_list or use_tuple) and not isinstance(outputs, (list, tuple)):
if use_list: if use_list:
return [outputs] return [outputs]
...@@ -94,8 +94,8 @@ def Rop(f, wrt, eval_points): ...@@ -94,8 +94,8 @@ def Rop(f, wrt, eval_points):
assert len(wrt) == len(eval_points) assert len(wrt) == len(eval_points)
#check that each element of wrt corresponds to an element # Check that each element of wrt corresponds to an element
#of eval_points with the same dimensionality # of eval_points with the same dimensionality.
for pack in enumerate(zip(wrt, eval_points)): for pack in enumerate(zip(wrt, eval_points)):
i = pack[0] i = pack[0]
wrt_elem, eval_point = pack[1] wrt_elem, eval_point = pack[1]
...@@ -701,14 +701,15 @@ class GradientError(Exception): ...@@ -701,14 +701,15 @@ class GradientError(Exception):
def __str__(self): def __str__(self):
# args may have been inserted by e.g. makeTester # args may have been inserted by e.g. makeTester
args_msg = ", ".join(str(a) for a in self.args) args_msg = ", ".join(str(a) for a in self.args)
return """GradientError: numeric gradient and analytic gradient exceed tolerance: return """\
GradientError: numeric gradient and analytic gradient exceed tolerance:
At position %i of argument %i, At position %i of argument %i,
abs. error = %f, abs. tolerance = %f abs. error = %f, abs. tolerance = %f
rel. error = %f, rel. tolerance = %f\nException args: %s rel. error = %f, rel. tolerance = %f
""" %(self.err_pos, self.arg, Exception args: %s""" % (self.err_pos, self.arg,
self.abs_err, self.abs_tol, self.abs_err, self.abs_tol,
self.rel_err, self.rel_tol, self.rel_err, self.rel_tol,
args_msg) args_msg)
verify_grad.E_grad = GradientError verify_grad.E_grad = GradientError
...@@ -789,8 +790,8 @@ def hessian(cost, wrt, consider_constant=None, warn_type=False, ...@@ -789,8 +790,8 @@ def hessian(cost, wrt, consider_constant=None, warn_type=False,
disconnected_inputs='raise'): disconnected_inputs='raise'):
""" """
:type cost: Scalar (0-dimensional) `Variable` :type cost: Scalar (0-dimensional) `Variable`
:type wrt: Vector (1-dimensional tensors) 'Variable' or list of :type wrt: Vector (1-dimensional tensor) 'Variable' or list of
vectors (1-dimensional tensors) `Variables`s vectors (1-dimensional tensors) `Variable`s
:param consider_constant: a list of expressions not to backpropagate :param consider_constant: a list of expressions not to backpropagate
through through
......
...@@ -94,7 +94,7 @@ def test002_jacobian_matrix(): ...@@ -94,7 +94,7 @@ def test002_jacobian_matrix():
evx[dx, dx, :] = vx[dx, :] evx[dx, dx, :] = vx[dx, :]
evz[dx, dx, :] = vz[dx, :] evz[dx, dx, :] = vz[dx, :]
assert numpy.allclose(vJs[0], evz) assert numpy.allclose(vJs[0], evz)
assert numpy.allclose(vJs[1 ], evx) assert numpy.allclose(vJs[1], evx)
def test003_jacobian_scalar(): def test003_jacobian_scalar():
...@@ -117,7 +117,7 @@ def test003_jacobian_scalar(): ...@@ -117,7 +117,7 @@ def test003_jacobian_scalar():
# test when the jacobian is called with a list as wrt # test when the jacobian is called with a list as wrt
Jx = tensor.jacobian(y, [x]) Jx = tensor.jacobian(y, [x])
assert isinstance(Jx,list) assert isinstance(Jx, list)
f = theano.function([x], Jx[0]) f = theano.function([x], Jx[0])
vx = numpy.cast[theano.config.floatX](rng.uniform()) vx = numpy.cast[theano.config.floatX](rng.uniform())
assert numpy.allclose(f(vx), 2) assert numpy.allclose(f(vx), 2)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论