提交 7a80329c authored 作者: Frederic's avatar Frederic

Make MRG random generator raise an error when there is bad size gived.

上级 58c73c21
......@@ -32,6 +32,8 @@ New Features
* If you use Enthought Python Distribution (EPD) now we use its blas
implementation by default (tested on Linux and Windows)
(Frederic B., Simon McGregor)
* MRG random now raise an error with a clear message when the passed shape
contain dimensions with bad value like 0. (Frédéric B. reported by Ian G.)
Sparse Sandbox graduate
* Remove0 op: it removes stored elements with value 0. (Frederic B.)
......
......@@ -14,7 +14,7 @@ from theano.tensor import (raw_random, TensorType, as_tensor_variable,
from theano.tensor import zeros_like, sqrt, log, sin, cos, join, prod
from theano.compile import optdb
from theano.gof import local_optimizer
from theano.gof.python25 import all
from theano.gof.python25 import all, any
import multinomial
......@@ -730,6 +730,11 @@ class MRG_RandomStreams(object):
msg = "size must be a tuple of int or a Theano variable"
assert all([isinstance(i,int) or isinstance(i,Variable)
for i in size]), msg
if len([i for i in size if isinstance(i, int) and i <= 0]) > 0:
raise ValueError(
"The specified size contain a dimension with value <= 0",
size)
else:
msg = "size must be a tuple of int or a Theano variable"
assert isinstance(size, Variable) and size.ndim==1, msg
......@@ -786,8 +791,8 @@ class MRG_RandomStreams(object):
Sample `n` (currently `n` needs to be 1) times from a multinomial
distribution defined by probabilities pvals.
Example : pvals = [[.98,.01, .01], [.01, .98 .01]] will probably result
in [[1,0,0],[0,1,0]].
Example : pvals = [[.98, .01, .01], [.01, .98, .01]] will
probably result in [[1,0,0],[0,1,0]].
.. note::
`size` and `ndim` are only there keep the same signature as other
......@@ -797,6 +802,12 @@ class MRG_RandomStreams(object):
if pvals is None:
raise TypeError("You have to specify pvals")
pvals = as_tensor_variable(pvals)
if size is not None:
if any([i for i in size if not (isinstance(i, int) and i <= 0)]):
raise ValueError(
"The specified size contain a dimension with value <= 0",
size)
if n == 1 and pvals.ndim == 2:
ndim, size, bcast = raw_random._infer_ndim_bcast(
ndim, size, pvals[:,0])
......
......@@ -597,3 +597,20 @@ def test_multinomial():
sys.stdout.flush()
basic_multinomialtest(f, steps, sample_size, pvals, prefix='gpu mrg ')
numpy.testing.assert_array_almost_equal(out, gpu_out, decimal=6)
class T_MRG(unittest.TestCase):
def test_bad_size(self):
R = MRG_RandomStreams(234, use_cuda=False)
for size, var_input in [
((0, 100), []),
((-1, 100), []),
((1, 0), []),
]:
self.assertRaises(ValueError, R.uniform, size)
self.assertRaises(ValueError, R.binomial, size)
self.assertRaises(ValueError, R.multinomial, size, 1, [])
self.assertRaises(ValueError, R.normal, size)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论