Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
246f740d
提交
246f740d
authored
1月 28, 2016
作者:
Amjad Almahairi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixed tests
上级
32549fe2
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
143 行增加
和
62 行删除
+143
-62
test_weighted_select.py
theano/sandbox/tests/test_weighted_select.py
+143
-62
没有找到文件。
theano/sandbox/tests/test_weighted_select.py
浏览文件 @
246f740d
import
numpy
import
theano
from
theano
import
config
,
function
,
tensor
from
theano.sandbox
import
multinomial
from
theano.sandbox.rng_mrg
import
MRG_RandomStreams
as
RandomStreams
import
unittest
def
test_select_distinct
():
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
=
1000
numpy
.
random
.
seed
(
12345
)
for
i
in
[
5
,
10
,
50
,
100
,
500
]:
uni
=
numpy
.
random
.
rand
(
i
)
.
astype
(
config
.
floatX
)
class
test_OP
(
unittest
.
TestCase
):
def
test_select_distinct
(
self
):
"""
Tests that WeightedSelectionFromUniform always selects distinct 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
=
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
/=
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_select_all
():
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
=
1000
numpy
.
random
.
seed
(
12345
)
for
_
in
range
(
100
):
uni
=
numpy
.
random
.
rand
(
n_elements
)
.
astype
(
config
.
floatX
)
self
.
assertRaises
(
ValueError
,
f
,
pvals
,
uni
,
n_selected
)
def
test_select_proportional_to_weight
(
self
):
"""
Tests that WeightedSelectionFromUniform selects elements, on average,
proportional to the their probabilities
"""
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
=
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
)
res
=
f
(
pvals
,
uni
,
n_elements
)
res
=
numpy
.
squeeze
(
res
)
assert
len
(
res
)
==
n_elements
assert
numpy
.
all
(
numpy
.
in1d
(
numpy
.
unique
(
res
),
res
)),
res
def
test_select_proportional_to_weight
():
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
=
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
):
uni
=
numpy
.
random
.
rand
(
n_selected
)
.
astype
(
config
.
floatX
)
res
=
f
(
pvals
,
uni
,
n_selected
)
res
=
numpy
.
squeeze
(
res
)
# print res
avg_pvals
[
res
]
+=
1
avg_pvals
/=
avg_pvals
.
sum
()
print
avg_pvals
print
numpy
.
squeeze
(
pvals
)
assert
numpy
.
mean
(
abs
(
avg_pvals
-
pvals
))
<
mean_rtol
avg_pvals
=
numpy
.
zeros
((
n_elements
,))
for
rep
in
range
(
1000
):
uni
=
numpy
.
random
.
rand
(
n_selected
)
.
astype
(
config
.
floatX
)
res
=
f
(
pvals
,
uni
,
n_selected
)
res
=
numpy
.
squeeze
(
res
)
avg_pvals
[
res
]
+=
1
avg_pvals
/=
avg_pvals
.
sum
()
assert
numpy
.
mean
(
abs
(
avg_pvals
-
pvals
))
<
mean_rtol
class
test_function
(
unittest
.
TestCase
):
def
test_select_distinct
(
self
):
"""
Tests that weighted_selection always selects distinct 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
=
1000
numpy
.
random
.
seed
(
12345
)
for
i
in
[
5
,
10
,
50
,
100
,
500
,
n_elements
]:
pvals
=
numpy
.
random
.
randint
(
1
,
100
,(
1
,
n_elements
))
.
astype
(
config
.
floatX
)
pvals
/=
pvals
.
sum
(
1
)
res
=
f
(
pvals
,
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 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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论