提交 ba22be05 authored 作者: Yoshua Bengio's avatar Yoshua Bengio

Added uniform and binomial op-creating functions with examples in test.

上级 e284625c
......@@ -80,6 +80,22 @@ class T_Random(unittest.TestCase):
self.failUnless(d0.owner.op == d1.owner.op)
self.failUnless(hash(d0.owner.op) == hash(d1.owner.op))
def test6(self):
x = tensor.vector()
u = uniform(9999,x,0.,10.)
fu = compile.function([x],[u])
res1 = fu(numpy.zeros((3)))
res2 = fu(numpy.zeros((3)))
self.failUnless(str(res1[0]).startswith('8.23389'))
self.failUnless(str(res2[0]).startswith('5.45926'))
b = binomial(121212,x,1,0.8)
fb = compile.function([x],[b])
res1 = fb(numpy.zeros((10)))
res2 = fb(numpy.zeros((10)))
self.failUnless(list(res1) == [1,0,1,1,1,1,1,1,1,1])
self.failUnless(list(res2) == [1,1,0,1,1,1,0,0,1,1])
if __name__ == '__main__':
unittest.main()
......
......@@ -120,4 +120,26 @@ class NumpyGenerator(gof.op.Op):
(shape, self.ndim) )
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论