提交 2ef495d9 authored 作者: Frederic's avatar Frederic

pep8

上级 401e2a9b
"""Define RandomStreams, providing random number variables for Theano graphs.""" """Define RandomStreams, providing random number variables for Theano
graphs.
"""
__docformat__ = "restructuredtext en" __docformat__ = "restructuredtext en"
import sys import sys
...@@ -8,6 +11,7 @@ from theano.compile import module, In, Component ...@@ -8,6 +11,7 @@ from theano.compile import module, In, Component
from theano.gof import Container from theano.gof import Container
from theano.tensor import raw_random from theano.tensor import raw_random
class RandomStreamsInstance(object): class RandomStreamsInstance(object):
"""RandomStreamsInstance""" """RandomStreamsInstance"""
def __init__(self, random_streams, memo, default_seed): def __init__(self, random_streams, memo, default_seed):
...@@ -18,44 +22,51 @@ class RandomStreamsInstance(object): ...@@ -18,44 +22,51 @@ class RandomStreamsInstance(object):
def initialize(self, seed=None): def initialize(self, seed=None):
"""Initialize each random stream """Initialize each random stream
:param seed: each random stream will be assigned a unique state that depends :param seed: each random stream will be assigned a unique
deterministically on this value. state that depends deterministically on this value.
:type seed: None or integer in range 0 to 2**30 :type seed: None or integer in range 0 to 2**30
:rtype: None :rtype: None
""" """
self.seed(seed) self.seed(seed)
def seed(self, seed=None): def seed(self, seed=None):
"""Re-initialize each random stream """Re-initialize each random stream
:param seed: each random stream will be assigned a unique state that depends :param seed: each random stream will be assigned a unique
deterministically on this value. state that depends deterministically on this value.
:type seed: None or integer in range 0 to 2**30 :type seed: None or integer in range 0 to 2**30
:rtype: None :rtype: None
""" """
if seed is None: if seed is None:
seed = self.default_seed seed = self.default_seed
#backport #backport
#seed = self.default_seed if seed is None else seed #seed = self.default_seed if seed is None else seed
seedgen = numpy.random.RandomState(seed) seedgen = numpy.random.RandomState(seed)
for old_r, new_r in self.random_streams.random_state_variables: for old_r, new_r in self.random_streams.random_state_variables:
old_r_seed = seedgen.randint(2**30) old_r_seed = seedgen.randint(2 ** 30)
old_r_container = self.memo[old_r].value old_r_container = self.memo[old_r].value
if old_r_container.value is None: if old_r_container.value is None:
#the cast to int here makes it work on 32bit machines, not sure why #the cast to int here makes it work on 32bit machines,
old_r_container.value = numpy.random.RandomState(int(old_r_seed)) #not sure why
old_r_container.value = numpy.random.RandomState(
int(old_r_seed))
else: else:
#the cast to int here makes it work on 32bit machines, not sure why #the cast to int here makes it work on 32bit machines,
#not sure why
old_r_container.value.seed(int(old_r_seed)) old_r_container.value.seed(int(old_r_seed))
def __getitem__(self, item): def __getitem__(self, item):
"""Retrieve the numpy RandomState instance associated with a particular stream """Retrieve the numpy RandomState instance associated with a
particular stream
:param item: a variable of type RandomStateType, associated with this RandomStream :param item: a variable of type RandomStateType, associated
with this RandomStream
:rtype: numpy RandomState (or None, before initialize) :rtype: numpy RandomState (or None, before initialize)
...@@ -67,9 +78,11 @@ class RandomStreamsInstance(object): ...@@ -67,9 +78,11 @@ class RandomStreamsInstance(object):
raise KeyError(item) raise KeyError(item)
def __setitem__(self, item, val): def __setitem__(self, item, val):
"""Set the numpy RandomState instance associated with a particular stream """Set the numpy RandomState instance associated with a
particular stream
:param item: a variable of type RandomStateType, associated with this RandomStream :param item: a variable of type RandomStateType, associated
with this RandomStream
:param val: the new value :param val: the new value
:type val: numpy RandomState :type val: numpy RandomState
...@@ -78,7 +91,8 @@ class RandomStreamsInstance(object): ...@@ -78,7 +91,8 @@ class RandomStreamsInstance(object):
""" """
if type(val) is not numpy.random.RandomState: if type(val) is not numpy.random.RandomState:
raise TypeError('only values of type RandomState are permitted', val) raise TypeError('only values of type RandomState are permitted',
val)
for old_r, new_r in self.random_streams.random_state_variables: for old_r, new_r in self.random_streams.random_state_variables:
if item is old_r: if item is old_r:
container = self.memo[item].value container = self.memo[item].value
...@@ -86,24 +100,34 @@ class RandomStreamsInstance(object): ...@@ -86,24 +100,34 @@ class RandomStreamsInstance(object):
return return
raise KeyError(item) raise KeyError(item)
class RandomStreams(Component, raw_random.RandomStreamsBase): class RandomStreams(Component, raw_random.RandomStreamsBase):
"""Module component with similar interface to numpy.random (numpy.random.RandomState)""" """Module component with similar interface to numpy.random
(numpy.random.RandomState)
"""
random_state_variables = [] random_state_variables = []
"""A list of pairs of the form (input_r, output_r). This will be over-ridden by the module """A list of pairs of the form (input_r, output_r). This will be
instance to contain stream generators. over-ridden by the module instance to contain stream
generators.
""" """
default_instance_seed = None default_instance_seed = None
"""Instance variable should take None or integer value. Used to seed the random number """Instance variable should take None or integer value. Used to
generator that provides seeds for member streams""" seed the random number generator that provides seeds for member
streams
"""
def __init__(self, seed=None): def __init__(self, seed=None):
""" """:type seed: None or int
:type seed: None or int
:param seed: a default seed to initialize the RandomState
instances after build. See `RandomStreamsInstance.__init__`
for more details.
:param seed: a default seed to initialize the RandomState instances after build. See
`RandomStreamsInstance.__init__` for more details.
""" """
super(RandomStreams, self).__init__() super(RandomStreams, self).__init__()
self.random_state_variables = [] self.random_state_variables = []
...@@ -115,7 +139,7 @@ class RandomStreams(Component, raw_random.RandomStreamsBase): ...@@ -115,7 +139,7 @@ class RandomStreams(Component, raw_random.RandomStreamsBase):
if old_r in memo: if old_r in memo:
assert memo[old_r].update is new_r assert memo[old_r].update is new_r
else: else:
memo[old_r] = In(old_r, memo[old_r] = In(old_r,
value=Container(old_r, storage=[None]), value=Container(old_r, storage=[None]),
update=new_r, update=new_r,
mutable=True) mutable=True)
...@@ -124,26 +148,28 @@ class RandomStreams(Component, raw_random.RandomStreamsBase): ...@@ -124,26 +148,28 @@ class RandomStreams(Component, raw_random.RandomStreamsBase):
"""override `Component.build` """ """override `Component.build` """
if self not in memo: if self not in memo:
print 'creating RandomStreamsInstance' print 'creating RandomStreamsInstance'
memo[self] = RandomStreamsInstance(self, memo, self.default_instance_seed) memo[self] = RandomStreamsInstance(self, memo,
self.default_instance_seed)
return memo[self] return memo[self]
def gen(self, op, *args, **kwargs): def gen(self, op, *args, **kwargs):
"""Create a new random stream in this container. """Create a new random stream in this container.
:param op: a RandomFunction instance to :param op: a RandomFunction instance to
:param args: interpreted by `op` :param args: interpreted by `op`
:param kwargs: interpreted by `op` :param kwargs: interpreted by `op`
:returns: The symbolic random draw part of op()'s return value. This function stores :returns: The symbolic random draw part of op()'s return
the updated RandomStateType Variable for use at `build` time. value. This function stores the updated RandomStateType
Variable for use at `build` time.
:rtype: TensorVariable :rtype: TensorVariable
""" """
random_state_variable = raw_random.random_state_type() random_state_variable = raw_random.random_state_type()
new_r, out = op(random_state_variable, *args, **kwargs) new_r, out = op(random_state_variable, *args, **kwargs)
out.rng = random_state_variable out.rng = random_state_variable
self.random_state_variables.append((random_state_variable, new_r)) self.random_state_variables.append((random_state_variable, new_r))
return out return out
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论