Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b6dccdbf
提交
b6dccdbf
authored
3月 22, 2017
作者:
Laurent Dinh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MultinomialWOReplacementFromUniform := ChoiceFromUniform
上级
fcbb3e9b
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
105 行增加
和
23 行删除
+105
-23
multinomial.py
theano/gpuarray/multinomial.py
+14
-4
test_gpuarray_multinomial_wo_replacement.pkl
...uarray/tests/test_gpuarray_multinomial_wo_replacement.pkl
+17
-0
test_multinomial.py
theano/gpuarray/tests/test_multinomial.py
+25
-11
multinomial.py
theano/sandbox/multinomial.py
+11
-1
rng_mrg.py
theano/sandbox/rng_mrg.py
+1
-1
test_multinomial_wo_replacement.py
theano/sandbox/tests/test_multinomial_wo_replacement.py
+20
-6
test_sandbox_multinomial_wo_replacement.pkl
...sandbox/tests/test_sandbox_multinomial_wo_replacement.pkl
+17
-0
没有找到文件。
theano/gpuarray/multinomial.py
浏览文件 @
b6dccdbf
# TODO test dtype != float32
# TODO test dtype != float32
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
os
import
os
import
warnings
try
:
try
:
import
pygpu
import
pygpu
...
@@ -232,7 +233,7 @@ KERNEL void k_multi_warp_multinomial(
...
@@ -232,7 +233,7 @@ KERNEL void k_multi_warp_multinomial(
return
(
3
,)
return
(
3
,)
class
GPUA
MultinomialWOReplacement
FromUniform
(
GpuKernelBase
,
Op
):
class
GPUA
Choice
FromUniform
(
GpuKernelBase
,
Op
):
"""
"""
The output is transposed compared to MultinomialWOReplacementFromUniform.
The output is transposed compared to MultinomialWOReplacementFromUniform.
We must insert a Transpose op after it.
We must insert a Transpose op after it.
...
@@ -506,13 +507,22 @@ def local_gpua_multinomial(op, context_name, inputs, outputs):
...
@@ -506,13 +507,22 @@ def local_gpua_multinomial(op, context_name, inputs, outputs):
@register_opt
(
'fast_compile'
)
@register_opt
(
'fast_compile'
)
@op_lifter
([
theano
.
sandbox
.
multinomial
.
MultinomialWOReplacement
FromUniform
])
@op_lifter
([
theano
.
sandbox
.
multinomial
.
Choice
FromUniform
])
@register_opt2
([
theano
.
sandbox
.
multinomial
.
MultinomialWOReplacement
FromUniform
],
'fast_compile'
)
@register_opt2
([
theano
.
sandbox
.
multinomial
.
Choice
FromUniform
],
'fast_compile'
)
def
local_gpua_multinomial_wor
(
op
,
context_name
,
inputs
,
outputs
):
def
local_gpua_multinomial_wor
(
op
,
context_name
,
inputs
,
outputs
):
# TODO : need description for function
# TODO : need description for function
p
,
u
,
n
=
inputs
p
,
u
,
n
=
inputs
m
,
=
outputs
m
,
=
outputs
if
((
p
.
dtype
==
u
.
dtype
==
'float32'
)
and
(
m
.
dtype
==
'int64'
)):
if
((
p
.
dtype
==
u
.
dtype
==
'float32'
)
and
(
m
.
dtype
==
'int64'
)):
gpu_op
=
GPUA
MultinomialWOReplacement
FromUniform
(
op
.
odtype
)
gpu_op
=
GPUA
Choice
FromUniform
(
op
.
odtype
)
return
GpuDimShuffle
([
False
,
False
],
[
1
,
0
])(
return
GpuDimShuffle
([
False
,
False
],
[
1
,
0
])(
gpu_op
(
p
,
u
,
n
))
gpu_op
(
p
,
u
,
n
))
class
GPUAMultinomialWOReplacementFromUniform
(
GPUAChoiceFromUniform
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
warnings
.
warn
(
"GPUAMultinomialWOReplacementFromUniform is deprecated, "
"use GPUAChoiceFromUniform instead."
,
DeprecationWarning
,
stacklevel
=
2
)
super
(
GPUAMultinomialWOReplacementFromUniform
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
theano/gpuarray/tests/test_gpuarray_multinomial_wo_replacement.pkl
0 → 100644
浏览文件 @
b6dccdbf
ccopy_reg
_reconstructor
p1
(ctheano.gpuarray.multinomial
GPUAMultinomialWOReplacementFromUniform
p2
c__builtin__
object
p3
NtRp4
(dp5
S'odtype'
p6
S'float32'
p7
sb.
\ No newline at end of file
theano/gpuarray/tests/test_multinomial.py
浏览文件 @
b6dccdbf
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
os
import
numpy
as
np
import
numpy
as
np
import
unittest
import
unittest
import
theano
import
theano
from
theano
import
config
,
function
,
tensor
from
theano
import
config
,
function
,
tensor
from
theano.compat
import
PY3
from
theano.misc.pkl_utils
import
CompatUnpickler
from
theano.sandbox
import
multinomial
from
theano.sandbox
import
multinomial
from
theano.sandbox.rng_mrg
import
MRG_RandomStreams
as
RandomStreams
from
theano.sandbox.rng_mrg
import
MRG_RandomStreams
as
RandomStreams
...
@@ -13,7 +16,7 @@ import theano.tests.unittest_tools as utt
...
@@ -13,7 +16,7 @@ import theano.tests.unittest_tools as utt
from
.config
import
mode_with_gpu
from
.config
import
mode_with_gpu
from
..multinomial
import
(
GPUAMultinomialFromUniform
,
from
..multinomial
import
(
GPUAMultinomialFromUniform
,
GPUA
MultinomialWOReplacement
FromUniform
)
GPUA
Choice
FromUniform
)
def
test_multinomial_output_dtype
():
def
test_multinomial_output_dtype
():
...
@@ -172,12 +175,12 @@ class test_OP_wor(unittest.TestCase):
...
@@ -172,12 +175,12 @@ class test_OP_wor(unittest.TestCase):
def
test_select_distinct
(
self
):
def
test_select_distinct
(
self
):
"""
"""
Tests that
MultinomialWOReplacement
FromUniform always selects distinct elements
Tests that
Choice
FromUniform always selects distinct elements
"""
"""
p
=
tensor
.
fmatrix
()
p
=
tensor
.
fmatrix
()
u
=
tensor
.
fvector
()
u
=
tensor
.
fvector
()
n
=
tensor
.
iscalar
()
n
=
tensor
.
iscalar
()
m
=
multinomial
.
MultinomialWOReplacement
FromUniform
(
'auto'
)(
p
,
u
,
n
)
m
=
multinomial
.
Choice
FromUniform
(
'auto'
)(
p
,
u
,
n
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
...
@@ -195,13 +198,13 @@ class test_OP_wor(unittest.TestCase):
...
@@ -195,13 +198,13 @@ class test_OP_wor(unittest.TestCase):
def
test_fail_select_alot
(
self
):
def
test_fail_select_alot
(
self
):
"""
"""
Tests that
MultinomialWOReplacement
FromUniform fails when asked to sample more
Tests that
Choice
FromUniform fails when asked to sample more
elements than the actual number of elements
elements than the actual number of elements
"""
"""
p
=
tensor
.
fmatrix
()
p
=
tensor
.
fmatrix
()
u
=
tensor
.
fvector
()
u
=
tensor
.
fvector
()
n
=
tensor
.
iscalar
()
n
=
tensor
.
iscalar
()
m
=
multinomial
.
MultinomialWOReplacement
FromUniform
(
'auto'
)(
p
,
u
,
n
)
m
=
multinomial
.
Choice
FromUniform
(
'auto'
)(
p
,
u
,
n
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
...
@@ -215,13 +218,13 @@ class test_OP_wor(unittest.TestCase):
...
@@ -215,13 +218,13 @@ class test_OP_wor(unittest.TestCase):
def
test_select_proportional_to_weight
(
self
):
def
test_select_proportional_to_weight
(
self
):
"""
"""
Tests that
MultinomialWOReplacement
FromUniform selects elements, on average,
Tests that
Choice
FromUniform selects elements, on average,
proportional to the their probabilities
proportional to the their probabilities
"""
"""
p
=
tensor
.
fmatrix
()
p
=
tensor
.
fmatrix
()
u
=
tensor
.
fvector
()
u
=
tensor
.
fvector
()
n
=
tensor
.
iscalar
()
n
=
tensor
.
iscalar
()
m
=
multinomial
.
MultinomialWOReplacement
FromUniform
(
'auto'
)(
p
,
u
,
n
)
m
=
multinomial
.
Choice
FromUniform
(
'auto'
)(
p
,
u
,
n
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
...
@@ -324,11 +327,11 @@ def test_gpu_opt_wor():
...
@@ -324,11 +327,11 @@ def test_gpu_opt_wor():
p
=
tensor
.
fmatrix
()
p
=
tensor
.
fmatrix
()
u
=
tensor
.
fvector
()
u
=
tensor
.
fvector
()
n
=
tensor
.
iscalar
()
n
=
tensor
.
iscalar
()
m
=
multinomial
.
MultinomialWOReplacement
FromUniform
(
'auto'
)(
p
,
u
,
n
)
m
=
multinomial
.
Choice
FromUniform
(
'auto'
)(
p
,
u
,
n
)
assert
m
.
dtype
==
'int64'
,
m
.
dtype
assert
m
.
dtype
==
'int64'
,
m
.
dtype
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
,
mode
=
mode_with_gpu
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
,
mode
=
mode_with_gpu
)
assert
any
([
type
(
node
.
op
)
is
GPUA
MultinomialWOReplacement
FromUniform
assert
any
([
type
(
node
.
op
)
is
GPUA
Choice
FromUniform
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
n_samples
=
3
n_samples
=
3
pval
=
np
.
arange
(
10000
*
4
,
dtype
=
'float32'
)
.
reshape
((
10000
,
4
))
+
0.1
pval
=
np
.
arange
(
10000
*
4
,
dtype
=
'float32'
)
.
reshape
((
10000
,
4
))
+
0.1
...
@@ -338,13 +341,24 @@ def test_gpu_opt_wor():
...
@@ -338,13 +341,24 @@ def test_gpu_opt_wor():
# Test with a row, it was failing in the past.
# Test with a row, it was failing in the past.
r
=
tensor
.
frow
()
r
=
tensor
.
frow
()
m
=
multinomial
.
MultinomialWOReplacement
FromUniform
(
'auto'
)(
r
,
u
,
n
)
m
=
multinomial
.
Choice
FromUniform
(
'auto'
)(
r
,
u
,
n
)
assert
m
.
dtype
==
'int64'
,
m
.
dtype
assert
m
.
dtype
==
'int64'
,
m
.
dtype
f
=
function
([
r
,
u
,
n
],
m
,
allow_input_downcast
=
True
,
mode
=
mode_with_gpu
)
f
=
function
([
r
,
u
,
n
],
m
,
allow_input_downcast
=
True
,
mode
=
mode_with_gpu
)
assert
any
([
type
(
node
.
op
)
is
GPUA
MultinomialWOReplacement
FromUniform
assert
any
([
type
(
node
.
op
)
is
GPUA
Choice
FromUniform
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
pval
=
np
.
arange
(
1
*
4
,
dtype
=
'float32'
)
.
reshape
((
1
,
4
))
+
0.1
pval
=
np
.
arange
(
1
*
4
,
dtype
=
'float32'
)
.
reshape
((
1
,
4
))
+
0.1
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
uval
=
np
.
ones_like
(
pval
[:,
0
])
*
0.5
uval
=
np
.
ones_like
(
pval
[:,
0
])
*
0.5
f
(
pval
,
uval
,
1
)
f
(
pval
,
uval
,
1
)
def
test_unpickle_legacy_op
():
testfile_dir
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
))
fname
=
'test_gpuarray_multinomial_wo_replacement.pkl'
if
not
PY3
:
with
open
(
os
.
path
.
join
(
testfile_dir
,
fname
),
'r'
)
as
fp
:
u
=
CompatUnpickler
(
fp
)
m
=
u
.
load
()
assert
isinstance
(
m
,
GPUAChoiceFromUniform
)
theano/sandbox/multinomial.py
浏览文件 @
b6dccdbf
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
import
warnings
import
theano
import
theano
from
theano
import
Op
,
Apply
from
theano
import
Op
,
Apply
...
@@ -211,7 +212,7 @@ class MultinomialFromUniform(Op):
...
@@ -211,7 +212,7 @@ class MultinomialFromUniform(Op):
z
[
0
][
n
,
numpy
.
searchsorted
(
cumsum
,
unis_n
)]
+=
1
z
[
0
][
n
,
numpy
.
searchsorted
(
cumsum
,
unis_n
)]
+=
1
class
MultinomialWOReplacement
FromUniform
(
MultinomialFromUniform
):
class
Choice
FromUniform
(
MultinomialFromUniform
):
"""
"""
Converts samples from a uniform into sample (without replacement) from a
Converts samples from a uniform into sample (without replacement) from a
multinomial.
multinomial.
...
@@ -626,3 +627,12 @@ def local_gpu_multinomial(node):
...
@@ -626,3 +627,12 @@ def local_gpu_multinomial(node):
# The dimshuffle is on the cpu, but will be moved to the
# The dimshuffle is on the cpu, but will be moved to the
# gpu by an opt.
# gpu by an opt.
return
[
gpu_from_host
(
ret
)]
return
[
gpu_from_host
(
ret
)]
class
MultinomialWOReplacementFromUniform
(
ChoiceFromUniform
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
warnings
.
warn
(
"MultinomialWOReplacementFromUniform is deprecated, "
"use ChoiceFromUniform instead."
,
DeprecationWarning
,
stacklevel
=
2
)
super
(
MultinomialWOReplacementFromUniform
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
theano/sandbox/rng_mrg.py
浏览文件 @
b6dccdbf
...
@@ -1514,7 +1514,7 @@ class MRG_RandomStreams(object):
...
@@ -1514,7 +1514,7 @@ class MRG_RandomStreams(object):
shape
=
p
[:,
0
]
.
shape
*
size
shape
=
p
[:,
0
]
.
shape
*
size
unis
=
self
.
uniform
(
size
=
shape
,
ndim
=
1
,
nstreams
=
nstreams
)
unis
=
self
.
uniform
(
size
=
shape
,
ndim
=
1
,
nstreams
=
nstreams
)
op
=
multinomial
.
MultinomialWOReplacement
FromUniform
(
dtype
)
op
=
multinomial
.
Choice
FromUniform
(
dtype
)
return
op
(
p
,
unis
,
as_tensor_variable
(
size
))
return
op
(
p
,
unis
,
as_tensor_variable
(
size
))
def
multinomial_wo_replacement
(
self
,
size
=
None
,
n
=
1
,
pvals
=
None
,
def
multinomial_wo_replacement
(
self
,
size
=
None
,
n
=
1
,
pvals
=
None
,
...
...
theano/sandbox/tests/test_multinomial_wo_replacement.py
浏览文件 @
b6dccdbf
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
import
os
from
theano
import
config
,
function
,
tensor
from
theano
import
config
,
function
,
tensor
from
theano.compat
import
PY3
from
theano.misc.pkl_utils
import
CompatUnpickler
from
theano.sandbox
import
multinomial
from
theano.sandbox
import
multinomial
from
theano.sandbox.rng_mrg
import
MRG_RandomStreams
as
RandomStreams
from
theano.sandbox.rng_mrg
import
MRG_RandomStreams
as
RandomStreams
import
unittest
import
unittest
...
@@ -10,12 +13,12 @@ class test_OP(unittest.TestCase):
...
@@ -10,12 +13,12 @@ class test_OP(unittest.TestCase):
def
test_select_distinct
(
self
):
def
test_select_distinct
(
self
):
"""
"""
Tests that
MultinomialWOReplacement
FromUniform always selects distinct elements
Tests that
Choice
FromUniform always selects distinct elements
"""
"""
p
=
tensor
.
fmatrix
()
p
=
tensor
.
fmatrix
()
u
=
tensor
.
fvector
()
u
=
tensor
.
fvector
()
n
=
tensor
.
iscalar
()
n
=
tensor
.
iscalar
()
m
=
multinomial
.
MultinomialWOReplacement
FromUniform
(
'auto'
)(
p
,
u
,
n
)
m
=
multinomial
.
Choice
FromUniform
(
'auto'
)(
p
,
u
,
n
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
...
@@ -43,13 +46,13 @@ class test_OP(unittest.TestCase):
...
@@ -43,13 +46,13 @@ class test_OP(unittest.TestCase):
def
test_fail_select_alot
(
self
):
def
test_fail_select_alot
(
self
):
"""
"""
Tests that
MultinomialWOReplacement
FromUniform fails when asked to sample more
Tests that
Choice
FromUniform fails when asked to sample more
elements than the actual number of elements
elements than the actual number of elements
"""
"""
p
=
tensor
.
fmatrix
()
p
=
tensor
.
fmatrix
()
u
=
tensor
.
fvector
()
u
=
tensor
.
fvector
()
n
=
tensor
.
iscalar
()
n
=
tensor
.
iscalar
()
m
=
multinomial
.
MultinomialWOReplacement
FromUniform
(
'auto'
)(
p
,
u
,
n
)
m
=
multinomial
.
Choice
FromUniform
(
'auto'
)(
p
,
u
,
n
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
...
@@ -63,13 +66,13 @@ class test_OP(unittest.TestCase):
...
@@ -63,13 +66,13 @@ class test_OP(unittest.TestCase):
def
test_select_proportional_to_weight
(
self
):
def
test_select_proportional_to_weight
(
self
):
"""
"""
Tests that
MultinomialWOReplacement
FromUniform selects elements, on average,
Tests that
Choice
FromUniform selects elements, on average,
proportional to the their probabilities
proportional to the their probabilities
"""
"""
p
=
tensor
.
fmatrix
()
p
=
tensor
.
fmatrix
()
u
=
tensor
.
fvector
()
u
=
tensor
.
fvector
()
n
=
tensor
.
iscalar
()
n
=
tensor
.
iscalar
()
m
=
multinomial
.
MultinomialWOReplacement
FromUniform
(
'auto'
)(
p
,
u
,
n
)
m
=
multinomial
.
Choice
FromUniform
(
'auto'
)(
p
,
u
,
n
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
...
@@ -164,3 +167,14 @@ class test_function(unittest.TestCase):
...
@@ -164,3 +167,14 @@ class test_function(unittest.TestCase):
avg_pvals
/=
avg_pvals
.
sum
()
avg_pvals
/=
avg_pvals
.
sum
()
avg_diff
=
numpy
.
mean
(
abs
(
avg_pvals
-
pvals
))
avg_diff
=
numpy
.
mean
(
abs
(
avg_pvals
-
pvals
))
assert
avg_diff
<
mean_rtol
assert
avg_diff
<
mean_rtol
def
test_unpickle_legacy_op
(
self
):
testfile_dir
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
))
fname
=
'test_sandbox_multinomial_wo_replacement.pkl'
if
not
PY3
:
with
open
(
os
.
path
.
join
(
testfile_dir
,
fname
),
'r'
)
as
fp
:
u
=
CompatUnpickler
(
fp
)
m
=
u
.
load
()
print
(
m
)
assert
isinstance
(
m
,
multinomial
.
ChoiceFromUniform
)
theano/sandbox/tests/test_sandbox_multinomial_wo_replacement.pkl
0 → 100644
浏览文件 @
b6dccdbf
ccopy_reg
_reconstructor
p1
(ctheano.sandbox.multinomial
MultinomialWOReplacementFromUniform
p2
c__builtin__
object
p3
NtRp4
(dp5
S'odtype'
p6
S'auto'
p7
sb.
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论