提交 4f48b960 authored 作者: Olivier Breuleux's avatar Olivier Breuleux

moved uniform and binomial in tensor_random to RandomState class methods

上级 705dfaaf
...@@ -12,6 +12,7 @@ from gof import \ ...@@ -12,6 +12,7 @@ from gof import \
from compile import function, eval_outputs, fast_compute from compile import function, eval_outputs, fast_compute
import tensor import tensor
import tensor_random
import scalar import scalar
import sparse import sparse
import gradient import gradient
......
...@@ -14,31 +14,6 @@ class RandomState(object): ...@@ -14,31 +14,6 @@ class RandomState(object):
instances returned by gen(), gen_like() instances returned by gen(), gen_like()
@type seed: int @type seed: int
""" """
@staticmethod
def _fn_from_dist(dist, cache={}):
"""Return a function from a distribution description
@param dist: identifier of a sampling distribution.
@type dist: callable or str or tuple(str, dict)
@param cache: The optional cache argument implements a closure, which ensures that
multiple requests for the same sampling function will get the same
sampling function. L{NumpyGenerator}.__hash__ depends on this.
@type cache: dict
"""
if callable(dist):
return dist
if isinstance(dist, str):
return getattr(numpy.random.RandomState, dist)
name, kwargs = dist
key = (name, tuple(kwargs.items()))
if key not in cache:
fn = getattr(numpy.random.RandomState, name)
fn = functools.partial(fn, **kwargs)
cache[key] = fn
return cache[key]
def __init__(self, seed): def __init__(self, seed):
self.seed = seed self.seed = seed
...@@ -69,6 +44,54 @@ class RandomState(object): ...@@ -69,6 +44,54 @@ class RandomState(object):
fn = RandomState._fn_from_dist(dist) fn = RandomState._fn_from_dist(dist)
return NumpyGenerator(self.seed-1, x.type.ndim, fn)(tensor.shape(x)) return NumpyGenerator(self.seed-1, x.type.ndim, fn)(tensor.shape(x))
def uniform_like(self, template, low=0.,high=1.):
"""
Return a multivariate uniform(low,high)
random variable in a tensor of the same shape as template
(template can either be a tensor or a shape tuple). Each element of the
resulting tensor is sampled independently. low and high can
be scalars or have the same shape as the template (or broadcastable
to it).
"""
return self.gen_like(('uniform',{'low':low,'high':high}),template)
def binomial_like(self, template, n=1, p=0.5):
"""
Return a multivariate binomial(n,p) random variable in a tensor of the same shape as template
(template can either be a tensor or a shape tuple). Each element of the
resulting tensor is sampled independently. low and high can
be scalars or have the same shape as the template (or broadcastable
to it).
"""
return self.gen_like(('binomial',{'n':n,'p':p}),template)
@staticmethod
def _fn_from_dist(dist, cache={}):
"""Return a function from a distribution description
@param dist: identifier of a sampling distribution.
@type dist: callable or str or tuple(str, dict)
@param cache: The optional cache argument implements a closure, which ensures that
multiple requests for the same sampling function will get the same
sampling function. L{NumpyGenerator}.__hash__ depends on this.
@type cache: dict
"""
if callable(dist):
return dist
if isinstance(dist, str):
return getattr(numpy.random.RandomState, dist)
name, kwargs = dist
key = (name, tuple(kwargs.items()))
if key not in cache:
fn = getattr(numpy.random.RandomState, name)
fn = functools.partial(fn, **kwargs)
cache[key] = fn
return cache[key]
class NumpyGenerator(gof.op.Op): class NumpyGenerator(gof.op.Op):
"""Supply a sequence of random tensors of a given shape, from a given """Supply a sequence of random tensors of a given shape, from a given
distribution. distribution.
...@@ -120,26 +143,3 @@ class NumpyGenerator(gof.op.Op): ...@@ -120,26 +143,3 @@ class NumpyGenerator(gof.op.Op):
(shape, self.ndim) ) (shape, self.ndim) )
output_storage[0][0] = self.fn(rng, size=shape) output_storage[0][0] = self.fn(rng, size=shape)
def uniform(seed, template, low=0.,high=1.):
"""
Return a multivariate uniform(low,high)
random variable in a tensor of the same shape as template
(template can either be a tensor or a shape tuple). Each element of the
resulting tensor is sampled independently. low and high can
be scalars or have the same shape as the template (or broadcastable
to it).
"""
return RandomState(seed).gen_like(('uniform',{'low':low,'high':high}),template)
def binomial(seed, template, n=1, p=0.5):
"""
Return a multivariate binomial(n,p) random variable in a tensor of the same shape as template
(template can either be a tensor or a shape tuple). Each element of the
resulting tensor is sampled independently. low and high can
be scalars or have the same shape as the template (or broadcastable
to it).
"""
return RandomState(seed).gen_like(('binomial',{'n':n,'p':p}),template)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论