Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
1923c879
提交
1923c879
authored
9月 10, 2013
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pep8
上级
27f6c59e
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
22 行增加
和
11 行删除
+22
-11
multinomial.py
theano/sandbox/multinomial.py
+22
-11
没有找到文件。
theano/sandbox/multinomial.py
浏览文件 @
1923c879
...
@@ -12,22 +12,28 @@ if cuda_available:
...
@@ -12,22 +12,28 @@ if cuda_available:
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
from
theano.sandbox.cuda.opt
import
register_opt
from
theano.sandbox.cuda.opt
import
register_opt
class
MultinomialFromUniform
(
Op
):
class
MultinomialFromUniform
(
Op
):
'''Converts samples from a uniform into sample from a multinomial.'''
'''Converts samples from a uniform into sample from a multinomial.'''
def
__init__
(
self
,
odtype
):
def
__init__
(
self
,
odtype
):
self
.
odtype
=
odtype
self
.
odtype
=
odtype
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
and
self
.
odtype
==
other
.
odtype
return
type
(
self
)
==
type
(
other
)
and
self
.
odtype
==
other
.
odtype
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
((
type
(
self
),
self
.
odtype
))
return
hash
((
type
(
self
),
self
.
odtype
))
def
__str__
(
self
):
def
__str__
(
self
):
return
'
%
s{
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
odtype
)
return
'
%
s{
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
odtype
)
def
__setstate__
(
self
,
dct
):
def
__setstate__
(
self
,
dct
):
self
.
__dict__
.
update
(
dct
)
self
.
__dict__
.
update
(
dct
)
try
:
try
:
self
.
odtype
self
.
odtype
except
AttributeError
:
except
AttributeError
:
self
.
odtype
=
'auto'
self
.
odtype
=
'auto'
def
make_node
(
self
,
pvals
,
unis
):
def
make_node
(
self
,
pvals
,
unis
):
pvals
=
T
.
as_tensor_variable
(
pvals
)
pvals
=
T
.
as_tensor_variable
(
pvals
)
unis
=
T
.
as_tensor_variable
(
unis
)
unis
=
T
.
as_tensor_variable
(
unis
)
...
@@ -35,7 +41,7 @@ class MultinomialFromUniform(Op):
...
@@ -35,7 +41,7 @@ class MultinomialFromUniform(Op):
raise
NotImplementedError
(
'pvals ndim should be 2'
,
pvals
.
ndim
)
raise
NotImplementedError
(
'pvals ndim should be 2'
,
pvals
.
ndim
)
if
unis
.
ndim
!=
1
:
if
unis
.
ndim
!=
1
:
raise
NotImplementedError
(
'unis ndim should be 1'
,
unis
.
ndim
)
raise
NotImplementedError
(
'unis ndim should be 1'
,
unis
.
ndim
)
if
self
.
odtype
==
'auto'
:
if
self
.
odtype
==
'auto'
:
odtype
=
pvals
.
dtype
odtype
=
pvals
.
dtype
else
:
else
:
odtype
=
self
.
odtype
odtype
=
self
.
odtype
...
@@ -121,6 +127,7 @@ class MultinomialFromUniform(Op):
...
@@ -121,6 +127,7 @@ class MultinomialFromUniform(Op):
}
}
} // END NESTED SCOPE
} // END NESTED SCOPE
"""
%
locals
()
"""
%
locals
()
def
perform
(
self
,
node
,
ins
,
outs
):
def
perform
(
self
,
node
,
ins
,
outs
):
(
pvals
,
unis
)
=
ins
(
pvals
,
unis
)
=
ins
(
z
,)
=
outs
(
z
,)
=
outs
...
@@ -165,7 +172,7 @@ class GpuMultinomialFromUniform(MultinomialFromUniform, GpuOp):
...
@@ -165,7 +172,7 @@ class GpuMultinomialFromUniform(MultinomialFromUniform, GpuOp):
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
)
if
self
.
odtype
==
'auto'
:
if
self
.
odtype
==
'auto'
:
odtype
=
pvals
.
dtype
odtype
=
pvals
.
dtype
else
:
else
:
odtype
=
self
.
odtype
odtype
=
self
.
odtype
...
@@ -226,7 +233,6 @@ class GpuMultinomialFromUniform(MultinomialFromUniform, GpuOp):
...
@@ -226,7 +233,6 @@ class GpuMultinomialFromUniform(MultinomialFromUniform, GpuOp):
"""
%
locals
()
"""
%
locals
()
def
c_code
(
self
,
node
,
name
,
ins
,
outs
,
sub
):
def
c_code
(
self
,
node
,
name
,
ins
,
outs
,
sub
):
(
pvals
,
unis
)
=
ins
(
pvals
,
unis
)
=
ins
(
z
,)
=
outs
(
z
,)
=
outs
...
@@ -327,25 +333,30 @@ class GpuMultinomialFromUniform(MultinomialFromUniform, GpuOp):
...
@@ -327,25 +333,30 @@ class GpuMultinomialFromUniform(MultinomialFromUniform, GpuOp):
} // END NESTED SCOPE
} // END NESTED SCOPE
"""
%
locals
()
"""
%
locals
()
@local_optimizer
()
@local_optimizer
()
def
local_gpu_multinomial
(
node
):
def
local_gpu_multinomial
(
node
):
if
type
(
node
.
op
)
is
MultinomialFromUniform
:
if
type
(
node
.
op
)
is
MultinomialFromUniform
:
p
,
u
=
node
.
inputs
p
,
u
=
node
.
inputs
m
,
=
node
.
outputs
m
,
=
node
.
outputs
if
(
p
.
dtype
==
u
.
dtype
==
m
.
dtype
==
'float32'
and
if
(
p
.
dtype
==
u
.
dtype
==
m
.
dtype
==
'float32'
and
any
([
i
.
owner
and
isinstance
(
i
.
owner
.
op
,
theano
.
sandbox
.
cuda
.
HostFromGpu
)
any
([
i
.
owner
and
isinstance
(
i
.
owner
.
op
,
theano
.
sandbox
.
cuda
.
HostFromGpu
)
for
i
in
node
.
inputs
])):
for
i
in
node
.
inputs
])):
gpu_op
=
GpuMultinomialFromUniform
(
node
.
op
.
odtype
)
gpu_op
=
GpuMultinomialFromUniform
(
node
.
op
.
odtype
)
return
[
host_from_gpu
(
gpu_op
(
*
[
gpu_from_host
(
i
)
for
i
in
node
.
inputs
]))
.
T
]
return
[
host_from_gpu
(
gpu_op
(
*
[
gpu_from_host
(
i
)
for
i
in
node
.
inputs
]))
.
T
]
if
(
isinstance
(
node
.
op
,
theano
.
sandbox
.
cuda
.
GpuFromHost
)
and
if
(
isinstance
(
node
.
op
,
theano
.
sandbox
.
cuda
.
GpuFromHost
)
and
node
.
inputs
[
0
]
.
owner
and
type
(
node
.
inputs
[
0
]
.
owner
.
op
)
is
MultinomialFromUniform
):
node
.
inputs
[
0
]
.
owner
and
type
(
node
.
inputs
[
0
]
.
owner
.
op
)
is
MultinomialFromUniform
):
multi
=
node
.
inputs
[
0
]
.
owner
multi
=
node
.
inputs
[
0
]
.
owner
p
,
u
=
multi
.
inputs
p
,
u
=
multi
.
inputs
m
,
=
multi
.
outputs
m
,
=
multi
.
outputs
if
(
p
.
dtype
==
u
.
dtype
==
m
.
dtype
==
'float32'
):
if
(
p
.
dtype
==
u
.
dtype
==
m
.
dtype
==
'float32'
):
gpu_op
=
GpuMultinomialFromUniform
(
multi
.
op
.
odtype
)
gpu_op
=
GpuMultinomialFromUniform
(
multi
.
op
.
odtype
)
ret
=
gpu_op
(
*
[
gpu_from_host
(
i
)
for
i
in
multi
.
inputs
])
.
T
ret
=
gpu_op
(
*
[
gpu_from_host
(
i
)
for
i
in
multi
.
inputs
])
.
T
# The dimshuffle is on the cpu, but will be moved to the gpu by an opt.
# The dimshuffle is on the cpu, but will be moved to the
# gpu by an opt.
return
[
gpu_from_host
(
ret
)]
return
[
gpu_from_host
(
ret
)]
if
cuda_available
:
if
cuda_available
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论