Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b2a3ac6c
提交
b2a3ac6c
authored
5月 12, 2011
作者:
Olivier Delalleau
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fixed potential bug with default dtypes, and added ability to control nstreams…
Fixed potential bug with default dtypes, and added ability to control nstreams in other sampling methods than just uniform
上级
afc67129
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
60 行增加
和
24 行删除
+60
-24
rng_mrg.py
theano/sandbox/rng_mrg.py
+60
-24
没有找到文件。
theano/sandbox/rng_mrg.py
浏览文件 @
b2a3ac6c
...
@@ -589,6 +589,35 @@ class GPU_mrg_uniform(mrg_uniform_base):
...
@@ -589,6 +589,35 @@ class GPU_mrg_uniform(mrg_uniform_base):
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
4
,)
return
(
4
,)
def
guess_n_streams
(
size
,
warn
=
True
):
"""
Return a guess at a good number of streams.
:param warn: If True, warn when a guess cannot be made (in which case
we return 30 * 256).
"""
# TODO: a smart way of choosing the number of streams, see #612.
# Note that this code was moved out of `MRG_RandomStreams` so that it can
# be easily accessed from tests, where we want to disable the warning.
if
(
isinstance
(
size
,
(
tuple
,
list
))
and
all
([
isinstance
(
i
,
int
)
for
i
in
size
])):
# We can make a guess.
r
=
1
for
s
in
size
:
r
*=
s
if
r
>
6
:
r
=
r
/
6
# chosen as fastest for rbm_benchmark
return
r
else
:
if
warn
:
assert
False
print
>>
sys
.
stderr
,
(
"MRG_RandomStreams Can't determine #streams from "
"size (
%
s), guessing 30*256"
)
%
str
(
size
)
return
30
*
256
class
MRG_RandomStreams
(
object
):
class
MRG_RandomStreams
(
object
):
"""Module component with similar interface to numpy.random (numpy.random.RandomState)"""
"""Module component with similar interface to numpy.random (numpy.random.RandomState)"""
...
@@ -654,18 +683,7 @@ class MRG_RandomStreams(object):
...
@@ -654,18 +683,7 @@ class MRG_RandomStreams(object):
return
rval
return
rval
def
n_streams
(
self
,
size
):
def
n_streams
(
self
,
size
):
# TODO: a smart way of choosing the number of streams, see #612.
return
guess_n_streams
(
size
,
warn
=
True
)
if
isinstance
(
size
,
(
tuple
,
list
))
and
all
([
isinstance
(
i
,
int
)
for
i
in
size
]):
r
=
1
for
s
in
size
:
r
*=
s
if
r
>
6
:
r
=
r
/
6
# chosen as fastest for rbm_benchmark
return
r
print
>>
sys
.
stderr
,
(
"MRG_RandomStreams Can't determine #streams from "
"size (
%
s), guessing 30*256"
)
%
str
(
size
)
return
30
*
256
def
pretty_return
(
self
,
node_rstate
,
new_rstate
,
sample
):
def
pretty_return
(
self
,
node_rstate
,
new_rstate
,
sample
):
sample
.
rstate
=
node_rstate
sample
.
rstate
=
node_rstate
...
@@ -674,7 +692,8 @@ class MRG_RandomStreams(object):
...
@@ -674,7 +692,8 @@ class MRG_RandomStreams(object):
node_rstate
.
default_update
=
new_rstate
node_rstate
.
default_update
=
new_rstate
return
sample
return
sample
def
uniform
(
self
,
size
=
None
,
low
=
0.0
,
high
=
1.0
,
ndim
=
None
,
dtype
=
config
.
floatX
,
nstreams
=
None
):
def
uniform
(
self
,
size
,
low
=
0.0
,
high
=
1.0
,
ndim
=
None
,
dtype
=
'floatX'
,
nstreams
=
None
):
"""
"""
Sample a tensor of given size whose element from a uniform
Sample a tensor of given size whose element from a uniform
distribution between low and high.
distribution between low and high.
...
@@ -683,10 +702,14 @@ class MRG_RandomStreams(object):
...
@@ -683,10 +702,14 @@ class MRG_RandomStreams(object):
ndim may be a plain integer to supplement the missing
ndim may be a plain integer to supplement the missing
information.
information.
:param
:
size: Can be a list of integer or Theano variable
:param size: Can be a list of integer or Theano variable
(ex: the shape of other Theano Variable)
(ex: the shape of other Theano Variable)
TODO: can size be None?
:param dtype: The output data type.
"""
"""
if
dtype
==
'floatX'
:
dtype
=
config
.
floatX
if
isinstance
(
size
,
tuple
):
if
isinstance
(
size
,
tuple
):
msg
=
"size must be a tuple of int or a Theano variable"
msg
=
"size must be a tuple of int or a Theano variable"
assert
all
([
isinstance
(
i
,
int
)
or
isinstance
(
i
,
Variable
)
assert
all
([
isinstance
(
i
,
int
)
or
isinstance
(
i
,
Variable
)
...
@@ -728,16 +751,19 @@ class MRG_RandomStreams(object):
...
@@ -728,16 +751,19 @@ class MRG_RandomStreams(object):
raise
NotImplementedError
(
'Increase the size to match the broadcasting pattern of `low` and `high` arguments'
)
raise
NotImplementedError
(
'Increase the size to match the broadcasting pattern of `low` and `high` arguments'
)
return
r
return
r
def
binomial
(
self
,
size
=
None
,
n
=
1
,
p
=
0.5
,
ndim
=
None
,
dtype
=
'int64'
):
def
binomial
(
self
,
size
=
None
,
n
=
1
,
p
=
0.5
,
ndim
=
None
,
dtype
=
'int64'
,
nstreams
=
None
):
if
n
==
1
:
if
n
==
1
:
if
dtype
==
'float32'
and
self
.
use_cuda
:
if
dtype
==
'float32'
and
self
.
use_cuda
:
return
cast
(
self
.
uniform
(
size
=
size
,
dtype
=
dtype
)
<
p
,
dtype
)
x
=
self
.
uniform
(
size
=
size
,
dtype
=
dtype
,
nstreams
=
nstreams
)
else
:
else
:
return
cast
(
self
.
uniform
(
size
=
size
)
<
p
,
dtype
)
x
=
self
.
uniform
(
size
=
size
,
nstreams
=
nstreams
)
return
cast
(
x
<
p
,
dtype
)
else
:
else
:
raise
NotImplementedError
(
"MRG_RandomStreams.binomial with n > 1"
)
raise
NotImplementedError
(
"MRG_RandomStreams.binomial with n > 1"
)
def
multinomial
(
self
,
size
=
None
,
n
=
1
,
pvals
=
None
,
ndim
=
None
,
dtype
=
'int64'
):
def
multinomial
(
self
,
size
=
None
,
n
=
1
,
pvals
=
None
,
ndim
=
None
,
dtype
=
'int64'
,
nstreams
=
None
):
"""
"""
Sample `n` (currently `n` needs to be 1) times from a multinomial
Sample `n` (currently `n` needs to be 1) times from a multinomial
distribution defined by probabilities pvals.
distribution defined by probabilities pvals.
...
@@ -758,22 +784,31 @@ class MRG_RandomStreams(object):
...
@@ -758,22 +784,31 @@ class MRG_RandomStreams(object):
ndim
,
size
,
pvals
[:,
0
])
ndim
,
size
,
pvals
[:,
0
])
assert
ndim
==
1
assert
ndim
==
1
bcast
=
bcast
+
(
pvals
.
type
.
broadcastable
[
-
1
],)
bcast
=
bcast
+
(
pvals
.
type
.
broadcastable
[
-
1
],)
unis
=
self
.
uniform
(
size
=
size
,
ndim
=
1
)
unis
=
self
.
uniform
(
size
=
size
,
ndim
=
1
,
nstreams
=
nstreams
)
op
=
multinomial
.
MultinomialFromUniform
(
dtype
)
op
=
multinomial
.
MultinomialFromUniform
(
dtype
)
return
op
(
pvals
,
unis
)
return
op
(
pvals
,
unis
)
else
:
else
:
raise
NotImplementedError
((
"MRG_RandomStreams.multinomial only"
raise
NotImplementedError
((
"MRG_RandomStreams.multinomial only"
" implemented with n == 1 and pvals.ndim = 2"
))
" implemented with n == 1 and pvals.ndim = 2"
))
def
normal
(
self
,
size
=
None
,
avg
=
0.0
,
std
=
1.0
,
ndim
=
None
,
dtype
=
config
.
floatX
):
def
normal
(
self
,
size
=
None
,
avg
=
0.0
,
std
=
1.0
,
ndim
=
None
,
dtype
=
'floatX'
,
nstreams
=
None
):
"""
"""
:param: size: Can be a list of integer or Theano variable(ex: the shape of other Theano Variable)
:param size: Can be a list of integers or Theano variables (ex: the
shape of another Theano Variable)
:param dtype: The output data type.
:param nstreams: Number of streams.
"""
"""
# We need an even number of ]0,1[ samples. Then we split them
# We need an even number of ]0,1[ samples. Then we split them
# in two halves. First half becomes our U1's for Box-Muller,
# in two halves. First half becomes our U1's for Box-Muller,
# second half our U2's. See Wikipedia page:
# second half our U2's. See Wikipedia page:
# http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
# http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
if
dtype
==
'floatX'
:
dtype
=
config
.
floatX
evened
=
False
evened
=
False
constant
=
False
constant
=
False
if
isinstance
(
size
,
tuple
)
and
all
([
isinstance
(
i
,
int
)
for
i
in
size
]):
if
isinstance
(
size
,
tuple
)
and
all
([
isinstance
(
i
,
int
)
for
i
in
size
]):
...
@@ -786,7 +821,8 @@ class MRG_RandomStreams(object):
...
@@ -786,7 +821,8 @@ class MRG_RandomStreams(object):
else
:
else
:
#if even, don't change, if odd, +1
#if even, don't change, if odd, +1
n_samples
=
prod
(
size
)
+
(
prod
(
size
)
%
2
)
n_samples
=
prod
(
size
)
+
(
prod
(
size
)
%
2
)
flattened
=
self
.
uniform
(
size
=
(
n_samples
,),
dtype
=
dtype
)
flattened
=
self
.
uniform
(
size
=
(
n_samples
,),
dtype
=
dtype
,
nstreams
=
nstreams
)
if
constant
:
if
constant
:
U1
=
flattened
[:
n_samples
//
2
]
U1
=
flattened
[:
n_samples
//
2
]
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论