Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
64030399
提交
64030399
authored
3月 31, 2011
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Test multinomial on the gpu.
上级
8ad4181f
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
18 行增加
和
9 行删除
+18
-9
test_multinomial.py
theano/sandbox/test_multinomial.py
+18
-9
没有找到文件。
theano/sandbox/test_multinomial.py
浏览文件 @
64030399
...
@@ -4,31 +4,35 @@ import theano
...
@@ -4,31 +4,35 @@ import theano
from
theano
import
tensor
,
shared
,
function
from
theano
import
tensor
,
shared
,
function
import
multinomial
import
multinomial
from
theano.compile.mode
import
get_default_mode
,
predefined_linkers
from
theano.compile.mode
import
get_default_mode
,
predefined_linkers
import
theano.sandbox.cuda
as
cuda
def
run_with_c
(
f
):
def
run_with_c
(
f
,
gpu
=
False
):
mode
=
get_default_mode
()
mode
=
get_default_mode
()
linker_orig
=
mode
.
linker
linker_orig
=
mode
.
linker
if
linker_orig
==
predefined_linkers
[
'py'
]:
if
linker_orig
==
predefined_linkers
[
'py'
]:
mode
.
linker
=
predefined_linkers
[
'c|py'
]
mode
.
linker
=
predefined_linkers
[
'c|py'
]
if
gpu
:
mode
=
mode
.
including
(
'gpu'
)
try
:
try
:
f
(
mode
)
f
(
mode
,
gpu
)
finally
:
finally
:
mode
.
linker
=
linker_orig
mode
.
linker
=
linker_orig
def
test_multi
m
omial_0
():
def
test_multi
n
omial_0
():
# This tests the MultinomialFromUniform Op directly, not going through the
# This tests the MultinomialFromUniform Op directly, not going through the
# multinomial() call in GPU random generation.
# multinomial() call in GPU random generation.
p
=
tensor
.
matrix
()
p
=
tensor
.
f
matrix
()
u
=
tensor
.
vector
()
u
=
tensor
.
f
vector
()
m
=
multinomial
.
MultinomialFromUniform
(
'auto'
)(
p
,
u
)
m
=
multinomial
.
MultinomialFromUniform
(
'auto'
)(
p
,
u
)
def
body
(
mode
):
def
body
(
mode
,
gpu
):
#the m*2 allows the multinomial to reuse output
#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
for
node
in
f
.
maker
.
env
.
toposort
()])
# test that both first and second samples can be drawn
# test that both first and second samples can be drawn
assert
numpy
.
allclose
(
f
([[
1
,
0
],
[
0
,
1
]],
[
.
1
,
.
1
]),
assert
numpy
.
allclose
(
f
([[
1
,
0
],
[
0
,
1
]],
[
.
1
,
.
1
]),
...
@@ -50,16 +54,19 @@ def test_multimomial_0():
...
@@ -50,16 +54,19 @@ def test_multimomial_0():
assert
numpy
.
allclose
(
r
,
[[
0
,
2
]]),
r
assert
numpy
.
allclose
(
r
,
[[
0
,
2
]]),
r
run_with_c
(
body
)
run_with_c
(
body
)
if
cuda
.
cuda_available
:
run_with_c
(
body
,
True
)
#TODO: check a bigger example (make sure blocking on GPU is handled correctly)
#TODO: check a bigger example (make sure blocking on GPU is handled correctly)
def
test_multinomial_large
():
def
test_multinomial_large
():
# DEBUG_MODE will test this on GPU
# DEBUG_MODE will test this on GPU
def
body
(
mode
):
def
body
(
mode
,
gpu
):
p
=
tensor
.
fmatrix
()
p
=
tensor
.
fmatrix
()
u
=
tensor
.
fvector
()
u
=
tensor
.
fvector
()
m
=
multinomial
.
MultinomialFromUniform
(
'auto'
)(
p
,
u
)
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
.
env
.
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
]
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
...
@@ -72,6 +79,8 @@ def test_multinomial_large():
...
@@ -72,6 +79,8 @@ def test_multinomial_large():
asdf
=
numpy
.
asarray
([
0
,
0
,
2
,
0
])
+
0
*
pval
asdf
=
numpy
.
asarray
([
0
,
0
,
2
,
0
])
+
0
*
pval
assert
numpy
.
allclose
(
mval
,
asdf
)
#broadcast over all rows
assert
numpy
.
allclose
(
mval
,
asdf
)
#broadcast over all rows
run_with_c
(
body
)
run_with_c
(
body
)
if
cuda
.
cuda_available
:
run_with_c
(
body
,
True
)
def
test_multinomial_dtypes
():
def
test_multinomial_dtypes
():
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论