提交 85438346 authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Merge pull request #552 from nouiz/err_size

Make MRG random generator raise an error when there is bad size gived.
...@@ -32,6 +32,8 @@ New Features ...@@ -32,6 +32,8 @@ New Features
* If you use Enthought Python Distribution (EPD) now we use its blas * If you use Enthought Python Distribution (EPD) now we use its blas
implementation by default (tested on Linux and Windows) implementation by default (tested on Linux and Windows)
(Frederic B., Simon McGregor) (Frederic B., Simon McGregor)
* MRG random now raises an error with a clear message when the passed shape
contains dimensions with bad value like 0. (Frédéric B. reported by Ian G.)
Sparse Sandbox graduate Sparse Sandbox graduate
* Remove0 op: it removes stored elements with value 0. (Frederic B.) * Remove0 op: it removes stored elements with value 0. (Frederic B.)
......
...@@ -357,7 +357,7 @@ Then in your ``~/.emacs`` file, add this: ...@@ -357,7 +357,7 @@ Then in your ``~/.emacs`` file, add this:
;; Next two lines are the checks to do. You can add more if you wish. ;; Next two lines are the checks to do. You can add more if you wish.
(epy-setup-checker "pyflakes %f") ;; For python syntax check (epy-setup-checker "pyflakes %f") ;; For python syntax check
(epy-setup-checker "pep8 %f") ;; For pep8 check (epy-setup-checker "pep8 -r %f") ;; For pep8 check
......
...@@ -14,7 +14,7 @@ from theano.tensor import (raw_random, TensorType, as_tensor_variable, ...@@ -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.tensor import zeros_like, sqrt, log, sin, cos, join, prod
from theano.compile import optdb from theano.compile import optdb
from theano.gof import local_optimizer from theano.gof import local_optimizer
from theano.gof.python25 import all from theano.gof.python25 import all, any
import multinomial import multinomial
...@@ -730,6 +730,11 @@ class MRG_RandomStreams(object): ...@@ -730,6 +730,11 @@ class MRG_RandomStreams(object):
msg = "size must be a tuple of int or a Theano variable" msg = "size must be a tuple of int or a Theano variable"
assert all([isinstance(i,int) or isinstance(i,Variable) assert all([isinstance(i,int) or isinstance(i,Variable)
for i in size]), msg for i in size]), msg
if any([isinstance(i, int) and i <= 0 for i in size]):
raise ValueError(
"The specified size contains a dimension with value <= 0",
size)
else: else:
msg = "size must be a tuple of int or a Theano variable" msg = "size must be a tuple of int or a Theano variable"
assert isinstance(size, Variable) and size.ndim==1, msg assert isinstance(size, Variable) and size.ndim==1, msg
...@@ -786,8 +791,8 @@ class MRG_RandomStreams(object): ...@@ -786,8 +791,8 @@ class MRG_RandomStreams(object):
Sample `n` (currently `n` needs to be 1) times from a multinomial Sample `n` (currently `n` needs to be 1) times from a multinomial
distribution defined by probabilities pvals. distribution defined by probabilities pvals.
Example : pvals = [[.98,.01, .01], [.01, .98 .01]] will probably result Example : pvals = [[.98, .01, .01], [.01, .98, .01]] will
in [[1,0,0],[0,1,0]]. probably result in [[1,0,0],[0,1,0]].
.. note:: .. note::
`size` and `ndim` are only there keep the same signature as other `size` and `ndim` are only there keep the same signature as other
...@@ -797,6 +802,12 @@ class MRG_RandomStreams(object): ...@@ -797,6 +802,12 @@ class MRG_RandomStreams(object):
if pvals is None: if pvals is None:
raise TypeError("You have to specify pvals") raise TypeError("You have to specify pvals")
pvals = as_tensor_variable(pvals) pvals = as_tensor_variable(pvals)
if size is not None:
if any([isinstance(i, int) and i <= 0 for i in size]):
raise ValueError(
"The specified size contains a dimension with value <= 0",
size)
if n == 1 and pvals.ndim == 2: if n == 1 and pvals.ndim == 2:
ndim, size, bcast = raw_random._infer_ndim_bcast( ndim, size, bcast = raw_random._infer_ndim_bcast(
ndim, size, pvals[:,0]) ndim, size, pvals[:,0])
......
...@@ -597,3 +597,20 @@ def test_multinomial(): ...@@ -597,3 +597,20 @@ def test_multinomial():
sys.stdout.flush() sys.stdout.flush()
basic_multinomialtest(f, steps, sample_size, pvals, prefix='gpu mrg ') basic_multinomialtest(f, steps, sample_size, pvals, prefix='gpu mrg ')
numpy.testing.assert_array_almost_equal(out, gpu_out, decimal=6) 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 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论