Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
68db88d1
提交
68db88d1
authored
1月 31, 2017
作者:
AdeB
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
change the mapping between multinomial_wo_replacement and choice
上级
e69d47e0
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
26 行增加
和
31 行删除
+26
-31
rng_mrg.py
theano/sandbox/rng_mrg.py
+26
-31
没有找到文件。
theano/sandbox/rng_mrg.py
浏览文件 @
68db88d1
...
@@ -1446,26 +1446,26 @@ class MRG_RandomStreams(object):
...
@@ -1446,26 +1446,26 @@ 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
choice
(
self
,
size
=
None
,
a
=
2
,
replace
=
True
,
p
=
None
,
ndim
=
None
,
def
choice
(
self
,
size
=
1
,
a
=
None
,
replace
=
True
,
p
=
None
,
ndim
=
None
,
dtype
=
'int64'
,
nstreams
=
None
):
dtype
=
'int64'
,
nstreams
=
None
):
"""
"""
Sample `size` times from a multinomial distribution defined by
Sample `size` times from a multinomial distribution defined by
probabilities `p` and values `a`.
probabilities `p`. Sampled values are between 0 and `p.shape[1]-1`.
Only sampling without replacement is implemented for now.
Parameters
Parameters
----------
----------
size: integer or None (default None)
size: integer or integer tensor (default 1)
the number of samples to be generated. If None, a single sample is
The number of samples. It should be between 1 and `p.shape[1]-1`
generated.
a: None
a: integer or 1d or 2d numpy array or theano tensor
For now, a should be None. This function will sample
the values of the samples. If a is an integer, the values are
values between 0 and `p.shape[1]-1`.
generated between 0 and a-1.
p: 1d or 2d numpy array or theano tensor
the probabilities of the distribution associated to each value of
`a`. It should have the same shape as a (if a is an array/tensor).
replace: bool (default True)
replace: bool (default True)
Whether the sample is with or without replacement.
Whether the sample is with or without replacement.
Only replace=False is implemented for now.
Only replace=False is implemented for now.
p: 2d numpy array or theano tensor
the probabilities of the distribution, corresponding to values
0 to `p.shape[1]-1`.
Example : p = [[.98, .01, .01], [.01, .49, .50]] and size=1 will
Example : p = [[.98, .01, .01], [.01, .49, .50]] and size=1 will
probably result in [[0],[2]]. When setting size=2, this
probably result in [[0],[2]]. When setting size=2, this
...
@@ -1473,17 +1473,15 @@ class MRG_RandomStreams(object):
...
@@ -1473,17 +1473,15 @@ class MRG_RandomStreams(object):
Notes
Notes
-----
-----
-
`size` and `ndim` are only there keep the same signature as other
-`size` and `ndim` are only there keep the same signature as other
uniform, binomial, normal, etc.
uniform, binomial, normal, etc.
-
Does not do any value checking on pvals, i.e. there is no
-Does not do any value checking on pvals, i.e. there is no
check that the elements are non-negative, less than 1, or
check that the elements are non-negative, less than 1, or
sum to 1. passing pvals = [[-2., 2.]] will result in
sum to 1. passing pvals = [[-2., 2.]] will result in
sampling [[0, 0]]
sampling [[0, 0]]
- When `a` and `p` are tensors their shape should be the same.
-Only replace=False is implemented for now.
- Only replace=False is implemented for now.
"""
"""
if
replace
:
if
replace
:
...
@@ -1491,40 +1489,37 @@ class MRG_RandomStreams(object):
...
@@ -1491,40 +1489,37 @@ class MRG_RandomStreams(object):
"MRG_RandomStreams.choice only works without replacement "
"MRG_RandomStreams.choice only works without replacement "
"for now."
)
"for now."
)
if
a
is
not
None
:
raise
TypeError
(
"For now, a has to be None in "
"MRG_RandomStreams.choice. Sampled values are "
"beween 0 and p.shape[1]-1"
)
if
p
is
None
:
if
p
is
None
:
raise
TypeError
(
"You have to specify p."
)
raise
TypeError
(
"For now, p has to be specified in "
"MRG_RandomStreams.choice."
)
p
=
as_tensor_variable
(
p
)
p
=
as_tensor_variable
(
p
)
if
ndim
is
not
None
:
if
ndim
is
not
None
:
raise
ValueError
(
"ndim argument to "
raise
ValueError
(
"ndim argument to "
"MRG_RandomStreams.
multinomial_wo_replacement
"
"MRG_RandomStreams.
choice
"
"is not used."
)
"is not used."
)
if
p
.
ndim
==
1
:
p
=
tensor
.
shape_padleft
(
p
)
if
p
.
ndim
!=
2
:
if
p
.
ndim
!=
2
:
raise
NotImplementedError
(
raise
NotImplementedError
(
"MRG_RandomStreams.multinomial_wo_replacement only implemented"
"MRG_RandomStreams.choice is only implemented for p.ndim = 2"
)
" for p.ndim = 1 or p.ndim = 2"
)
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
.
MultinomialWOReplacementFromUniform
(
dtype
)
op
=
multinomial
.
MultinomialWOReplacementFromUniform
(
dtype
)
sampled_indices
=
op
(
p
,
unis
,
as_tensor_variable
(
size
))
return
op
(
p
,
unis
,
as_tensor_variable
(
size
))
if
isinstance
(
a
,
int
):
return
sampled_indices
a
=
tensor
.
as_tensor_variable
(
a
)
return
a
[
sampled_indices
]
def
multinomial_wo_replacement
(
self
,
size
=
None
,
n
=
1
,
pvals
=
None
,
def
multinomial_wo_replacement
(
self
,
size
=
None
,
n
=
1
,
pvals
=
None
,
ndim
=
None
,
dtype
=
'int64'
,
nstreams
=
None
):
ndim
=
None
,
dtype
=
'int64'
,
nstreams
=
None
):
warnings
.
warn
(
'MRG_RandomStreams.multinomial_wo_replacement() is '
warnings
.
warn
(
'MRG_RandomStreams.multinomial_wo_replacement() is '
'deprecated and will be removed in the next release of '
'deprecated and will be removed in the next release of '
'Theano. Please use MRG_RandomStreams.choice() instead.'
)
'Theano. Please use MRG_RandomStreams.choice() instead.'
)
return
self
.
choice
(
size
=
n
,
replace
=
False
,
p
=
pvals
,
dtype
=
dtype
,
return
self
.
choice
(
size
=
n
,
a
=
None
,
replace
=
False
,
p
=
pvals
,
nstreams
=
nstreams
,
ndim
=
ndim
)
dtype
=
dtype
,
nstreams
=
nstreams
,
ndim
=
ndim
)
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论