Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
af18317e
提交
af18317e
authored
11月 13, 2013
作者:
Sigurd Spieckermann
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added numpy.random.choice function to theano.tensor.raw_random random
numbers implementation
上级
c2d3f2f6
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
83 行增加
和
0 行删除
+83
-0
raw_random.py
theano/tensor/raw_random.py
+56
-0
test_raw_random.py
theano/tensor/tests/test_raw_random.py
+27
-0
没有找到文件。
theano/tensor/raw_random.py
浏览文件 @
af18317e
...
...
@@ -572,6 +572,49 @@ 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.
"""
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.
...
...
@@ -829,6 +872,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
浏览文件 @
af18317e
...
...
@@ -474,6 +474,33 @@ 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."""
# 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
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论