提交 33a8c9de authored 作者: nouiz's avatar nouiz

Merge pull request #1253 from delallea/minor

Minor fixes
...@@ -10,10 +10,9 @@ if PY3: ...@@ -10,10 +10,9 @@ 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 recurcively. # message, we need to unpack arguments recursively.
def exc_message(e): def exc_message(e):
msg = e.args[0] msg = e.args[0]
if isinstance(msg, Exception): if isinstance(msg, Exception):
...@@ -21,7 +20,7 @@ if PY3: ...@@ -21,7 +20,7 @@ if PY3:
return msg return msg
def cmp(a, b): def cmp(a, b):
"""Return -1 if x<y, 0 if x==y, 1 if x > y.""" """Return -1 if x < y, 0 if x == y, 1 if x > y."""
return (a > b) - (a < b) return (a > b) - (a < b)
all = all all = all
...@@ -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]
......
...@@ -293,7 +293,6 @@ class Param(object): ...@@ -293,7 +293,6 @@ class Param(object):
`mutable` flag. `mutable` flag.
False: do not permit any output to be aliased to the input False: do not permit any output to be aliased to the input
False: do not permit any output to be aliased to the input
:param strict: False -> function arguments may be copied or cast to match the :param strict: False -> function arguments may be copied or cast to match the
type required by the parameter `variable`. type required by the parameter `variable`.
True -> function arguments must exactly match the type True -> function arguments must exactly match the type
......
...@@ -615,7 +615,7 @@ def guess_n_streams(size, warn=True): ...@@ -615,7 +615,7 @@ def guess_n_streams(size, warn=True):
for s in size: for s in size:
r *= s r *= s
if r > 6: if r > 6:
r = r//6 # chosen as fastest for rbm_benchmark r = r // 6 # chosen as fastest for rbm_benchmark
# The purpose of sampling from many streams is to be able to use # The purpose of sampling from many streams is to be able to use
# the GPU to its full capacity. It just wastes RAM and stream-initialization time to # the GPU to its full capacity. It just wastes RAM and stream-initialization time to
...@@ -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
......
...@@ -1773,20 +1773,13 @@ class Remove0Tester(utt.InferShapeTester): ...@@ -1773,20 +1773,13 @@ class Remove0Tester(utt.InferShapeTester):
if theano.config.mode not in ['FAST_COMPILE']: if theano.config.mode not in ['FAST_COMPILE']:
# list of apply nodes in the optimized graph. # list of apply nodes in the optimized graph.
nodes = f.maker.fgraph.toposort() nodes = f.maker.fgraph.toposort()
v = [True for node in nodes] # Check there isn't any Remove0 instance not inplace.
# In python 3, list comprehention variables do not leak assert not any([isinstance(node.op, Remove0) and
# in the outside scope, so we bind node varible below not node.op.inplace for node in nodes]), (
# to make the code behave the same under all 'Inplace optimization should have been applied')
# versions. However, the logic here does not look # Check there is at least one Remove0 inplace.
# right: the length of v is always the same as that of assert any([isinstance(node.op, Remove0) and node.op.inplace
# nodes and the only result of the assert is to check for node in nodes])
# that nodes is not empty. The intent was probably to
# keep if clause inside the [] and check every node.
node = nodes[-1]
if isinstance(node.op, Remove0) and node.op.inplace:
assert len(v), \
'Inplacing optimization should have been applied.'
# checking # checking
# makes sense to change its name # makes sense to change its name
target = mat target = mat
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论