提交 ea0717ee authored 作者: Olivier Delalleau's avatar Olivier Delalleau

PEP8 and a minor code simplification

上级 4f4265ff
...@@ -10,7 +10,6 @@ if PY3: ...@@ -10,7 +10,6 @@ if PY3:
from operator import truediv as operator_div from operator import truediv as operator_div
# In python 3.x, when an exception is reraised it saves original # In python 3.x, when an exception is reraised it saves original
# exception in its args, therefore in order to find the actual # exception in its args, therefore in order to find the actual
# message, we need to unpack arguments recursively. # message, we need to unpack arguments recursively.
...@@ -36,7 +35,6 @@ else: ...@@ -36,7 +35,6 @@ else:
from operator import div as operator_div from operator import div as operator_div
def exc_message(e): def exc_message(e):
return e[0] return e[0]
......
...@@ -693,7 +693,7 @@ class MRG_RandomStreams(object): ...@@ -693,7 +693,7 @@ class MRG_RandomStreams(object):
rval = numpy.zeros((n_streams,6), dtype='int32') rval = numpy.zeros((n_streams,6), dtype='int32')
rval[0] = self.rstate rval[0] = self.rstate
for i in xrange(1, n_streams): for i in xrange(1, n_streams):
rval[i] = ff_2p72(rval[i-1]) rval[i] = ff_2p72(rval[i - 1])
if inc_rstate: if inc_rstate:
self.inc_rstate() self.inc_rstate()
return rval return rval
...@@ -741,7 +741,7 @@ class MRG_RandomStreams(object): ...@@ -741,7 +741,7 @@ class MRG_RandomStreams(object):
if isinstance(size, tuple): if isinstance(size, tuple):
msg = "size must be a tuple of int or a Theano variable" msg = "size must be a tuple of int or a Theano variable"
assert all([isinstance(i, (numpy.integer, int)) or isinstance(i,Variable) assert all([isinstance(i, (numpy.integer, int, Variable))
for i in size]), msg for i in size]), msg
if any([isinstance(i, (numpy.integer, int)) and i <= 0 for i in size]): if any([isinstance(i, (numpy.integer, int)) and i <= 0 for i in size]):
raise ValueError( raise ValueError(
...@@ -750,12 +750,12 @@ class MRG_RandomStreams(object): ...@@ -750,12 +750,12 @@ class MRG_RandomStreams(object):
else: else:
msg = "size must be a tuple of int or a Theano variable" msg = "size must be a tuple of int or a Theano variable"
assert isinstance(size, Variable) and size.ndim==1, msg assert isinstance(size, Variable) and size.ndim == 1, msg
if nstreams is None: if nstreams is None:
nstreams = self.n_streams(size) nstreams = self.n_streams(size)
if self.use_cuda and dtype=='float32': if self.use_cuda and dtype == 'float32':
rstates = self.get_substream_rstates(nstreams) rstates = self.get_substream_rstates(nstreams)
rstates = rstates.flatten() rstates = rstates.flatten()
# HACK - we use fact that int32 and float32 have same size to # HACK - we use fact that int32 and float32 have same size to
...@@ -779,10 +779,12 @@ class MRG_RandomStreams(object): ...@@ -779,10 +779,12 @@ class MRG_RandomStreams(object):
node_rstate = shared(self.get_substream_rstates(nstreams)) node_rstate = shared(self.get_substream_rstates(nstreams))
u = self.pretty_return(node_rstate, u = self.pretty_return(node_rstate,
*mrg_uniform.new(node_rstate, ndim, dtype, size)) *mrg_uniform.new(node_rstate, ndim, dtype, size))
r = u * (high-low) + low r = u * (high - low) + low
if u.type.broadcastable != r.type.broadcastable: if u.type.broadcastable != r.type.broadcastable:
raise NotImplementedError( 'Increase the size to match the broadcasting pattern of `low` and `high` arguments') raise NotImplementedError(
'Increase the size to match the broadcasting pattern of '
'`low` and `high` arguments')
assert r.dtype == dtype assert r.dtype == dtype
return r return r
...@@ -836,9 +838,9 @@ class MRG_RandomStreams(object): ...@@ -836,9 +838,9 @@ class MRG_RandomStreams(object):
"MRG_RandomStreams.multinomial, which does not use " "MRG_RandomStreams.multinomial, which does not use "
"the ndim argument.") "the ndim argument.")
ndim, size, bcast = raw_random._infer_ndim_bcast( ndim, size, bcast = raw_random._infer_ndim_bcast(
ndim, size, pvals[:,0]) ndim, size, pvals[:, 0])
assert ndim==1 assert ndim == 1
bcast = bcast+(pvals.type.broadcastable[-1],) bcast = bcast + (pvals.type.broadcastable[-1],)
unis = self.uniform(size=size, ndim=1, nstreams=nstreams) unis = self.uniform(size=size, ndim=1, nstreams=nstreams)
op = multinomial.MultinomialFromUniform(dtype) op = multinomial.MultinomialFromUniform(dtype)
return op(pvals, unis) return op(pvals, unis)
...@@ -882,7 +884,7 @@ class MRG_RandomStreams(object): ...@@ -882,7 +884,7 @@ class MRG_RandomStreams(object):
evened = True evened = True
else: else:
#if even, don't change, if odd, +1 #if even, don't change, if odd, +1
n_samples = prod(size)+(prod(size)%2) n_samples = prod(size) + (prod(size) % 2)
flattened = self.uniform(size=(n_samples,), dtype=dtype, flattened = self.uniform(size=(n_samples,), dtype=dtype,
nstreams=nstreams) nstreams=nstreams)
...@@ -902,7 +904,7 @@ class MRG_RandomStreams(object): ...@@ -902,7 +904,7 @@ class MRG_RandomStreams(object):
# so trying this instead # so trying this instead
first_half = sqrt_ln_U1 * cos(numpy.array(2.0 * numpy.pi, dtype=dtype) * U2) first_half = sqrt_ln_U1 * cos(numpy.array(2.0 * numpy.pi, dtype=dtype) * U2)
second_half = sqrt_ln_U1 * sin(numpy.array(2.0 * numpy.pi, dtype=dtype)*U2) second_half = sqrt_ln_U1 * sin(numpy.array(2.0 * numpy.pi, dtype=dtype) * U2)
normal_samples = join(0, first_half, second_half) normal_samples = join(0, first_half, second_half)
final_samples = None final_samples = None
...@@ -921,6 +923,7 @@ class MRG_RandomStreams(object): ...@@ -921,6 +923,7 @@ class MRG_RandomStreams(object):
assert final_samples.dtype == dtype assert final_samples.dtype == dtype
return final_samples return final_samples
@local_optimizer([None]) @local_optimizer([None])
def mrg_random_make_inplace(node): def mrg_random_make_inplace(node):
op = node.op op = node.op
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论