Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7461eaab
提交
7461eaab
authored
6月 28, 2010
作者:
Simon Lemieux
浏览文件
操作
浏览文件
下载
差异文件
merge
上级
29bea05f
6bd132c5
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
33 行增加
和
17 行删除
+33
-17
multinomial.py
theano/sandbox/multinomial.py
+29
-13
rng_mrg.py
theano/sandbox/rng_mrg.py
+3
-4
test_rng_mrg.py
theano/sandbox/test_rng_mrg.py
+1
-0
没有找到文件。
theano/sandbox/multinomial.py
浏览文件 @
7461eaab
...
@@ -4,7 +4,7 @@ import theano.tensor as T
...
@@ -4,7 +4,7 @@ import theano.tensor as T
from
theano.tensor.opt
import
register_specialize
from
theano.tensor.opt
import
register_specialize
from
theano.gof
import
local_optimizer
from
theano.gof
import
local_optimizer
from
theano.sandbox.cuda
import
cuda_available
from
theano.sandbox.cuda
import
cuda_available
,
cuda_enabled
if
cuda_available
:
if
cuda_available
:
from
theano.sandbox.cuda
import
CudaNdarrayType
from
theano.sandbox.cuda
import
CudaNdarrayType
from
theano.sandbox.cuda.basic_ops
import
host_from_gpu
,
gpu_from_host
from
theano.sandbox.cuda.basic_ops
import
host_from_gpu
,
gpu_from_host
...
@@ -109,12 +109,11 @@ class GpuMultinomial(Multinomial):
...
@@ -109,12 +109,11 @@ class GpuMultinomial(Multinomial):
raise
TypeError
(
'pvals must be cudandarray'
,
pvals
)
raise
TypeError
(
'pvals must be cudandarray'
,
pvals
)
if
not
isinstance
(
unis
.
type
,
CudaNdarrayType
):
if
not
isinstance
(
unis
.
type
,
CudaNdarrayType
):
raise
TypeError
(
'unis must be cudandarray'
,
unis
)
raise
TypeError
(
'unis must be cudandarray'
,
unis
)
return
Apply
(
self
,
[
pvals
,
unis
],
[
pvals
.
type
()])
return
Apply
(
self
,
[
pvals
,
unis
],
[
pvals
.
type
()])
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
#
return ()
return
()
return
(
super
(
GpuMultinomial
,
self
)
.
c_code_cache_version
(),
1
)
#
return (super(GpuMultinomial,self).c_code_cache_version(),1)
def
c_support_code_apply
(
self
,
node
,
nodename
):
def
c_support_code_apply
(
self
,
node
,
nodename
):
return
"""
return
"""
...
@@ -128,7 +127,7 @@ class GpuMultinomial(Multinomial):
...
@@ -128,7 +127,7 @@ class GpuMultinomial(Multinomial):
float * global_outs
float * global_outs
)
)
{
{
int n =
32
*blockIdx.x + threadIdx.x;
int n =
blockDim.x
*blockIdx.x + threadIdx.x;
if (n < nb_multi)
if (n < nb_multi)
{
{
...
@@ -201,14 +200,31 @@ class GpuMultinomial(Multinomial):
...
@@ -201,14 +200,31 @@ class GpuMultinomial(Multinomial):
int nb_outcomes = CudaNdarray_HOST_DIMS(
%(z)
s)[0];
int nb_outcomes = CudaNdarray_HOST_DIMS(
%(z)
s)[0];
int nb_multi = CudaNdarray_HOST_DIMS(
%(z)
s)[1];
int nb_multi = CudaNdarray_HOST_DIMS(
%(z)
s)[1];
int nb_block;
//TODO : change this for a beautiful constant
if (nb_multi
%% 32
== 0)
int max_nb_blocks = 2<<15 - 1;
nb_block = nb_multi/32;
int nb_blocks = max_nb_blocks + 1;
else
int nb_threads=16; // so it really starts at 32, because of the *2
nb_block = (int)((float)nb_multi/32. + 1.);
do
{
nb_threads*=2;
if (nb_multi
%%
nb_threads == 0)
nb_blocks = nb_multi/nb_threads;
else
nb_blocks = (int)((float)nb_multi/(float)nb_threads + 1.);
} while (nb_blocks > max_nb_blocks);
//printf("
\\
nN=
%%
i b=
%%
i t=
%%
i t*b=
%%
i", nb_multi, nb_blocks, nb_threads, nb_blocks*nb_threads);
// TODO : next line is a bit hardcoded...
if (nb_threads > 512)
{
PyErr_Format(PyExc_ValueError, "Mutinomial is not implemented for as many rows in the matrix (
%%
i)", nb_multi);
%(fail)
s;
}
dim3 n_blocks(nb_block,1,1);
dim3 n_blocks(nb_block
s
,1,1);
dim3 n_threads(
32
,1,1);
dim3 n_threads(
nb_threads
,1,1);
int n_shared = 0;
int n_shared = 0;
k_multi_warp_
%(name)
s<<<n_blocks, n_threads, n_shared>>>(
k_multi_warp_
%(name)
s<<<n_blocks, n_threads, n_shared>>>(
...
@@ -244,6 +260,6 @@ gpu_multinomial = GpuMultinomial()
...
@@ -244,6 +260,6 @@ gpu_multinomial = GpuMultinomial()
def
use_gpu_multinomial
(
node
):
def
use_gpu_multinomial
(
node
):
if
node
.
op
==
multinomial
:
if
node
.
op
==
multinomial
:
return
[
host_from_gpu
(
gpu_multinomial
(
*
[
gpu_from_host
(
i
)
for
i
in
node
.
inputs
]))]
return
[
host_from_gpu
(
gpu_multinomial
(
*
[
gpu_from_host
(
i
)
for
i
in
node
.
inputs
]))]
if
theano
.
config
.
device
.
startswith
(
'gpu'
):
if
cuda_enabled
:
#
theano.config.device.startswith('gpu'):
register_specialize
(
use_gpu_multinomial
)
register_specialize
(
use_gpu_multinomial
)
theano/sandbox/rng_mrg.py
浏览文件 @
7461eaab
...
@@ -685,7 +685,7 @@ class MRG_RandomStreams(object):
...
@@ -685,7 +685,7 @@ class MRG_RandomStreams(object):
else
:
else
:
raise
NotImplementedError
(
"MRG_RandomStreams.binomial with n > 1"
)
raise
NotImplementedError
(
"MRG_RandomStreams.binomial with n > 1"
)
def
multinomial
(
self
,
size
=
None
,
n
=
1
,
pvals
=
[[
.
5
,
.
5
]]
,
ndim
=
None
,
dtype
=
'int64'
):
def
multinomial
(
self
,
size
=
None
,
n
=
1
,
pvals
=
None
,
ndim
=
None
,
dtype
=
'int64'
):
"""
"""
Sample `n` (currently `n` needs to be 1) times from a multinomial distribution defined by
Sample `n` (currently `n` needs to be 1) times from a multinomial distribution defined by
probabilities pvals.
probabilities pvals.
...
@@ -696,13 +696,12 @@ class MRG_RandomStreams(object):
...
@@ -696,13 +696,12 @@ class MRG_RandomStreams(object):
`size` and `ndim` are only there keep the same signature as other uniform, binomial, normal, etc.
`size` and `ndim` are only there keep the same signature as other uniform, binomial, normal, etc.
todo : adapt multinomial to take that into account
todo : adapt multinomial to take that into account
"""
"""
if
pvals
is
None
:
raise
TypeError
(
"You have to specify pvals"
)
pvals
=
as_tensor_variable
(
pvals
)
pvals
=
as_tensor_variable
(
pvals
)
if
n
==
1
and
pvals
.
ndim
==
2
:
if
n
==
1
and
pvals
.
ndim
==
2
:
pvals
=
as_tensor_variable
(
pvals
)
unis
=
self
.
uniform
(
size
=
pvals
.
shape
[
0
:
1
],
ndim
=
1
)
unis
=
self
.
uniform
(
size
=
pvals
.
shape
[
0
:
1
],
ndim
=
1
)
return
cast
(
multinomial
(
pvals
.
T
,
unis
)
.
T
,
dtype
)
return
cast
(
multinomial
(
pvals
.
T
,
unis
)
.
T
,
dtype
)
else
:
else
:
raise
NotImplementedError
(
"MRG_RandomStreams.multinomial only implemented with n == 1 and pvals.ndim = 2"
)
raise
NotImplementedError
(
"MRG_RandomStreams.multinomial only implemented with n == 1 and pvals.ndim = 2"
)
...
...
theano/sandbox/test_rng_mrg.py
浏览文件 @
7461eaab
...
@@ -528,6 +528,7 @@ def test_multinomial():
...
@@ -528,6 +528,7 @@ def test_multinomial():
print
''
print
''
print
'ON GPU:'
print
'ON GPU:'
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
True
)
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
True
)
pvals
=
numpy
.
asarray
(
pvals
,
dtype
=
'float32'
)
n
=
R
.
multinomial
(
pvals
=
pvals
,
dtype
=
'float32'
)
n
=
R
.
multinomial
(
pvals
=
pvals
,
dtype
=
'float32'
)
assert
n
.
dtype
==
'float32'
#well, it's really that this test w GPU doesn't make sense otw
assert
n
.
dtype
==
'float32'
#well, it's really that this test w GPU doesn't make sense otw
f
=
theano
.
function
([],
theano
.
Out
(
f
=
theano
.
function
([],
theano
.
Out
(
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论