Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7197ebcd
提交
7197ebcd
authored
12月 21, 2015
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pep8
上级
a1304650
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
23 行增加
和
20 行删除
+23
-20
test_multinomial.py
theano/sandbox/tests/test_multinomial.py
+23
-19
test_flake8.py
theano/tests/test_flake8.py
+0
-1
没有找到文件。
theano/sandbox/tests/test_multinomial.py
浏览文件 @
7197ebcd
...
...
@@ -8,11 +8,11 @@ from theano.sandbox import multinomial
from
theano.compile.mode
import
get_default_mode
,
predefined_linkers
import
theano.sandbox.cuda
as
cuda
import
theano.tests.unittest_tools
as
utt
import
six.moves.cPickle
as
pickle
import
os
from
theano.compat
import
PY3
from
theano.misc.pkl_utils
import
CompatUnpickler
def
get_mode
(
gpu
):
mode
=
get_default_mode
()
mode
=
copy
.
copy
(
mode
)
...
...
@@ -37,14 +37,14 @@ def test_n_samples_1():
u
=
tensor
.
fvector
()
n
=
tensor
.
iscalar
()
m
=
multinomial
.
MultinomialFromUniform
(
'auto'
)(
p
,
u
,
n
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
numpy
.
random
.
seed
(
12345
)
for
i
in
[
1
,
5
,
10
,
100
,
1000
,
10000
]:
uni
=
numpy
.
random
.
rand
(
2
*
i
)
.
astype
(
config
.
floatX
)
uni
=
numpy
.
random
.
rand
(
2
*
i
)
.
astype
(
config
.
floatX
)
res
=
f
([[
1.0
,
0.0
],
[
0.0
,
1.0
]],
uni
,
i
)
utt
.
assert_allclose
(
res
,
[[
i
*
1.0
,
0.0
],
[
0.0
,
i
*
1.0
]])
utt
.
assert_allclose
(
res
,
[[
i
*
1.0
,
0.0
],
[
0.0
,
i
*
1.0
]])
def
test_n_samples_2
():
...
...
@@ -52,24 +52,26 @@ def test_n_samples_2():
u
=
tensor
.
fvector
()
n
=
tensor
.
iscalar
()
m
=
multinomial
.
MultinomialFromUniform
(
'auto'
)(
p
,
u
,
n
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
numpy
.
random
.
seed
(
12345
)
for
i
in
[
1
,
5
,
10
,
100
,
1000
]:
uni
=
numpy
.
random
.
rand
(
i
)
.
astype
(
config
.
floatX
)
pvals
=
numpy
.
random
.
randint
(
1
,
1000
,(
1
,
1000
))
.
astype
(
config
.
floatX
)
pvals
=
numpy
.
random
.
randint
(
1
,
1000
,
(
1
,
1000
))
.
astype
(
config
.
floatX
)
pvals
/=
pvals
.
sum
(
1
)
res
=
f
(
pvals
,
uni
,
i
)
assert
res
.
sum
()
==
i
for
i
in
[
1
,
5
,
10
,
100
,
1000
]:
uni
=
numpy
.
random
.
rand
(
i
)
.
astype
(
config
.
floatX
)
pvals
=
numpy
.
random
.
randint
(
1
,
1000000
,(
1
,
1000000
))
.
astype
(
config
.
floatX
)
pvals
=
numpy
.
random
.
randint
(
1
,
1000000
,
(
1
,
1000000
))
.
astype
(
config
.
floatX
)
pvals
/=
pvals
.
sum
(
1
)
res
=
f
(
pvals
,
uni
,
i
)
assert
res
.
sum
()
==
i
def
test_n_samples_compatibility
():
"""
This test checks if the new change to MultinomialFromUniform is still compatible
...
...
@@ -83,28 +85,30 @@ def test_n_samples_compatibility():
pickle.dump([X, samples], open("multinomial_test_graph.pkl", "w"))
"""
folder
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
with
open
(
os
.
path
.
join
(
folder
,
"multinomial_test_graph.pkl"
),
"rb"
)
as
pkl_file
:
with
open
(
os
.
path
.
join
(
folder
,
"multinomial_test_graph.pkl"
),
"rb"
)
as
pkl_file
:
if
PY3
:
u
=
CompatUnpickler
(
pkl_file
,
encoding
=
"latin1"
)
else
:
u
=
CompatUnpickler
(
pkl_file
)
X
,
samples
=
u
.
load
()
f
=
theano
.
function
([
X
],
samples
)
res
=
f
(
numpy
.
random
.
randn
(
20
,
10
))
res
=
f
(
numpy
.
random
.
randn
(
20
,
10
))
assert
numpy
.
all
(
res
.
sum
(
axis
=
1
)
==
1
)
def
test_multinomial_0
():
# This tests the MultinomialFromUniform Op directly, not going through the
# multinomial() call in GPU random generation.
p
=
tensor
.
fmatrix
()
u
=
tensor
.
fvector
()
m
=
multinomial
.
MultinomialFromUniform
(
'auto'
)(
p
,
u
)
def
body
(
mode
,
gpu
):
# the m*2 allows the multinomial to reuse output
f
=
function
([
p
,
u
],
m
*
2
,
allow_input_downcast
=
True
,
mode
=
mode
)
f
=
function
([
p
,
u
],
m
*
2
,
allow_input_downcast
=
True
,
mode
=
mode
)
if
gpu
:
assert
any
([
type
(
node
.
op
)
is
multinomial
.
GpuMultinomialFromUniform
...
...
@@ -112,7 +116,7 @@ def test_multinomial_0():
# test that both first and second samples can be drawn
utt
.
assert_allclose
(
f
([[
1
,
0
],
[
0
,
1
]],
[
.
1
,
.
1
]),
[[
2
,
0
],
[
0
,
2
]])
[[
2
,
0
],
[
0
,
2
]])
# test that both second labels can be drawn
r
=
f
([[
.
2
,
.
8
],
[
.
3
,
.
7
]],
[
.
31
,
.
31
])
...
...
@@ -140,12 +144,12 @@ def test_multinomial_large():
p
=
tensor
.
fmatrix
()
u
=
tensor
.
fvector
()
m
=
multinomial
.
MultinomialFromUniform
(
'auto'
)(
p
,
u
)
f
=
function
([
p
,
u
],
m
*
2
,
allow_input_downcast
=
True
,
mode
=
mode
)
f
=
function
([
p
,
u
],
m
*
2
,
allow_input_downcast
=
True
,
mode
=
mode
)
if
gpu
:
assert
any
([
type
(
node
.
op
)
is
multinomial
.
GpuMultinomialFromUniform
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
pval
=
numpy
.
arange
(
10000
*
4
,
dtype
=
'float32'
)
.
reshape
((
10000
,
4
))
+
0.1
pval
=
numpy
.
arange
(
10000
*
4
,
dtype
=
'float32'
)
.
reshape
((
10000
,
4
))
+
0.1
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
uval
=
numpy
.
ones_like
(
pval
[:,
0
])
*
0.5
mval
=
f
(
pval
,
uval
)
...
...
@@ -160,7 +164,7 @@ def test_multinomial_large():
else
:
raise
NotImplementedError
(
config
.
cast_policy
)
utt
.
assert_allclose
(
mval
.
sum
(
axis
=
1
),
2
)
asdf
=
numpy
.
asarray
([
0
,
0
,
2
,
0
])
+
0
*
pval
asdf
=
numpy
.
asarray
([
0
,
0
,
2
,
0
])
+
0
*
pval
utt
.
assert_allclose
(
mval
,
asdf
)
# broadcast over all rows
run_with_c
(
body
)
if
cuda
.
cuda_available
:
...
...
@@ -201,10 +205,10 @@ def test_gpu_opt():
f
=
function
([
p
,
u
],
m_gpu
,
allow_input_downcast
=
True
,
mode
=
get_mode
(
True
))
assert
any
([
type
(
node
.
op
)
is
multinomial
.
GpuMultinomialFromUniform
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
pval
=
numpy
.
arange
(
10000
*
4
,
dtype
=
'float32'
)
.
reshape
((
10000
,
4
))
+
0.1
pval
=
numpy
.
arange
(
10000
*
4
,
dtype
=
'float32'
)
.
reshape
((
10000
,
4
))
+
0.1
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
uval
=
numpy
.
ones_like
(
pval
[:,
0
])
*
0.5
mval
=
f
(
pval
,
uval
)
f
(
pval
,
uval
)
# Test with a row, it was failing in the past.
r
=
tensor
.
frow
()
...
...
@@ -215,7 +219,7 @@ def test_gpu_opt():
f
=
function
([
r
,
u
],
m_gpu
,
allow_input_downcast
=
True
,
mode
=
get_mode
(
True
))
assert
any
([
type
(
node
.
op
)
is
multinomial
.
GpuMultinomialFromUniform
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
pval
=
numpy
.
arange
(
1
*
4
,
dtype
=
'float32'
)
.
reshape
((
1
,
4
))
+
0.1
pval
=
numpy
.
arange
(
1
*
4
,
dtype
=
'float32'
)
.
reshape
((
1
,
4
))
+
0.1
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
uval
=
numpy
.
ones_like
(
pval
[:,
0
])
*
0.5
mval2
=
f
(
pval
,
uval
)
f
(
pval
,
uval
)
theano/tests/test_flake8.py
浏览文件 @
7197ebcd
...
...
@@ -104,7 +104,6 @@ whitelist_flake8 = [
"sandbox/tests/test_scan.py"
,
"sandbox/tests/test_rng_mrg.py"
,
"sandbox/tests/test_neighbourhoods.py"
,
"sandbox/tests/test_multinomial.py"
,
"sandbox/tests/__init__.py"
,
"sandbox/cuda/var.py"
,
"sandbox/cuda/GpuConvGrad3D.py"
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论