Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2176d21b
提交
2176d21b
authored
12月 14, 2015
作者:
Amjad Almahairi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
start impl
上级
e9c56c39
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
112 行增加
和
0 行删除
+112
-0
multinomial.py
theano/sandbox/multinomial.py
+66
-0
rng_mrg.py
theano/sandbox/rng_mrg.py
+46
-0
没有找到文件。
theano/sandbox/multinomial.py
浏览文件 @
2176d21b
...
@@ -192,6 +192,72 @@ class MultinomialFromUniform(Op):
...
@@ -192,6 +192,72 @@ class MultinomialFromUniform(Op):
z
[
0
][
n
,
m
]
+=
1
z
[
0
][
n
,
m
]
+=
1
break
break
class
WeightedSelectionFromUniform
(
Op
):
"""
Converts samples from a uniform into sample from a multinomial.
"""
__props__
=
(
"odtype"
,)
def
__init__
(
self
,
odtype
):
self
.
odtype
=
odtype
def
__str__
(
self
):
return
'
%
s{
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
odtype
)
def
__setstate__
(
self
,
dct
):
self
.
__dict__
.
update
(
dct
)
try
:
self
.
odtype
except
AttributeError
:
self
.
odtype
=
'auto'
def
make_node
(
self
,
pvals
,
unis
,
n
=
1
):
pvals
=
T
.
as_tensor_variable
(
pvals
)
unis
=
T
.
as_tensor_variable
(
unis
)
if
pvals
.
ndim
!=
2
:
raise
NotImplementedError
(
'pvals ndim should be 2'
,
pvals
.
ndim
)
if
unis
.
ndim
!=
1
:
raise
NotImplementedError
(
'unis ndim should be 1'
,
unis
.
ndim
)
if
self
.
odtype
==
'auto'
:
odtype
=
pvals
.
dtype
else
:
odtype
=
self
.
odtype
out
=
T
.
tensor
(
dtype
=
odtype
,
broadcastable
=
pvals
.
type
.
broadcastable
)
return
Apply
(
self
,
[
pvals
,
unis
,
as_scalar
(
n
)],
[
out
])
def
grad
(
self
,
ins
,
outgrads
):
pvals
,
unis
,
n
=
ins
(
gz
,)
=
outgrads
return
[
T
.
zeros_like
(
x
)
for
x
in
ins
]
def
perform
(
self
,
node
,
ins
,
outs
):
(
pvals
,
unis
,
n_samples
)
=
ins
(
z
,)
=
outs
if
unis
.
shape
[
0
]
!=
pvals
.
shape
[
0
]
*
n_samples
:
raise
ValueError
(
"unis.shape[0] != pvals.shape[0] * n_samples"
,
unis
.
shape
[
0
],
pvals
.
shape
[
0
],
n_samples
)
if
z
[
0
]
is
None
or
numpy
.
any
(
z
[
0
]
.
shape
!=
[
pvals
.
shape
[
0
],
n_samples
]):
z
[
0
]
=
numpy
.
zeros
((
pvals
.
shape
[
0
],
n_samples
),
dtype
=
node
.
outputs
[
0
]
.
dtype
)
nb_multi
=
pvals
.
shape
[
0
]
nb_outcomes
=
pvals
.
shape
[
1
]
# For each multinomial, loop over each possible outcome,
# and set selected pval to 0 after being selected
for
c
in
range
(
n_samples
):
for
n
in
range
(
nb_multi
):
cummul
=
0
unis_n
=
unis
[
c
*
nb_multi
+
n
]
for
m
in
range
(
nb_outcomes
):
cummul
+=
pvals
[
n
,
m
]
if
(
cummul
>
unis_n
):
z
[
0
][
n
,
c
]
=
m
# set to zero so that it's not selected again
pvals
[
n
,
m
]
=
0.
class
GpuMultinomialFromUniform
(
MultinomialFromUniform
,
GpuOp
):
class
GpuMultinomialFromUniform
(
MultinomialFromUniform
,
GpuOp
):
"""
"""
...
...
theano/sandbox/rng_mrg.py
浏览文件 @
2176d21b
...
@@ -1363,6 +1363,52 @@ class MRG_RandomStreams(object):
...
@@ -1363,6 +1363,52 @@ class MRG_RandomStreams(object):
raise
NotImplementedError
((
"MRG_RandomStreams.multinomial only"
raise
NotImplementedError
((
"MRG_RandomStreams.multinomial only"
" implemented for pvals.ndim = 2"
))
" implemented for pvals.ndim = 2"
))
def
weighted_selection
(
self
,
size
=
None
,
n
=
1
,
pvals
=
None
,
ndim
=
None
,
dtype
=
'int64'
,
nstreams
=
None
):
"""
Sample `n` times (`n` needs to be in [1, m], where m is pvals.shape[1], default 1)
*WITHOUT replacement* from a multinomial distribution defined by probabilities pvals.
Example : WRITEME
Notes
-----
-`size` and `ndim` are only there keep the same signature as other
uniform, binomial, normal, etc.
TODO : adapt multinomial to take that into account
-Does not do any value checking on pvals, i.e. there is no
check that the elements are non-negative, less than 1, or
sum to 1. passing pvals = [[-2., 2.]] will result in
sampling [[0, 0]]
"""
if
pvals
is
None
:
raise
TypeError
(
"You have to specify pvals"
)
pvals
=
as_tensor_variable
(
pvals
)
if
n
>
pvals
.
shape
[
1
]:
raise
ValueError
(
"Cannot sample without replacement n samples bigger "
"than the size of the distribution."
)
if
size
is
not
None
:
raise
ValueError
(
"Provided a size argument to "
"MRG_RandomStreams.weighted_selection, which does not use "
"the size argument."
)
if
ndim
is
not
None
:
raise
ValueError
(
"Provided an ndim argument to "
"MRG_RandomStreams.weighted_selection, which does not use "
"the ndim argument."
)
if
pvals
.
ndim
==
2
:
# size = [pvals.shape[0], as_tensor_variable(n)]
size
=
pvals
[:,
0
]
.
shape
*
n
unis
=
self
.
uniform
(
size
=
size
,
ndim
=
1
,
nstreams
=
nstreams
)
op
=
multinomial
.
WeightedSelectionFromUniform
(
dtype
)
n_samples
=
as_tensor_variable
(
n
)
return
op
(
pvals
,
unis
,
n_samples
)
else
:
raise
NotImplementedError
((
"MRG_RandomStreams.weighted_selection only"
" implemented for pvals.ndim = 2"
))
def
normal
(
self
,
size
,
avg
=
0.0
,
std
=
1.0
,
ndim
=
None
,
def
normal
(
self
,
size
,
avg
=
0.0
,
std
=
1.0
,
ndim
=
None
,
dtype
=
None
,
nstreams
=
None
):
dtype
=
None
,
nstreams
=
None
):
"""
"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论