提交 401e2a9b authored 作者: Frederic's avatar Frederic

pep8

上级 4bfde33c
"""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 copy, sys import copy
import sys
import numpy import numpy
from theano.gof import Container from theano.gof import Container
from theano.compile.sharedvalue import SharedVariable, shared_constructor, shared from theano.compile.sharedvalue import (SharedVariable, shared_constructor,
shared)
import raw_random import raw_random
class RandomStateSharedVariable(SharedVariable): class RandomStateSharedVariable(SharedVariable):
pass pass
@shared_constructor @shared_constructor
def randomstate_constructor(value, name=None, strict=False, allow_downcast=None, borrow=False): def randomstate_constructor(value, name=None, strict=False,
allow_downcast=None, borrow=False):
"""SharedVariable Constructor for RandomState""" """SharedVariable Constructor for RandomState"""
if not isinstance(value, numpy.random.RandomState): if not isinstance(value, numpy.random.RandomState):
raise TypeError raise TypeError
...@@ -25,17 +33,26 @@ def randomstate_constructor(value, name=None, strict=False, allow_downcast=None, ...@@ -25,17 +33,26 @@ def randomstate_constructor(value, name=None, strict=False, allow_downcast=None,
strict=strict, strict=strict,
allow_downcast=allow_downcast) allow_downcast=allow_downcast)
class RandomStreams(raw_random.RandomStreamsBase): class RandomStreams(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)
"""
state_updates = [] state_updates = []
"""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
"""
gen_seedgen = None gen_seedgen = None
"""numpy.RandomState instance that gen() uses to seed new streams. """numpy.RandomState instance that gen() uses to seed new streams.
...@@ -48,8 +65,10 @@ class RandomStreams(raw_random.RandomStreamsBase): ...@@ -48,8 +65,10 @@ class RandomStreams(raw_random.RandomStreamsBase):
""" """
:type seed: None or int :type seed: None or int
:param seed: a default seed to initialize the RandomState instances after build. See :param seed: a default seed to initialize the RandomState
`RandomStreamsInstance.__init__` for more details. instances after build. See `RandomStreamsInstance.__init__`
for more details.
""" """
super(RandomStreams, self).__init__() super(RandomStreams, self).__init__()
self.state_updates = [] self.state_updates = []
...@@ -59,47 +78,54 @@ class RandomStreams(raw_random.RandomStreamsBase): ...@@ -59,47 +78,54 @@ class RandomStreams(raw_random.RandomStreamsBase):
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_instance_seed seed = self.default_instance_seed
seedgen = numpy.random.RandomState(seed) seedgen = numpy.random.RandomState(seed)
for old_r, new_r in self.state_updates: for old_r, new_r in self.state_updates:
old_r_seed = seedgen.randint(2**30) old_r_seed = seedgen.randint(2 ** 30)
old_r.set_value(numpy.random.RandomState(int(old_r_seed)), old_r.set_value(numpy.random.RandomState(int(old_r_seed)),
borrow=True) borrow=True)
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)
:note: This is kept for compatibility with `tensor.randomstreams.RandomStreams`. The :note: This is kept for compatibility with
simpler syntax ``item.rng.get_value()`` is also valid. `tensor.randomstreams.RandomStreams`. The simpler syntax
``item.rng.get_value()`` is also valid.
""" """
return item.get_value(borrow=True) return item.get_value(borrow=True)
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
:rtype: None :rtype: None
:note: This is kept for compatibility with `tensor.randomstreams.RandomStreams`. The :note: This is kept for compatibility with
simpler syntax ``item.rng.set_value(val)`` is also valid. `tensor.randomstreams.RandomStreams`. The simpler syntax
``item.rng.set_value(val)`` is also valid.
""" """
item.set_value(val, borrow=True) item.set_value(val, borrow=True)
...@@ -113,12 +139,14 @@ class RandomStreams(raw_random.RandomStreamsBase): ...@@ -113,12 +139,14 @@ class RandomStreams(raw_random.RandomStreamsBase):
: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
""" """
seed = int(self.gen_seedgen.randint(2**30)) seed = int(self.gen_seedgen.randint(2 ** 30))
random_state_variable = shared(numpy.random.RandomState(seed)) random_state_variable = shared(numpy.random.RandomState(seed))
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
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论