Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4f48b960
提交
4f48b960
authored
6月 03, 2008
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
moved uniform and binomial in tensor_random to RandomState class methods
上级
705dfaaf
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
49 行增加
和
48 行删除
+49
-48
__init__.py
__init__.py
+1
-0
tensor_random.py
tensor_random.py
+48
-48
没有找到文件。
__init__.py
浏览文件 @
4f48b960
...
@@ -12,6 +12,7 @@ from gof import \
...
@@ -12,6 +12,7 @@ from gof import \
from
compile
import
function
,
eval_outputs
,
fast_compute
from
compile
import
function
,
eval_outputs
,
fast_compute
import
tensor
import
tensor
import
tensor_random
import
scalar
import
scalar
import
sparse
import
sparse
import
gradient
import
gradient
...
...
tensor_random.py
浏览文件 @
4f48b960
...
@@ -14,31 +14,6 @@ class RandomState(object):
...
@@ -14,31 +14,6 @@ class RandomState(object):
instances returned by gen(), gen_like()
instances returned by gen(), gen_like()
@type seed: int
@type seed: int
"""
"""
@staticmethod
def
_fn_from_dist
(
dist
,
cache
=
{}):
"""Return a function from a distribution description
@param dist: identifier of a sampling distribution.
@type dist: callable or str or tuple(str, dict)
@param cache: The optional cache argument implements a closure, which ensures that
multiple requests for the same sampling function will get the same
sampling function. L{NumpyGenerator}.__hash__ depends on this.
@type cache: dict
"""
if
callable
(
dist
):
return
dist
if
isinstance
(
dist
,
str
):
return
getattr
(
numpy
.
random
.
RandomState
,
dist
)
name
,
kwargs
=
dist
key
=
(
name
,
tuple
(
kwargs
.
items
()))
if
key
not
in
cache
:
fn
=
getattr
(
numpy
.
random
.
RandomState
,
name
)
fn
=
functools
.
partial
(
fn
,
**
kwargs
)
cache
[
key
]
=
fn
return
cache
[
key
]
def
__init__
(
self
,
seed
):
def
__init__
(
self
,
seed
):
self
.
seed
=
seed
self
.
seed
=
seed
...
@@ -69,6 +44,54 @@ class RandomState(object):
...
@@ -69,6 +44,54 @@ class RandomState(object):
fn
=
RandomState
.
_fn_from_dist
(
dist
)
fn
=
RandomState
.
_fn_from_dist
(
dist
)
return
NumpyGenerator
(
self
.
seed
-
1
,
x
.
type
.
ndim
,
fn
)(
tensor
.
shape
(
x
))
return
NumpyGenerator
(
self
.
seed
-
1
,
x
.
type
.
ndim
,
fn
)(
tensor
.
shape
(
x
))
def
uniform_like
(
self
,
template
,
low
=
0.
,
high
=
1.
):
"""
Return a multivariate uniform(low,high)
random variable in a tensor of the same shape as template
(template can either be a tensor or a shape tuple). Each element of the
resulting tensor is sampled independently. low and high can
be scalars or have the same shape as the template (or broadcastable
to it).
"""
return
self
.
gen_like
((
'uniform'
,{
'low'
:
low
,
'high'
:
high
}),
template
)
def
binomial_like
(
self
,
template
,
n
=
1
,
p
=
0.5
):
"""
Return a multivariate binomial(n,p) random variable in a tensor of the same shape as template
(template can either be a tensor or a shape tuple). Each element of the
resulting tensor is sampled independently. low and high can
be scalars or have the same shape as the template (or broadcastable
to it).
"""
return
self
.
gen_like
((
'binomial'
,{
'n'
:
n
,
'p'
:
p
}),
template
)
@staticmethod
def
_fn_from_dist
(
dist
,
cache
=
{}):
"""Return a function from a distribution description
@param dist: identifier of a sampling distribution.
@type dist: callable or str or tuple(str, dict)
@param cache: The optional cache argument implements a closure, which ensures that
multiple requests for the same sampling function will get the same
sampling function. L{NumpyGenerator}.__hash__ depends on this.
@type cache: dict
"""
if
callable
(
dist
):
return
dist
if
isinstance
(
dist
,
str
):
return
getattr
(
numpy
.
random
.
RandomState
,
dist
)
name
,
kwargs
=
dist
key
=
(
name
,
tuple
(
kwargs
.
items
()))
if
key
not
in
cache
:
fn
=
getattr
(
numpy
.
random
.
RandomState
,
name
)
fn
=
functools
.
partial
(
fn
,
**
kwargs
)
cache
[
key
]
=
fn
return
cache
[
key
]
class
NumpyGenerator
(
gof
.
op
.
Op
):
class
NumpyGenerator
(
gof
.
op
.
Op
):
"""Supply a sequence of random tensors of a given shape, from a given
"""Supply a sequence of random tensors of a given shape, from a given
distribution.
distribution.
...
@@ -120,26 +143,3 @@ class NumpyGenerator(gof.op.Op):
...
@@ -120,26 +143,3 @@ class NumpyGenerator(gof.op.Op):
(
shape
,
self
.
ndim
)
)
(
shape
,
self
.
ndim
)
)
output_storage
[
0
][
0
]
=
self
.
fn
(
rng
,
size
=
shape
)
output_storage
[
0
][
0
]
=
self
.
fn
(
rng
,
size
=
shape
)
def
uniform
(
seed
,
template
,
low
=
0.
,
high
=
1.
):
"""
Return a multivariate uniform(low,high)
random variable in a tensor of the same shape as template
(template can either be a tensor or a shape tuple). Each element of the
resulting tensor is sampled independently. low and high can
be scalars or have the same shape as the template (or broadcastable
to it).
"""
return
RandomState
(
seed
)
.
gen_like
((
'uniform'
,{
'low'
:
low
,
'high'
:
high
}),
template
)
def
binomial
(
seed
,
template
,
n
=
1
,
p
=
0.5
):
"""
Return a multivariate binomial(n,p) random variable in a tensor of the same shape as template
(template can either be a tensor or a shape tuple). Each element of the
resulting tensor is sampled independently. low and high can
be scalars or have the same shape as the template (or broadcastable
to it).
"""
return
RandomState
(
seed
)
.
gen_like
((
'binomial'
,{
'n'
:
n
,
'p'
:
p
}),
template
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论