提交 246f740d authored 作者: Amjad Almahairi's avatar Amjad Almahairi

fixed tests

上级 32549fe2
import numpy import numpy
import theano
from theano import config, function, tensor from theano import config, function, tensor
from theano.sandbox import multinomial from theano.sandbox import multinomial
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
import unittest
def test_select_distinct(): class test_OP(unittest.TestCase):
p = tensor.fmatrix()
u = tensor.fvector() def test_select_distinct(self):
n = tensor.iscalar() """
m = multinomial.WeightedSelectionFromUniform('auto')(p, u, n) Tests that WeightedSelectionFromUniform always selects distinct elements
"""
f = function([p, u, n], m, allow_input_downcast=True) p = tensor.fmatrix()
u = tensor.fvector()
n_elements = 1000 n = tensor.iscalar()
numpy.random.seed(12345) m = multinomial.WeightedSelectionFromUniform('auto')(p, u, n)
for i in [5, 10, 50, 100, 500]:
uni = numpy.random.rand(i).astype(config.floatX) f = function([p, u, n], m, allow_input_downcast=True)
n_elements = 1000
numpy.random.seed(12345)
for i in [5, 10, 50, 100, 500, n_elements]:
uni = numpy.random.rand(i).astype(config.floatX)
pvals = numpy.random.randint(1,100,(1,n_elements)).astype(config.floatX)
pvals /= pvals.sum(1)
res = f(pvals, uni, i)
res = numpy.squeeze(res)
assert len(res) == i
assert numpy.all(numpy.in1d(numpy.unique(res), res)), res
def test_fail_select_alot(self):
"""
Tests that WeightedSelectionFromUniform fails when asked to sample more
elements than the actual number of elements
"""
p = tensor.fmatrix()
u = tensor.fvector()
n = tensor.iscalar()
m = multinomial.WeightedSelectionFromUniform('auto')(p, u, n)
f = function([p, u, n], m, allow_input_downcast=True)
n_elements = 100
n_selected = 200
numpy.random.seed(12345)
uni = numpy.random.rand(n_selected).astype(config.floatX)
pvals = numpy.random.randint(1,100,(1,n_elements)).astype(config.floatX) pvals = numpy.random.randint(1,100,(1,n_elements)).astype(config.floatX)
pvals /= pvals.sum(1) pvals /= pvals.sum(1)
res = f(pvals, uni, i) self.assertRaises(ValueError, f, pvals, uni, n_selected)
res = numpy.squeeze(res)
assert len(res) == i def test_select_proportional_to_weight(self):
assert numpy.all(numpy.in1d(numpy.unique(res), res)), res """
Tests that WeightedSelectionFromUniform selects elements, on average,
def test_select_all(): proportional to the their probabilities
p = tensor.fmatrix() """
u = tensor.fvector() p = tensor.fmatrix()
n = tensor.iscalar() u = tensor.fvector()
m = multinomial.WeightedSelectionFromUniform('auto')(p, u, n) n = tensor.iscalar()
m = multinomial.WeightedSelectionFromUniform('auto')(p, u, n)
f = function([p, u, n], m, allow_input_downcast=True)
f = function([p, u, n], m, allow_input_downcast=True)
n_elements = 1000
numpy.random.seed(12345) n_elements = 100
for _ in range(100): n_selected = 10
uni = numpy.random.rand(n_elements).astype(config.floatX) mean_rtol = 0.04
numpy.random.seed(12345)
pvals = numpy.random.randint(1,100,(1,n_elements)).astype(config.floatX) pvals = numpy.random.randint(1,100,(1,n_elements)).astype(config.floatX)
pvals /= pvals.sum(1) pvals /= pvals.sum(1)
res = f(pvals, uni, n_elements) avg_pvals = numpy.zeros((n_elements,))
res = numpy.squeeze(res)
assert len(res) == n_elements for rep in range(1000):
assert numpy.all(numpy.in1d(numpy.unique(res), res)), res uni = numpy.random.rand(n_selected).astype(config.floatX)
res = f(pvals, uni, n_selected)
res = numpy.squeeze(res)
def test_select_proportional_to_weight(): avg_pvals[res] += 1
p = tensor.fmatrix() avg_pvals /= avg_pvals.sum()
u = tensor.fvector() assert numpy.mean(abs(avg_pvals - pvals)) < mean_rtol
n = tensor.iscalar()
m = multinomial.WeightedSelectionFromUniform('auto')(p, u, n)
class test_function(unittest.TestCase):
f = function([p, u, n], m, allow_input_downcast=True)
def test_select_distinct(self):
n_elements = 100 """
n_selected = 10 Tests that weighted_selection always selects distinct elements
mean_rtol = 0.04 """
numpy.random.seed(12345) th_rng = RandomStreams(12345)
pvals = numpy.random.randint(1,100,(1,n_elements)).astype(config.floatX)
pvals /= pvals.sum(1) p = tensor.fmatrix()
avg_pvals = numpy.zeros((n_elements,)) n = tensor.iscalar()
m = th_rng.weighted_selection(pvals=p, n=n)
for rep in range(1000):
uni = numpy.random.rand(n_selected).astype(config.floatX) f = function([p, n], m, allow_input_downcast=True)
res = f(pvals, uni, n_selected)
res = numpy.squeeze(res) n_elements = 1000
# print res numpy.random.seed(12345)
avg_pvals[res] += 1 for i in [5, 10, 50, 100, 500, n_elements]:
avg_pvals /= avg_pvals.sum() pvals = numpy.random.randint(1,100,(1,n_elements)).astype(config.floatX)
pvals /= pvals.sum(1)
print avg_pvals res = f(pvals, i)
print numpy.squeeze(pvals) res = numpy.squeeze(res)
assert numpy.mean(abs(avg_pvals - pvals)) < mean_rtol assert len(res) == i
assert numpy.all(numpy.in1d(numpy.unique(res), res)), res
def test_fail_select_alot(self):
"""
Tests that weighted_selection fails when asked to sample more
elements than the actual number of elements
"""
th_rng = RandomStreams(12345)
p = tensor.fmatrix()
n = tensor.iscalar()
m = th_rng.weighted_selection(pvals=p, n=n)
f = function([p, n], m, allow_input_downcast=True)
n_elements = 100
n_selected = 200
numpy.random.seed(12345)
pvals = numpy.random.randint(1,100,(1,n_elements)).astype(config.floatX)
pvals /= pvals.sum(1)
self.assertRaises(ValueError, f, pvals, n_selected)
def test_select_proportional_to_weight(self):
"""
Tests that weighted_selection selects elements, on average,
proportional to the their probabilities
"""
th_rng = RandomStreams(12345)
p = tensor.fmatrix()
n = tensor.iscalar()
m = th_rng.weighted_selection(pvals=p, n=n)
f = function([p, n], m, allow_input_downcast=True)
n_elements = 100
n_selected = 10
mean_rtol = 0.04
numpy.random.seed(12345)
pvals = numpy.random.randint(1,100,(1,n_elements)).astype(config.floatX)
pvals /= pvals.sum(1)
avg_pvals = numpy.zeros((n_elements,))
for rep in range(1000):
res = f(pvals, n_selected)
res = numpy.squeeze(res)
avg_pvals[res] += 1
avg_pvals /= avg_pvals.sum()
assert numpy.mean(abs(avg_pvals - pvals)) < mean_rtol
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论