Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a8178d62
提交
a8178d62
authored
2月 03, 2014
作者:
Xavier Bouthillier/
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added numpy wrapper for poisson distribution
上级
94506569
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
61 行增加
和
0 行删除
+61
-0
raw_random.py
theano/tensor/raw_random.py
+34
-0
test_raw_random.py
theano/tensor/tests/test_raw_random.py
+27
-0
没有找到文件。
theano/tensor/raw_random.py
浏览文件 @
a8178d62
...
@@ -622,6 +622,40 @@ def choice(random_state, size=None, a=2, replace=True, p=None, ndim=None,
...
@@ -622,6 +622,40 @@ def choice(random_state, size=None, a=2, replace=True, p=None, ndim=None,
broadcastable
=
bcast
))
broadcastable
=
bcast
))
return
op
(
random_state
,
size
,
a
,
replace
,
p
)
return
op
(
random_state
,
size
,
a
,
replace
,
p
)
def
poisson_helper
(
random_state
,
lam
,
size
):
"""
Helper function to draw random numbers using numpy's poisson function.
This is a generalization of numpy.random.poisson to the case where
`lam` is a tensor.
"""
return
random_state
.
poisson
(
lam
,
size
)
def
poisson
(
random_state
,
size
=
None
,
lam
=
1.0
,
ndim
=
None
,
dtype
=
'int64'
):
"""
Draw samples from a Poisson distribution.
The Poisson distribution is the limit of the Binomial distribution for large N.
:param lam: float
Expectation of interval, should be >= 0.
:param size: int or tuple of ints, optional
Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn.
:param dtype: the dtype of the return value (which will represent counts)
size or ndim must be given
"""
lam
=
tensor
.
as_tensor_variable
(
lam
)
ndim
,
size
,
bcast
=
_infer_ndim_bcast
(
ndim
,
size
)
op
=
RandomFunction
(
poisson_helper
,
tensor
.
TensorType
(
dtype
=
dtype
,
broadcastable
=
bcast
))
return
op
(
random_state
,
size
,
lam
)
def
permutation_helper
(
random_state
,
n
,
shape
):
def
permutation_helper
(
random_state
,
n
,
shape
):
"""Helper function to generate permutations from integers.
"""Helper function to generate permutations from integers.
...
...
theano/tensor/tests/test_raw_random.py
浏览文件 @
a8178d62
...
@@ -505,6 +505,33 @@ class T_random_function(utt.InferShapeTester):
...
@@ -505,6 +505,33 @@ class T_random_function(utt.InferShapeTester):
self
.
assertTrue
(
numpy
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
numpy
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
numpy
.
allclose
(
val1
,
numpy_val1
))
self
.
assertTrue
(
numpy
.
allclose
(
val1
,
numpy_val1
))
def
test_poisson
(
self
):
"""Test that raw_random.poisson 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
=
poisson
(
rng_R
,
lam
=
5
,
size
=
(
11
,
8
))
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
.
poisson
(
5
,
size
=
(
11
,
8
))
numpy_val1
=
numpy_rng
.
poisson
(
5
,
size
=
(
11
,
8
))
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
):
def
test_permutation
(
self
):
"""Test that raw_random.permutation generates the same
"""Test that raw_random.permutation generates the same
results as numpy."""
results as numpy."""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论