Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
fd40e50e
提交
fd40e50e
authored
11月 16, 2013
作者:
Frédéric Bastien
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1607 from sisp/raw_random_choice
added numpy.random.choice function
上级
85db9b9d
43e62d0f
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
117 行增加
和
4 行删除
+117
-4
raw_random.py
theano/tensor/raw_random.py
+61
-0
test_raw_random.py
theano/tensor/tests/test_raw_random.py
+34
-4
test_shared_randomstreams.py
theano/tensor/tests/test_shared_randomstreams.py
+22
-0
没有找到文件。
theano/tensor/raw_random.py
浏览文件 @
fd40e50e
...
...
@@ -575,6 +575,54 @@ def random_integers(random_state, size=None, low=0, high=1, ndim=None,
return
op
(
random_state
,
size
,
low
,
high
)
def
choice_helper
(
random_state
,
a
,
replace
,
p
,
size
):
"""
Helper function to draw random numbers using numpy's choice function.
This is a generalization of numpy.random.choice to the case where `a`,
`replace` and `p` are tensors.
"""
if
a
.
ndim
>
1
:
raise
ValueError
(
'a.ndim (
%
i) must be 0 or 1'
%
a
.
ndim
)
if
p
.
ndim
==
1
:
if
p
.
size
==
0
:
p
=
None
else
:
raise
ValueError
(
'p.ndim (
%
i) must be 1'
%
p
.
ndim
)
replace
=
bool
(
replace
)
return
random_state
.
choice
(
a
,
size
,
replace
,
p
)
def
choice
(
random_state
,
size
=
None
,
a
=
2
,
replace
=
True
,
p
=
None
,
ndim
=
None
,
dtype
=
'int64'
):
"""
Choose values from `a` with or without replacement. `a` can be a 1-D array
or a positive scalar. If `a` is a scalar, the samples are drawn from the
range 0,...,a-1.
If the size argument is ambiguous on the number of dimensions, ndim
may be a plain integer to supplement the missing information.
If size is None, a scalar will be returned.
"""
# numpy.random.choice is only available for numpy versions >= 1.7
major
,
minor
,
_
=
numpy
.
version
.
short_version
.
split
(
'.'
)
if
(
int
(
major
),
int
(
minor
))
<
(
1
,
7
):
raise
ImportError
(
'choice requires at NumPy version >= 1.7 '
'(
%
s)'
%
numpy
.
__version__
)
a
=
tensor
.
as_tensor_variable
(
a
)
if
isinstance
(
replace
,
bool
):
replace
=
tensor
.
constant
(
replace
,
dtype
=
'int8'
)
else
:
replace
=
tensor
.
as_tensor_variable
(
replace
)
# encode p=None as an empty vector
p
=
tensor
.
as_tensor_variable
(
p
or
[])
ndim
,
size
,
bcast
=
_infer_ndim_bcast
(
ndim
,
size
)
op
=
RandomFunction
(
choice_helper
,
tensor
.
TensorType
(
dtype
=
dtype
,
broadcastable
=
bcast
))
return
op
(
random_state
,
size
,
a
,
replace
,
p
)
def
permutation_helper
(
random_state
,
n
,
shape
):
"""Helper function to generate permutations from integers.
...
...
@@ -832,6 +880,19 @@ class RandomStreamsBase(object):
"""
return
self
.
gen
(
random_integers
,
size
,
low
,
high
,
ndim
=
ndim
,
dtype
=
dtype
)
def
choice
(
self
,
size
=
None
,
a
=
2
,
replace
=
True
,
p
=
None
,
ndim
=
None
,
dtype
=
'int64'
):
"""
Choose values from `a` with or without replacement. `a` can be a 1-D
array or a positive scalar. If `a` is a scalar, the samples are drawn
from the range 0,...,a-1.
If the size argument is ambiguous on the number of dimensions,
ndim may be a plain integer to supplement the missing
information.
"""
return
self
.
gen
(
choice
,
size
,
a
,
replace
,
p
,
ndim
=
ndim
,
dtype
=
dtype
)
def
permutation
(
self
,
size
=
None
,
n
=
1
,
ndim
=
None
,
dtype
=
'int64'
):
"""
...
...
theano/tensor/tests/test_raw_random.py
浏览文件 @
fd40e50e
__docformat__
=
"restructuredtext en"
import
sys
import
unittest
import
numpy
as
N
import
numpy
from
theano.tests
import
unittest_tools
as
utt
from
theano.tensor.raw_random
import
*
from
theano.tensor
import
(
raw_random
,
ivector
,
dvector
,
iscalar
,
dcol
,
dtensor3
)
from
theano.tests
import
unittest_tools
as
utt
from
theano
import
tensor
from
theano
import
compile
,
config
,
gof
...
...
@@ -474,6 +471,39 @@ class T_random_function(utt.InferShapeTester):
update
=
post_r2
,
mutable
=
True
)],
[
out2
],
accept_inplace
=
True
)
self
.
assertRaises
(
ValueError
,
f2
)
def
test_choice
(
self
):
"""Test that raw_random.choice generates the same
results as numpy."""
# numpy.random.choice is only available for numpy versions >= 1.7
major
,
minor
,
_
=
numpy
.
version
.
short_version
.
split
(
'.'
)
if
(
int
(
major
),
int
(
minor
))
<
(
1
,
7
):
raise
utt
.
SkipTest
(
'choice requires at NumPy version >= 1.7 '
'(
%
s)'
%
numpy
.
__version__
)
# Check over two calls to see if the random state is correctly updated.
rng_R
=
random_state_type
()
# Use non-default parameters, and larger dimensions because of
# the integer nature of the result
post_r
,
out
=
choice
(
rng_R
,
(
11
,
8
),
10
,
1
,
0
)
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
numpy_rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
val0
=
f
()
val1
=
f
()
numpy_val0
=
numpy_rng
.
choice
(
10
,
(
11
,
8
),
True
,
None
)
numpy_val1
=
numpy_rng
.
choice
(
10
,
(
11
,
8
),
True
,
None
)
print
val0
print
numpy_val0
print
val1
print
numpy_val1
self
.
assertTrue
(
numpy
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
numpy
.
allclose
(
val1
,
numpy_val1
))
def
test_permutation
(
self
):
"""Test that raw_random.permutation generates the same
...
...
theano/tensor/tests/test_shared_randomstreams.py
浏览文件 @
fd40e50e
...
...
@@ -182,6 +182,28 @@ class T_SharedRandomStreams(unittest.TestCase):
assert
numpy
.
all
(
fn_val0
==
numpy_val0
)
assert
numpy
.
all
(
fn_val1
==
numpy_val1
)
def
test_choice
(
self
):
"""Test that RandomStreams.choice generates the same results as numpy"""
# numpy.random.choice is only available for numpy versions >= 1.7
major
,
minor
,
_
=
numpy
.
version
.
short_version
.
split
(
'.'
)
if
(
int
(
major
),
int
(
minor
))
<
(
1
,
7
):
raise
utt
.
SkipTest
(
'choice requires at NumPy version >= 1.7 '
'(
%
s)'
%
numpy
.
__version__
)
# Check over two calls to see if the random state is correctly updated.
random
=
RandomStreams
(
utt
.
fetch_seed
())
fn
=
function
([],
random
.
choice
((
11
,
8
),
10
,
1
,
0
))
fn_val0
=
fn
()
fn_val1
=
fn
()
rng_seed
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
.
randint
(
2
**
30
)
rng
=
numpy
.
random
.
RandomState
(
int
(
rng_seed
))
#int() is for 32bit
numpy_val0
=
rng
.
choice
(
10
,
(
11
,
8
),
True
,
None
)
numpy_val1
=
rng
.
choice
(
10
,
(
11
,
8
),
True
,
None
)
assert
numpy
.
all
(
fn_val0
==
numpy_val0
)
assert
numpy
.
all
(
fn_val1
==
numpy_val1
)
def
test_permutation
(
self
):
"""Test that RandomStreams.permutation generates the same results as numpy"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论