Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6113a371
提交
6113a371
authored
2月 04, 2010
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add numpy tensor versions of random_integers and multinomial (not supported by numpy)
上级
c72fc516
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
165 行增加
和
2 行删除
+165
-2
raw_random.py
theano/tensor/raw_random.py
+165
-2
没有找到文件。
theano/tensor/raw_random.py
浏览文件 @
6113a371
...
@@ -306,6 +306,84 @@ def normal(random_state, size=None, avg=0.0, std=1.0, ndim=None):
...
@@ -306,6 +306,84 @@ def normal(random_state, size=None, avg=0.0, std=1.0, ndim=None):
tensor
.
TensorType
(
dtype
=
'float64'
,
broadcastable
=
(
False
,)
*
ndim
)
)
tensor
.
TensorType
(
dtype
=
'float64'
,
broadcastable
=
(
False
,)
*
ndim
)
)
return
op
(
random_state
,
size
,
avg
,
std
)
return
op
(
random_state
,
size
,
avg
,
std
)
def
random_integers_helper
(
random_state
,
low
,
high
,
size
):
'''
Helper function to draw random integers.
This is a generalization of numpy.random.random_integers to the case where
low and high are tensors.
'''
# Figure out the output shape
if
size
is
not
None
:
out_ndim
=
len
(
size
)
else
:
out_ndim
=
max
(
low
.
ndim
,
high
.
ndim
)
# broadcast low and high to out_ndim dimensions
if
low
.
ndim
>
out_ndim
:
raise
ValueError
(
'low.ndim (
%
i) should not be larger than len(size) (
%
i)'
%
(
low
.
ndim
,
out_ndim
),
low
,
size
)
if
low
.
ndim
<
out_ndim
:
low
=
low
.
reshape
((
1
,)
*
(
out_ndim
-
low
.
ndim
)
+
low
.
shape
)
if
high
.
ndim
>
out_ndim
:
raise
ValueError
(
'high.ndim (
%
i) should not be larger than len(size) (
%
i)'
%
(
high
.
ndim
,
out_ndim
),
high
,
size
)
if
high
.
ndim
<
out_ndim
:
high
=
high
.
reshape
((
1
,)
*
(
out_ndim
-
high
.
ndim
)
+
high
.
shape
)
if
size
is
not
None
:
out_size
=
tuple
(
size
)
else
:
out_size
=
()
for
dim
in
range
(
out_ndim
):
dim_len
=
max
(
low
.
shape
[
dim
],
high
.
shape
[
dim
])
out_size
=
out_size
+
(
dim_len
,)
# Build the indices over which to loop
# This process leads to the same result as numpy.ndindex for out_ind,
# but allows for indices of low and high to be repeated if these
# tensors are broadcasted along some dimensions.
# TODO: move the logic somewhere else
out_ind
=
[()]
low_ind
=
[()]
high_ind
=
[()]
for
dim
in
range
(
out_ndim
):
_out_ind
=
[]
_low_ind
=
[]
_high_ind
=
[]
o_range
=
range
(
out_size
[
dim
])
if
low
.
shape
[
dim
]
==
out_size
[
dim
]:
l_range
=
o_range
elif
low
.
shape
[
dim
]
==
1
:
#broadcast
l_range
=
(
0
,)
*
out_size
[
dim
]
else
:
raise
ValueError
(
'low.shape[
%
i] (
%
i) should be equal to size[
%
i] (
%
i) or to 1'
\
%
(
dim
,
low
.
shape
[
dim
],
dim
,
out_size
[
dim
]),
low
,
size
)
if
high
.
shape
[
dim
]
==
out_size
[
dim
]:
h_range
=
o_range
elif
high
.
shape
[
dim
]
==
1
:
#broadcast
h_range
=
(
0
,)
*
out_size
[
dim
]
else
:
raise
ValueError
(
'high.shape[
%
i] (
%
i) should be equal to size[
%
i] (
%
i) or to 1'
\
%
(
dim
,
high
.
shape
[
dim
],
dim
,
out_size
[
dim
]),
high
,
size
)
for
(
ol
,
ll
,
hl
)
in
zip
(
out_ind
,
low_ind
,
high_ind
):
for
oi
,
li
,
hi
in
zip
(
o_range
,
l_range
,
h_range
):
_out_ind
.
append
(
ol
+
(
oi
,))
_low_ind
.
append
(
ll
+
(
li
,))
_high_ind
.
append
(
hl
+
(
hi
,))
out_ind
=
_out_ind
low_ind
=
_low_ind
high_ind
=
_high_ind
# Iterate over these indices, drawing one sample at a time from numpy
out
=
numpy
.
ndarray
(
out_size
)
for
oi
,
li
,
hi
in
zip
(
out_ind
,
low_ind
,
high_ind
):
out
[
oi
]
=
random_state
.
random_integers
(
low
=
low
[
li
],
high
=
high
[
li
])
return
out
def
random_integers
(
random_state
,
size
=
None
,
low
=
0
,
high
=
1
,
ndim
=
None
):
def
random_integers
(
random_state
,
size
=
None
,
low
=
0
,
high
=
1
,
ndim
=
None
):
"""
"""
Usage: random_integers(random_state, size, low=0, high=1)
Usage: random_integers(random_state, size, low=0, high=1)
...
@@ -318,7 +396,7 @@ def random_integers(random_state, size=None, low=0, high=1, ndim=None):
...
@@ -318,7 +396,7 @@ def random_integers(random_state, size=None, low=0, high=1, ndim=None):
low
=
tensor
.
as_tensor_variable
(
low
)
low
=
tensor
.
as_tensor_variable
(
low
)
high
=
tensor
.
as_tensor_variable
(
high
)
high
=
tensor
.
as_tensor_variable
(
high
)
ndim
,
size
=
_infer_ndim
(
ndim
,
size
,
low
,
high
)
ndim
,
size
=
_infer_ndim
(
ndim
,
size
,
low
,
high
)
op
=
RandomFunction
(
'random_integers'
,
op
=
RandomFunction
(
random_integers_helper
,
tensor
.
TensorType
(
dtype
=
'int64'
,
broadcastable
=
(
False
,)
*
ndim
)
)
tensor
.
TensorType
(
dtype
=
'int64'
,
broadcastable
=
(
False
,)
*
ndim
)
)
return
op
(
random_state
,
size
,
low
,
high
)
return
op
(
random_state
,
size
,
low
,
high
)
...
@@ -372,6 +450,91 @@ def permutation(random_state, size=None, n=1, ndim=None):
...
@@ -372,6 +450,91 @@ def permutation(random_state, size=None, n=1, ndim=None):
ndim_added
=
1
)
ndim_added
=
1
)
return
op
(
random_state
,
size
,
n
)
return
op
(
random_state
,
size
,
n
)
def
multinomial_helper
(
random_state
,
n
,
pvals
,
size
):
'''
Helper function drawing from multinomial distributions.
This is a generalization of numpy.random.multinomial to the case where
n and pvals are tensors.
'''
# Figure out the shape if it's None
# Note: the output ndim will be ndim+1, because the multinomial
# adds a dimension. The length of that dimension is pvals.shape[-1].
if
size
is
not
None
:
ndim
=
len
(
size
)
else
:
ndim
=
max
(
n
.
ndim
,
pvals
.
ndim
-
1
)
out_ndim
=
ndim
+
1
# broadcast n to ndim dimensions and pvals to ndim+1
if
n
.
ndim
>
ndim
:
raise
ValueError
(
'n.ndim (
%
i) should not be larger than len(size) (
%
i)'
%
(
n
.
ndim
,
ndim
),
n
,
size
)
if
n
.
ndim
<
ndim
:
n
=
n
.
reshape
((
1
,)
*
(
ndim
-
n
.
ndim
)
+
n
.
shape
)
if
pvals
.
ndim
-
1
>
ndim
:
raise
ValueError
(
'pvals.ndim-1 (
%
i) should not be larger than len(size) (
%
i)'
%
(
pvals
.
ndim
-
1
,
ndim
),
pvals
,
size
)
if
pvals
.
ndim
-
1
<
ndim
:
pvals
=
pvals
.
reshape
((
1
,)
*
(
ndim
-
pvals
.
ndim
+
1
)
+
pvals
.
shape
)
if
size
is
not
None
:
size
=
tuple
(
size
)
else
:
size
=
()
for
dim
in
range
(
ndim
):
dim_len
=
max
(
n
.
shape
[
dim
],
pvals
.
shape
[
dim
])
size
=
size
+
(
dim_len
,)
out_size
=
size
+
(
pvals
.
shape
[
-
1
],)
# Build the indices over which to loop
# This process leads to the same result as numpy.ndindex for main_ind,
# but allows for indices of n and pvals to be repeated if these tensors
# are broadcasted along some dimensions.
# TODO: move the logic somewhere else
# Note that here, pvals_ind and main_ind index the rows (inner-most
# 1D subtensors) of pvals and out (respectively), not their
# individual elements
main_ind
=
[()]
n_ind
=
[()]
pvals_ind
=
[()]
for
dim
in
range
(
ndim
):
_main_ind
=
[]
_n_ind
=
[]
_pvals_ind
=
[]
m_range
=
range
(
size
[
dim
])
if
n
.
shape
[
dim
]
==
size
[
dim
]:
n_range
=
m_range
elif
n
.
shape
[
dim
]
==
1
:
#broadcast
n_range
=
(
0
,)
*
size
[
dim
]
else
:
raise
ValueError
(
'n.shape[
%
i] (
%
i) should be equal to size[
%
i] (
%
i) or to 1'
\
%
(
dim
,
n
.
shape
[
dim
],
dim
,
size
[
dim
]),
n
,
size
)
if
pvals
.
shape
[
dim
]
==
size
[
dim
]:
p_range
=
m_range
elif
pvals
.
shape
[
dim
]
==
1
:
#broadcast
p_range
=
(
0
,)
*
size
[
dim
]
else
:
raise
ValueError
(
'pvals.shape[
%
i] (
%
i) should be equal to size[
%
i] (
%
i) or to 1'
\
%
(
dim
,
pvals
.
shape
[
dim
],
dim
,
size
[
dim
]),
pvals
,
size
)
for
(
ml
,
nl
,
pl
)
in
zip
(
main_ind
,
n_ind
,
pvals_ind
):
for
mi
,
ni
,
pi
in
zip
(
m_range
,
n_range
,
p_range
):
_main_ind
.
append
(
ml
+
(
mi
,))
_n_ind
.
append
(
nl
+
(
ni
,))
_pvals_ind
.
append
(
pl
+
(
pi
,))
main_ind
=
_main_ind
n_ind
=
_n_ind
pvals_ind
=
_pvals_ind
# Iterate over these indices, drawing from one multinomial at a time from numpy
out
=
numpy
.
ndarray
(
out_size
)
for
mi
,
ni
,
pi
in
zip
(
main_ind
,
n_ind
,
pvals_ind
):
out
[
mi
]
=
random_state
.
multinomial
(
n
=
n
[
ni
],
pvals
=
pvals
[
pi
])
return
out
def
multinomial
(
random_state
,
size
=
None
,
n
=
1
,
pvals
=
[
0.5
,
0.5
],
ndim
=
None
):
def
multinomial
(
random_state
,
size
=
None
,
n
=
1
,
pvals
=
[
0.5
,
0.5
],
ndim
=
None
):
"""
"""
Sample n times from a multinomial distribution defined by probabilities pvals,
Sample n times from a multinomial distribution defined by probabilities pvals,
...
@@ -387,7 +550,7 @@ def multinomial(random_state, size=None, n=1, pvals=[0.5, 0.5], ndim=None):
...
@@ -387,7 +550,7 @@ def multinomial(random_state, size=None, n=1, pvals=[0.5, 0.5], ndim=None):
n
=
tensor
.
as_tensor_variable
(
n
)
n
=
tensor
.
as_tensor_variable
(
n
)
pvals
=
tensor
.
as_tensor_variable
(
pvals
)
pvals
=
tensor
.
as_tensor_variable
(
pvals
)
ndim
,
size
=
_infer_ndim
(
ndim
,
size
,
n
,
pvals
[
0
])
ndim
,
size
=
_infer_ndim
(
ndim
,
size
,
n
,
pvals
[
0
])
op
=
RandomFunction
(
'multinomial'
,
op
=
RandomFunction
(
multinomial_helper
,
tensor
.
TensorType
(
dtype
=
'int64'
,
broadcastable
=
(
False
,)
*
(
ndim
+
1
)),
tensor
.
TensorType
(
dtype
=
'int64'
,
broadcastable
=
(
False
,)
*
(
ndim
+
1
)),
ndim_added
=
1
)
ndim_added
=
1
)
return
op
(
random_state
,
size
,
n
,
pvals
)
return
op
(
random_state
,
size
,
n
,
pvals
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论